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
46
A
46A
A. Ball Game
800
brute force; implementation
A kindergarten teacher Natalia Pavlovna has invented a new ball game. This game not only develops the children's physique, but also teaches them how to count. The game goes as follows. Kids stand in circle. Let's agree to think of the children as numbered with numbers from 1 to n clockwise and the child number 1 is hol...
The first line contains integer n (2 ≀ n ≀ 100) which indicates the number of kids in the circle.
In the single line print n - 1 numbers which are the numbers of children who will get the ball after each throw. Separate the numbers by spaces.
Input: 10 | Output: 2 4 7 1 6 2 9 7 6
Beginner
2
1,358
97
144
0
768
D
768D
D. Jon and Orbs
2,200
dp; math; probabilities
Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he...
First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively.Each of the next q lines contain a single integer pi (1 ≀ pi ≀ 1000) β€” i-th query.
Output q lines. On i-th of them output single integer β€” answer for i-th query.
Input: 1 11 | Output: 1
Hard
3
694
228
78
7
1,093
E
1093E
E. Intersection of Permutations
2,400
data structures
You are given two permutations \(a\) and \(b\), both consisting of \(n\) elements. Permutation of \(n\) elements is such a integer sequence that each value from \(1\) to \(n\) appears exactly once in it.You are asked to perform two types of queries with them: \(1~l_a~r_a~l_b~r_b\) β€” calculate the number of values which...
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m \le 2 \cdot 10^5\)) β€” the number of elements in both permutations and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) β€” permutation \(a\). It is guaranteed that each...
Print the answers for the queries of the first type, each answer in the new line β€” the number of values which appear in both segment \([l_a; r_a]\) of positions in permutation \(a\) and segment \([l_b; r_b]\) of positions in permutation \(b\).
Consider the first query of the first example. Values on positions \([1; 2]\) of \(a\) are \([5, 1]\) and values on positions \([4; 5]\) of \(b\) are \([1, 4]\). Only value \(1\) appears in both segments.After the first swap (the second query) permutation \(b\) becomes \([2, 1, 3, 5, 4, 6]\).After the second swap (the ...
Input: 6 7 5 1 4 2 3 6 2 5 3 1 4 6 1 1 2 4 5 2 2 4 1 1 2 4 5 1 2 3 3 5 1 1 6 1 2 2 4 1 1 4 4 1 3 | Output: 1 1 1 2 0
Expert
1
666
787
243
10
1,758
F
1758F
F. Decent Division
3,000
constructive algorithms; data structures
A binary string is a string where every character is \(\texttt{0}\) or \(\texttt{1}\). Call a binary string decent if it has an equal number of \(\texttt{0}\)s and \(\texttt{1}\)s.Initially, you have an infinite binary string \(t\) whose characters are all \(\texttt{0}\)s. You are given a sequence \(a\) of \(n\) update...
The first line contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β€” the number of updates.The next \(n\) lines each contain a single integer \(a_i\) (\(1 \leq a_i \leq 2 \cdot 10^5\)) β€” the index of the \(i\)-th update to the string.
After the \(i\)-th update, first output a single integer \(x_i\) β€” the number of ranges to be removed from \(S\) after update \(i\).In the following \(x_i\) lines, output two integers \(l\) and \(r\) (\(1 \leq l < r \leq 10^6\)), which denotes that the range \([l,r]\) should be removed from \(S\). Each of these ranges ...
Line breaks are provided in the sample only for the sake of clarity, and you don't need to print them in your output.After the first update, the set of indices where \(a_i = 1\) is \(\{1\}\). The interval \([1, 2]\) is added, so \(S_1 = \{[1, 2]\}\), which has one \(\texttt{0}\) and one \(\texttt{1}\).After the second ...
Input: 5 1 6 5 5 6 | Output: 0 1 1 2 0 1 5 6 1 5 6 2 6 7 4 5 1 4 5 0 1 6 7 0
Master
2
1,504
247
990
17
1,070
A
1070A
A. Find a Number
2,200
dp; graphs; number theory; shortest paths
You are given two positive integers \(d\) and \(s\). Find minimal positive integer \(n\) which is divisible by \(d\) and has sum of digits equal to \(s\).
The first line contains two positive integers \(d\) and \(s\) (\(1 \le d \le 500, 1 \le s \le 5000\)) separated by space.
Print the required number or -1 if it doesn't exist.
Input: 13 50 | Output: 699998
Hard
4
154
121
52
10
1,303
B
1303B
B. National Project
1,400
math
Your company was appointed to lay new asphalt on the highway of length \(n\). You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there ar...
The first line contains a single integer \(T\) (\(1 \le T \le 10^4\)) β€” the number of test cases.Next \(T\) lines contain test cases β€” one per line. Each line contains three integers \(n\), \(g\) and \(b\) (\(1 \le n, g, b \le 10^9\)) β€” the length of the highway and the number of good and bad days respectively.
Print \(T\) integers β€” one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.
In the first test case, you can just lay new asphalt each day, since days \(1, 3, 5\) are good.In the second test case, you can also lay new asphalt each day, since days \(1\)-\(8\) are good.
Input: 3 5 1 1 8 10 10 1000000 1 1000000 | Output: 5 8 499999500000
Easy
1
1,127
312
180
13
1,190
D
1190D
D. Tokitsukaze and Strange Rectangle
2,000
data structures; divide and conquer; sortings; two pointers
There are \(n\) points on the plane, the \(i\)-th of which is at \((x_i, y_i)\). Tokitsukaze wants to draw a strange rectangular area and pick all the points in the area.The strange area is enclosed by three lines, \(x = l\), \(y = a\) and \(x = r\), as its left side, its bottom side and its right side respectively, wh...
The first line contains a single integer \(n\) (\(1 \leq n \leq 2 \times 10^5\)) β€” the number of points on the plane.The \(i\)-th of the next \(n\) lines contains two integers \(x_i\), \(y_i\) (\(1 \leq x_i, y_i \leq 10^9\)) β€” the coordinates of the \(i\)-th point.All points are distinct.
Print a single integer β€” the number of different non-empty sets of points she can obtain.
For the first example, there is exactly one set having \(k\) points for \(k = 1, 2, 3\), so the total number is \(3\).For the second example, the numbers of sets having \(k\) points for \(k = 1, 2, 3\) are \(3\), \(2\), \(1\) respectively, and their sum is \(6\).For the third example, as the following figure shows, the...
Input: 31 11 21 3 | Output: 3
Hard
4
994
289
89
11
1,999
F
1999F
F. Expected Median
1,500
combinatorics; math
Arul has a binary array\(^{\text{βˆ—}}\) \(a\) of length \(n\).He will take all subsequences\(^{\text{†}}\) of length \(k\) (\(k\) is odd) of this array and find their median.\(^{\text{‑}}\)What is the sum of all these values?As this sum can be very large, output it modulo \(10^9 + 7\). In other words, print the remainde...
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 2 \cdot 10^5\), \(k\) is odd) β€” the length of the array and the length of the subsequence, respectively.The second line of eac...
For each test case, print the sum modulo \(10^9 + 7\).
In the first test case, there are four subsequences of \([1,0,0,1]\) with length \(k=3\): \([1,0,0]\): median \(= 0\). \([1,0,1]\): median \(= 1\). \([1,0,1]\): median \(= 1\). \([0,0,1]\): median \(= 0\). The sum of the results is \(0+1+1+0=2\).In the second test case, all subsequences of length \(1\) have median \(1\...
Input: 84 31 0 0 15 11 1 1 1 15 50 1 0 1 06 31 0 1 0 1 14 31 0 1 15 31 0 1 1 02 10 034 171 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | Output: 2 5 0 16 4 7 0 333606206
Medium
2
748
504
54
19
1,013
B
1013B
B. And
1,200
greedy
There is an array with n elements a1, a2, ..., an and the number x.In one operation you can select some i (1 ≀ i ≀ n) and replace element ai with ai & x, where & denotes the bitwise and operation.You want the array to have at least two equal elements after applying some operations (possibly, none). In other words, ther...
The first line contains integers n and x (2 ≀ n ≀ 100 000, 1 ≀ x ≀ 100 000), number of elements in the array and the number to and with.The second line contains n integers ai (1 ≀ ai ≀ 100 000), the elements of the array.
Print a single integer denoting the minimal number of operations to do, or -1, if it is impossible.
In the first example one can apply the operation to the last element of the array. That replaces 7 with 3, so we achieve the goal in one move.In the second example the array already has two equal elements.In the third example applying the operation won't change the array at all, so it is impossible to make some pair of...
Input: 4 31 2 3 7 | Output: 1
Easy
1
491
221
99
10
1,068
C
1068C
C. Colored Rooks
1,700
constructive algorithms; graphs
Ivan is a novice painter. He has \(n\) dyes of different colors. He also knows exactly \(m\) pairs of colors which harmonize with each other.Ivan also enjoy playing chess. He has \(5000\) rooks. He wants to take \(k\) rooks, paint each of them in one of \(n\) colors and then place this \(k\) rooks on a chessboard of si...
The first line of input contains \(2\) integers \(n\), \(m\) (\(1 \le n \le 100\), \(0 \le m \le min(1000, \,\, \frac{n(n-1)}{2})\)) β€” number of colors and number of pairs of colors which harmonize with each other.In next \(m\) lines pairs of colors which harmonize with each other are listed. Colors are numbered from \...
Print \(n\) blocks, \(i\)-th of them describes rooks of \(i\)-th color.In the first line of block print one number \(a_{i}\) (\(1 \le a_{i} \le 5000\)) β€” number of rooks of color \(i\). In each of next \(a_{i}\) lines print two integers \(x\) and \(y\) (\(1 \le x, \,\, y \le 10^{9}\)) β€” coordinates of the next rook.All...
Rooks arrangements for all three examples (red is color \(1\), green is color \(2\) and blue is color \(3\)).
Input: 3 21 22 3 | Output: 23 41 441 22 22 45 415 1
Medium
2
1,060
391
443
10
729
D
729D
D. Sea Battle
1,700
constructive algorithms; greedy; math
Galya is playing one-dimensional Sea Battle on a 1 Γ— n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other.Galya doesn't know the ships location. She can shoot to some cells and after each shot sh...
The first line contains four positive integers n, a, b, k (1 ≀ n ≀ 2Β·105, 1 ≀ a, b ≀ n, 0 ≀ k ≀ n - 1) β€” the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made.The second line contains a string of length n, consisting of zeros and ones. If the i-t...
In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship.In the second line print the cells Galya should shoot at.Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from th...
There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the ""1"" character). So, it is necessary to make two shots: one at the left part, and one at the right part.
Input: 5 1 2 100100 | Output: 24 2
Medium
3
684
469
384
7
1,768
D
1768D
D. Lucky Permutation
1,800
constructive algorithms; dfs and similar; graphs; greedy
You are given a permutation\(^\dagger\) \(p\) of length \(n\).In one operation, you can choose two indices \(1 \le i < j \le n\) and swap \(p_i\) with \(p_j\).Find the minimum number of operations needed to have exactly one inversion\(^\ddagger\) in the permutation.\(^\dagger\) A permutation is an array consisting of \...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(p_1,p_2,\ldots, p_n\) (\(1 \...
For each test case output a single integer β€” the minimum number of operations needed to have exactly one inversion in the permutation. It can be proven that an answer always exists.
In the first test case, the permutation already satisfies the condition.In the second test case, you can perform the operation with \((i,j)=(1,2)\), after that the permutation will be \([2,1]\) which has exactly one inversion.In the third test case, it is not possible to satisfy the condition with less than \(3\) opera...
Input: 422 121 243 4 1 242 4 3 1 | Output: 0 1 3 1
Medium
4
744
474
181
17
1,718
A2
1718A2
A2. Burenka and Traditions (hard version)
1,900
data structures; dp; greedy
This is the hard version of this problem. The difference between easy and hard versions is only the constraints on \(a_i\) and on \(n\). You can make hacks only if both versions of the problem are solved.Burenka is the crown princess of Buryatia, and soon she will become the \(n\)-th queen of the country. There is an a...
The first line contains a single integer \(t\) \((1 \le t \le 500)\) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) \((1 \le n \le 10^5)\) - the size of the arrayThe second line of each test case contains \(n\) integers \(a_1, a_2, ...
For each test case, output a single number β€” the minimum time that Burenka will need.
In the first test case, Burenka can choose segment \(l = 1\), \(r = 4\), and \(x=5\). so it will fill the array with zeros in \(2\) seconds.In the second test case, Burenka first selects segment \(l = 1\), \(r = 2\), and \(x = 1\), after which \(a = [0, 2, 2]\), and then the segment \(l = 2\), \(r = 3\), and \(x=2\), w...
Input: 745 5 5 531 3 220 032 5 761 2 3 3 2 11027 27 34 32 2 31 23 56 52 451822 1799 57 23 55 | Output: 2 2 0 2 4 7 4
Hard
3
1,158
461
85
17
487
A
487A
A. Fight the Monster
1,800
binary search; brute force; implementation
A monster is attacking the Cyberland!Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (HP), offensive power (ATK) and defensive power (DEF).During the battle, every second the monster's HP decrease by max(0, ATKY - DEFM), while Yang's HP decreases by max(0, ATK...
The first line contains three integers HPY, ATKY, DEFY, separated by a space, denoting the initial HP, ATK and DEF of Master Yang.The second line contains three integers HPM, ATKM, DEFM, separated by a space, denoting the HP, ATK and DEF of the monster.The third line contains three integers h, a, d, separated by a spac...
The only output line should contain an integer, denoting the minimum bitcoins Master Yang should spend in order to win.
For the first sample, prices for ATK and DEF are extremely high. Master Yang can buy 99 HP, then he can beat the monster with 1 HP left.For the second sample, Master Yang is strong enough to beat the monster, so he doesn't need to buy anything.
Input: 1 2 11 100 11 100 100 | Output: 99
Medium
3
727
438
119
4
1,166
A
1166A
A. Silent Classroom
900
combinatorics; greedy
There are \(n\) students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they mus...
The first line contains a single integer \(n\) (\(1\leq n \leq 100\)) β€” the number of students.After this \(n\) lines follow.The \(i\)-th line contains the name of the \(i\)-th student.It is guaranteed each name is a string of lowercase English letters of length at most \(20\). Note that multiple students may share the...
The output must consist of a single integer \(x\) β€” the minimum possible number of chatty pairs.
In the first sample the minimum number of pairs is \(1\). This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.In the second sample the minimum number of pairs is \(2\). This can be achieved, for example, by putting kamb...
Input: 4 jorge jose oscar jerry | Output: 1
Beginner
2
1,254
331
96
11
1,006
A
1006A
A. Adjacent Replacements
800
implementation
Mishka got an integer array \(a\) of length \(n\) as a birthday present (what a surprise!).Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it ""Mishka's Adjacent Replacements Algorithm"". This algorithm can be represented as a sequence of steps: Replace each occu...
The first line of the input contains one integer number \(n\) (\(1 \le n \le 1000\)) β€” the number of elements in Mishka's birthday present (surprisingly, an array).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β€” the elements of the array.
Print \(n\) integers β€” \(b_1, b_2, \dots, b_n\), where \(b_i\) is the final value of the \(i\)-th element of the array after applying ""Mishka's Adjacent Replacements Algorithm"" to the array \(a\). Note that you cannot change the order of elements in the array.
The first example is described in the problem statement.
Input: 51 2 4 5 10 | Output: 1 1 3 5 9
Beginner
1
2,142
295
262
10
2,067
B
2067B
B. Two Large Bags
1,200
brute force; dp; greedy; sortings
You have two large bags of numbers. Initially, the first bag contains \(n\) numbers: \(a_1, a_2, \ldots, a_n\), while the second bag is empty. You are allowed to perform the following operations: Choose any number from the first bag and move it to the second bag. Choose a number from the first bag that is also present ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains an integer \(n\) (\(2 \le n \le 1000\)) β€” the length of the array \(a\). It is guaranteed that \(n\) is an even numb...
For each test case, print ""YES"" if it is possible to equalize the contents of the bags. Otherwise, output ""NO"".You can output each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"", ""yEs"" will be recognized as a positive answer).
Let's analyze the sixth test case: we will show the sequence of operations that leads to the equality of the bags. Initially, the first bag consists of the numbers \((3, 3, 4, 5, 3, 3)\), and the second bag is empty. In the first operation, move the number \(3\) from the first bag to the second. State: \((3, 4, 5, 3, 3...
Input: 921 122 141 1 4 443 4 3 342 3 4 463 3 4 5 3 362 2 2 4 4 481 1 1 1 1 1 1 4109 9 9 10 10 10 10 10 10 10 | Output: Yes No Yes Yes No Yes No Yes Yes
Easy
4
515
516
253
20
1,981
A
1981A
A. Turtle and Piggy Are Playing a Game
800
brute force; greedy; math
Turtle and Piggy are playing a number game.First, Turtle will choose an integer \(x\), such that \(l \le x \le r\), where \(l, r\) are given. It's also guaranteed that \(2l \le r\).Then, Piggy will keep doing the following operation until \(x\) becomes \(1\): Choose an integer \(p\) such that \(p \ge 2\) and \(p \mid x...
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 \(l, r\) (\(1 \le l \le r \le 10^9, 2l \le r\)) β€” The range where Turtle can choose the integer from.
For each test case, output a single integer β€” the maximum score.
In the first test case, Turtle can choose an integer \(x\), such that \(2 \le x \le 4\). He can choose \(x = 4\). Then Piggy can choose \(p = 2\) for \(2\) times. After that, \(x\) will become \(1\), and the score will be \(2\), which is maximized.In the second test case, Turtle can choose an integer \(3 \le x \le 6\)....
Input: 52 43 62 156 22114514 1919810 | Output: 2 2 3 4 20
Beginner
3
556
317
64
19
612
E
612E
E. Square Root of Permutation
2,200
combinatorics; constructive algorithms; dfs and similar; graphs; math
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5,...
The first line contains integer n (1 ≀ n ≀ 106) β€” the number of elements in permutation p.The second line contains n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) β€” the elements of permutation p.
If there is no permutation q such that q2 = p print the number ""-1"".If the answer exists print it. The only line should contain n different integers qi (1 ≀ qi ≀ n) β€” the elements of the permutation q. If there are several solutions print any of them.
Input: 42 1 4 3 | Output: 3 4 2 1
Hard
5
490
196
253
6
353
C
353C
C. Find Maximum
1,600
implementation; math; number theory
Valera has array a, consisting of n integers a0, a1, ..., an - 1, and function f(x), taking an integer from 0 to 2n - 1 as its single argument. Value f(x) is calculated by formula , where value bit(i) equals one if the binary representation of number x contains a 1 on the i-th position, and zero otherwise.For example, ...
The first line contains integer n (1 ≀ n ≀ 105) β€” the number of array elements. The next line contains n space-separated integers a0, a1, ..., an - 1 (0 ≀ ai ≀ 104) β€” elements of array a.The third line contains a sequence of digits zero and one without spaces s0s1... sn - 1 β€” the binary representation of number m. Numb...
Print a single integer β€” the maximum value of function f(x) for all .
In the first test case m = 20 = 1, f(0) = 0, f(1) = a0 = 3.In the second sample m = 20 + 21 + 23 = 11, the maximum value of function equals f(5) = a0 + a2 = 17 + 10 = 27.
Input: 23 810 | Output: 3
Medium
3
486
333
69
3
1,430
D
1430D
D. String Deletion
1,700
binary search; data structures; greedy; two pointers
You have a string \(s\) consisting of \(n\) characters. Each character is either 0 or 1.You can perform operations on the string. Each operation consists of two steps: select an integer \(i\) from \(1\) to the length of the string \(s\), then delete the character \(s_i\) (the string length gets reduced by \(1\), the in...
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the length of the string \(s\).The second line contains string \(s\) of \(n\) characters. Each character is either 0 or 1.It'...
For each test case, print a single integer β€” the maximum number of operations you can perform.
In the first test case, you can, for example, select \(i = 2\) and get string 010 after the first operation. After that, you can select \(i = 3\) and get string 1. Finally, you can only select \(i = 1\) and get empty string.
Input: 5 6 111010 1 0 1 1 2 11 6 101010 | Output: 3 1 1 1 3
Medium
4
1,392
409
94
14
825
C
825C
C. Multi-judge Solving
1,600
greedy; implementation
Makes solves problems on Decoforces and lots of other different online judges. Each problem is denoted by its difficulty β€” a positive integer number. Difficulties are measured the same across all the judges (the problem with difficulty d on Decoforces is as hard as the problem with difficulty d on any other judge). Mak...
The first line contains two integer numbers n, k (1 ≀ n ≀ 103, 1 ≀ k ≀ 109).The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109).
Print minimum number of problems Makes should solve on other judges in order to solve all chosen problems on Decoforces.
In the first example Makes at first solves problems 1 and 2. Then in order to solve the problem with difficulty 9, he should solve problem with difficulty no less than 5. The only available are difficulties 5 and 6 on some other judge. Solving any of these will give Makes opportunity to solve problem 3.In the second ex...
Input: 3 32 1 9 | Output: 1
Medium
2
1,360
166
120
8
336
E
336E
E. Vasily the Bear and Painting Square
2,700
bitmasks; combinatorics; dp; implementation
Vasily the bear has two favorite integers n and k and a pencil. Besides, he's got k jars with different water color paints. All jars are numbered in some manner from 1 to k, inclusive. The jar number i contains the paint of the i-th color. Initially the bear took a pencil and drew four segments on the coordinate plane....
The first line contains two integers n and k, separated by a space (0 ≀ n, k ≀ 200).
Print exactly one integer β€” the answer to the problem modulo 1000000007 (109 + 7).
Input: 0 0 | Output: 1
Master
4
1,861
84
82
3
1,217
A
1217A
A. Creating a Character
1,300
binary search; math
You play your favourite game yet another time. You chose the character you didn't play before. It has \(str\) points of strength and \(int\) points of intelligence. Also, at start, the character has \(exp\) free experience points you can invest either in strength or in intelligence (by investing one point you can eithe...
The first line contains the single integer \(T\) (\(1 \le T \le 100\)) β€” the number of queries. Next \(T\) lines contain descriptions of queries β€” one per line.This line contains three integers \(str\), \(int\) and \(exp\) (\(1 \le str, int \le 10^8\), \(0 \le exp \le 10^8\)) β€” the initial strength and intelligence of ...
Print \(T\) integers β€” one per query. For each query print the number of different character builds you can create.
In the first query there are only three appropriate character builds: \((str = 7, int = 5)\), \((8, 4)\) and \((9, 3)\). All other builds are either too smart or don't use all free points.In the second query there is only one possible build: \((2, 1)\).In the third query there are two appropriate builds: \((7, 6)\), \(...
Input: 4 5 3 4 2 1 0 3 5 5 4 10 6 | Output: 3 1 2 0
Easy
2
792
378
115
12
1,796
C
1796C
C. Maximum Set
1,600
binary search; math
A set of positive integers \(S\) is called beautiful if, for every two integers \(x\) and \(y\) from this set, either \(x\) divides \(y\) or \(y\) divides \(x\) (or both).You are given two integers \(l\) and \(r\). Consider all beautiful sets consisting of integers not less than \(l\) and not greater than \(r\). You ha...
The first line contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β€” the number of test cases.Each test case consists of one line containing two integers \(l\) and \(r\) (\(1 \le l \le r \le 10^6\)).
For each test case, print two integers β€” the maximum possible size of a beautiful set consisting of integers from \(l\) to \(r\), and the number of such sets with maximum possible size. Since the second number can be very large, print it modulo \(998244353\).
In the first test case, the maximum possible size of a beautiful set with integers from \(3\) to \(11\) is \(2\). There are \(4\) such sets which have the maximum possible size: \(\{ 3, 6 \}\); \(\{ 3, 9 \}\); \(\{ 4, 8 \}\); \(\{ 5, 10 \}\).
Input: 43 1113 371 224 100 | Output: 2 4 2 6 5 1 5 7
Medium
2
611
205
259
17
1,336
E1
1336E1
E1. Chiori and Doll Picking (easy version)
2,700
bitmasks; brute force; combinatorics; math
This is the easy version of the problem. The only difference between easy and hard versions is the constraint of \(m\). You can make hacks only if both versions are solved.Chiori loves dolls and now she is going to decorate her bedroom! As a doll collector, Chiori has got \(n\) dolls. The \(i\)-th doll has a non-negati...
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le m \le 35\)) β€” the number of dolls and the maximum value of the picking way.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i < 2^m\)) β€” the values of dolls.
Print \(m+1\) integers \(p_0, p_1, \ldots, p_m\) β€” \(p_i\) is equal to the number of picking ways with value \(i\) by modulo \(998\,244\,353\).
Input: 4 4 3 5 8 14 | Output: 2 2 6 6 0
Master
4
1,011
277
143
13
756
A
756A
A. Pavel and barbecue
1,700
constructive algorithms; dfs and similar
Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction.Pavel has a plan: a permutation p and a sequence b1, b2, .....
The first line contain the integer n (1 ≀ n ≀ 2Β·105) β€” the number of skewers.The second line contains a sequence of integers p1, p2, ..., pn (1 ≀ pi ≀ n) β€” the permutation, according to which Pavel wants to move the skewers.The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to wh...
Print single integer β€” the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements.
In the first example Pavel can change the permutation to 4, 3, 1, 2.In the second example Pavel can change any element of b to 1.
Input: 44 3 2 10 1 1 1 | Output: 2
Medium
2
1,219
359
185
7
1,864
H
1864H
H. Asterism Stream
3,200
dp; math; matrices
Bogocubic is playing a game with amenotiomoi. First, Bogocubic fixed an integer \(n\), and then he gave amenotiomoi an integer \(x\) which is initially equal to \(1\).In one move amenotiomoi performs one of the following operations with the same probability: increase \(x\) by \(1\); multiply \(x\) by \(2\). Bogocubic w...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows.The only line of each test case contains one integer \(n\) (\(1 \le n \le 10^{18}\)).
For each test case, output a single integer β€” the expected number of moves modulo \(998\,244\,353\).
In the first test case, \(n\le x\) without any operations, so the answer is \(0\).In the second test case, for \(n = 4\), here is the list of all possible sequences of operations and their probabilities: \(1\stackrel{+1}{\longrightarrow}2\stackrel{+1}{\longrightarrow}3\stackrel{+1}{\longrightarrow}4\), the probability ...
Input: 714815998244353296574916252563317494288321850420024 | Output: 0 499122179 717488133 900515847 93715054 44488799 520723508
Master
3
838
245
100
18
618
E
618E
E. Robot Arm
2,500
data structures; geometry
Roger is a robot. He has an arm that is a series of n segments connected to each other. The endpoints of the i-th segment are initially located at points (i - 1, 0) and (i, 0). The endpoint at (i - 1, 0) is colored red and the endpoint at (i, 0) is colored blue for all segments. Thus, the blue endpoint of the i-th segm...
The first line of the input will contain two integers n and m (1 ≀ n, m ≀ 300 000) β€” the number of segments and the number of operations to perform.Each of the next m lines contains three integers xi, yi and zi describing a move. If xi = 1, this line describes a move of type 1, where yi denotes the segment number and z...
Print m lines. The i-th line should contain two real values, denoting the coordinates of the blue endpoint of the last segment after applying operations 1, ..., i. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 4.Namely, let's assume that your answer for a particular value...
The following pictures shows the state of the arm after each operation. The coordinates of point F are printed after applying each operation. For simplicity, we only show the blue endpoints of a segment (with the exception for the red endpoint of the first segment). For instance, the point labeled B is the blue endpoin...
Input: 5 41 1 32 3 902 5 481 4 1 | Output: 8.0000000000 0.00000000005.0000000000 -3.00000000004.2568551745 -2.66913060644.2568551745 -3.6691306064
Expert
2
1,817
514
452
6
868
G
868G
G. El Toll Caves
3,300
math
The prehistoric caves of El Toll are located in MoiΓ  (Barcelona). You have heard that there is a treasure hidden in one of n possible spots in the caves. You assume that each of the spots has probability 1 / n to contain a treasure.You cannot get into the caves yourself, so you have constructed a robot that can search ...
The first line contains the number of test cases T (1 ≀ T ≀ 1000).Each of the next T lines contains two integers n and k (1 ≀ k ≀ n ≀ 5Β·108).
For each test case output the answer in a separate line.
In the first case the robot will repeatedly search in the only spot. The expected number of days in this case is 2. Note that in spite of the fact that we know the treasure spot from the start, the robot still has to search there until he succesfully recovers the treasure.In the second case the answer can be shown to b...
Input: 31 12 13 2 | Output: 2500000007777777786
Master
1
1,295
141
56
8
1,406
B
1406B
B. Maximum Product
1,200
brute force; dp; greedy; implementation; sortings
You are given an array of integers \(a_1,a_2,\ldots,a_n\). Find the maximum possible value of \(a_ia_ja_ka_la_t\) among all five indices \((i, j, k, l, t)\) (\(i<j<k<l<t\)).
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 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\) (\(5\le n\le 10^5\)) β€” the size of the array.The second line of each tes...
For each test case, print one integer β€” the answer to the problem.
In the first test case, choosing \(a_1,a_2,a_3,a_4,a_5\) is a best choice: \((-1)\cdot (-2) \cdot (-3)\cdot (-4)\cdot (-5)=-120\).In the second test case, choosing \(a_1,a_2,a_3,a_5,a_6\) is a best choice: \((-1)\cdot (-2) \cdot (-3)\cdot 2\cdot (-1)=12\).In the third test case, choosing \(a_1,a_2,a_3,a_4,a_5\) is a be...
Input: 4 5 -1 -2 -3 -4 -5 6 -1 -2 -3 1 2 -1 6 -1 0 0 0 -1 -1 6 -9 -7 -5 -3 -2 1 | Output: -120 12 0 945
Easy
5
173
521
66
14
470
A
470A
A. Crystal Ball Sequence
1,400
*special; implementation
Crystal ball sequence on hexagonal lattice is defined as follows: n-th element is the number of lattice points inside a hexagon with (n + 1) points on each side. The formula is Hn = 3Β·nΒ·(n + 1) + 1. You are given n; calculate n-th element of the sequence.
The only line of input contains an integer n (0 ≀ n ≀ 9).
Output the n-th element of crystal ball sequence.
Input: 1 | Output: 7
Easy
2
255
57
49
4
1,562
E
1562E
E. Rescue Niwen!
2,500
dp; greedy; string suffix structures; strings
Morning desert sun horizonRise above the sands of time...Fates Warning, ""Exodus""After crossing the Windswept Wastes, Ori has finally reached the Windtorn Ruins to find the Heart of the Forest! However, the ancient repository containing this priceless Willow light did not want to open!Ori was taken aback, but the Voic...
Each test contains multiple test cases.The first line contains one positive integer \(t\) (\(1 \le t \le 10^3\)), denoting the number of test cases. Description of the test cases follows.The first line of each test case contains one positive integer \(n\) (\(1 \le n \le 5000\)) β€” length of the string.The second line of...
For every test case print one non-negative integer β€” the answer to the problem.
In first test case the ""expansion"" of the string is: 'a', 'ac', 'acb', 'acba', 'acbac', 'c', 'cb', 'cba', 'cbac', 'b', 'ba', 'bac', 'a', 'ac', 'c'. The answer can be, for example, 'a', 'ac', 'acb', 'acba', 'acbac', 'b', 'ba', 'bac', 'c'.
Input: 7 5 acbac 8 acabacba 12 aaaaaaaaaaaa 10 abacabadac 8 dcbaabcd 3 cba 6 sparky | Output: 9 17 12 29 14 3 9
Expert
4
1,478
507
79
15
1,245
B
1245B
B. Restricted RPS
1,200
constructive algorithms; dp; greedy
Let \(n\) be a positive integer. Let \(a, b, c\) be nonnegative integers such that \(a + b + c = n\).Alice and Bob are gonna play rock-paper-scissors \(n\) times. Alice knows the sequences of hands that Bob will play. However, Alice has to play rock \(a\) times, paper \(b\) times, and scissors \(c\) times.Alice wins if...
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases.Then, \(t\) testcases follow, each consisting of three lines: The first line contains a single integer \(n\) (\(1 \le n \le 100\)). The second line contains three integers, \(a, b, c\) (\(0 \le a, b, c \le n\)). It is guaran...
For each testcase: If Alice cannot win, print ""NO"" (without the quotes). Otherwise, print ""YES"" (without the quotes). Also, print a string \(t\) of length \(n\) made up of only 'R', 'P', and 'S' β€” a sequence of hands that Alice can use to win. \(t\) must contain exactly \(a\) 'R's, \(b\) 'P's, and \(c\) 'S's. If th...
In the first testcase, in the first hand, Alice plays paper and Bob plays rock, so Alice beats Bob. In the second hand, Alice plays scissors and Bob plays paper, so Alice beats Bob. In the third hand, Alice plays rock and Bob plays scissors, so Alice beats Bob. Alice beat Bob 3 times, and \(3 \ge \lceil \frac{3}{2} \rc...
Input: 2 3 1 1 1 RPS 3 3 0 0 RPS | Output: YES PSR NO
Easy
3
804
549
528
12
1,648
E
1648E
E. Air Reform
3,200
data structures; dfs and similar; divide and conquer; dsu; graphs; implementation; trees
Berland is a large country with developed airlines. In total, there are \(n\) cities in the country that are historically served by the Berlaflot airline. The airline operates bi-directional flights between \(m\) pairs of cities, \(i\)-th of them connects cities with numbers \(a_i\) and \(b_i\) and has a price \(c_i\) ...
Each test consists of multiple test cases. The first line contains one integer \(t\) (\(1 \le t \le 10\,000\)) β€” the amount of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(4 \le n \le 200\,000\), \(n - 1 \le m \le 200\,000\), \(m \le \frac{(n - 1) (n - 2)}{2}\)) β€” the amount of c...
For each test case you should print \(m\) integers in a single line, \(i\)-th of them should be the price of \(i\)-th Berlaflot flight after the air reform.
In the first test case S8 Airlines will provide flights between these pairs of cities: \((1, 3)\), \((1, 4)\) and \((2, 4)\).The cost of a flight between cities \(1\) and \(3\) will be equal to \(2\), since the minimum cost of the Berlaflot route is \(2\) β€” the route consists of a flight between cities \(1\) and \(2\) ...
Input: 34 31 2 12 3 24 3 35 51 2 11 3 12 4 14 5 25 1 36 61 2 32 3 13 6 53 4 24 5 42 4 2 | Output: 3 3 3 1 1 1 2 2 4 4 5 3 4 4
Master
7
2,238
1,113
156
16
1,282
A
1282A
A. Temporarily unavailable
900
implementation; math
Polycarp lives on the coordinate axis \(Ox\) and travels from the point \(x=a\) to \(x=b\). It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis \(Ox\) at the point \(x=c\) the base station of the mobile operator is placed. It is known that the radius of its coverage is \(r\). Thus...
The first line contains a positive integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. In the following lines are written \(t\) test cases.The description of each test case is one line, which contains four integers \(a\), \(b\), \(c\) and \(r\) (\(-10^8 \le a,b,c \le 10^8\), \(0 \le r \le 10^8\)) β€” the coo...
Print \(t\) numbers β€” answers to given test cases in the order they are written in the test. Each answer is an integer β€” the number of minutes during which Polycarp will be unavailable during his movement.
The following picture illustrates the first test case. Polycarp goes from \(1\) to \(10\). The yellow area shows the coverage area of the station with a radius of coverage of \(1\), which is located at the point of \(7\). The green area shows a part of the path when Polycarp is out of coverage area.
Input: 9 1 10 7 1 3 3 3 0 8 2 10 4 8 2 10 100 -10 20 -17 2 -3 2 2 0 -3 1 2 0 2 3 2 3 -1 3 -2 2 | Output: 7 0 4 0 30 5 4 0 3
Beginner
2
739
604
205
12
177
G1
177G1
G1. Fibonacci Strings
2,400
strings
Fibonacci strings are defined as follows: f1 = Β«aΒ» f2 = Β«bΒ» fn = fn - 1 fn - 2, n > 2 Thus, the first five Fibonacci strings are: ""a"", ""b"", ""ba"", ""bab"", ""babba"".You are given a Fibonacci string and m strings si. For each string si, find the number of times it occurs in the given Fibonacci string as a substrin...
The first line contains two space-separated integers k and m β€” the number of a Fibonacci string and the number of queries, correspondingly.Next m lines contain strings si that correspond to the queries. It is guaranteed that strings si aren't empty and consist only of characters ""a"" and ""b"".The input limitations fo...
For each string si print the number of times it occurs in the given Fibonacci string as a substring. Since the numbers can be large enough, print them modulo 1000000007 (109 + 7). Print the answers for the strings in the order in which they are given in the input.
Input: 6 5ababbaaba | Output: 35331
Expert
1
322
689
264
1
1,667
D
1667D
D. Edge Elimination
2,900
constructive algorithms; dfs and similar; dp; trees
You are given a tree (connected, undirected, acyclic graph) with \(n\) vertices. Two edges are adjacent if they share exactly one endpoint. In one move you can remove an arbitrary edge, if that edge is adjacent to an even number of remaining edges.Remove all of the edges, or determine that it is impossible. If there ar...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β€” the number of vertices in the tree.Then...
For each test case print ""NO"" if it is impossible to remove all the edges.Otherwise print ""YES"", and in the next \(n-1\) lines print a possible order of the removed edges. For each edge, print its endpoints in any order.
Test case \(1\): it is possible to remove the edge, because it is not adjacent to any other edge.Test case \(2\): both edges are adjacent to exactly one edge, so it is impossible to remove any of them. So the answer is ""NO"".Test case \(3\): the edge \(2-3\) is adjacent to two other edges. So it is possible to remove ...
Input: 5 2 1 2 3 1 2 2 3 4 1 2 2 3 3 4 5 1 2 2 3 3 4 3 5 7 1 2 1 3 2 4 2 5 3 6 3 7 | Output: YES 2 1 NO YES 2 3 3 4 2 1 YES 3 5 2 3 2 1 4 3 NO
Master
4
352
605
224
16
1,717
A
1717A
A. Madoka and Strange Thoughts
800
math; number theory
Madoka is a very strange girl, and therefore she suddenly wondered how many pairs of integers \((a, b)\) exist, where \(1 \leq a, b \leq n\), for which \(\frac{\operatorname{lcm}(a, b)}{\operatorname{gcd}(a, b)} \leq 3\).In this problem, \(\operatorname{gcd}(a, b)\) denotes the greatest common divisor of the numbers \(...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Description of the test cases follows.The first and the only line of each test case contains the integer \(n\) (\(1 \le n \le 10^8\)).
For each test case output a single integer β€” the number of pairs of integers satisfying the condition.
For \(n = 1\) there is exactly one pair of numbers β€” \((1, 1)\) and it fits.For \(n = 2\), there are only \(4\) pairs β€” \((1, 1)\), \((1, 2)\), \((2, 1)\), \((2, 2)\) and they all fit.For \(n = 3\), all \(9\) pair are suitable, except \((2, 3)\) and \((3, 2)\), since their \(\operatorname{lcm}\) is \(6\), and \(\operat...
Input: 612345100000000 | Output: 1 4 7 10 11 266666666
Beginner
2
436
275
102
17
1,700
C
1700C
C. Helping the Nature
1,700
constructive algorithms; data structures; greedy
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees.There are \(n\) trees growing near the path, the current levels of moisture of each...
The first line contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β€” the number of test cases. The description of \(t\) test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 200\,000\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots...
For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists.
In the first test case it's enough to apply the operation of adding \(1\) to the whole array \(2\) times. In the second test case you can apply the operation of decreasing \(4\) times on the prefix of length \(3\) and get an array \(6, 0, 3\). After that apply the operation of decreasing \(6\) times on the prefix of le...
Input: 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 | Output: 2 13 36 33
Medium
3
835
486
115
17
1,238
C
1238C
C. Standard Free2play
1,600
dp; greedy; math
You are playing a game where your character should overcome different obstacles. The current problem is to come down from a cliff. The cliff has height \(h\), and there is a moving platform on each height \(x\) from \(1\) to \(h\).Each platform is either hidden inside the cliff or moved out. At first, there are \(n\) m...
The first line contains one integer \(q\) (\(1 \le q \le 100\)) β€” the number of queries. Each query contains two lines and is independent of all other queries.The first line of each query contains two integers \(h\) and \(n\) (\(1 \le h \le 10^9\), \(1 \le n \le \min(h, 2 \cdot 10^5)\)) β€” the height of the cliff and th...
For each query print one integer β€” the minimum number of magic crystals you have to spend to safely come down on the ground level (with height \(0\)).
Input: 4 3 2 3 1 8 6 8 7 6 5 3 2 9 6 9 8 5 4 3 1 1 1 1 | Output: 0 1 2 0
Medium
3
1,610
605
150
12
526
E
526E
E. Transmitting Levels
2,400
dp; implementation
Optimizing the amount of data transmitted via a network is an important and interesting part of developing any network application. In one secret game developed deep in the ZeptoLab company, the game universe consists of n levels, located in a circle. You can get from level i to levels i - 1 and i + 1, also you can get...
The first line contains two integers n, q (2 ≀ n ≀ 106, 1 ≀ q ≀ 50) β€” the number of levels in the game universe and the number of distinct values of b that you need to process.The second line contains n integers ai (1 ≀ ai ≀ 109) β€” the sizes of the levels in bytes.The next q lines contain integers bj (), determining th...
For each value of kj from the input print on a single line integer mj (1 ≀ mj ≀ n), determining the minimum number of groups to divide game levels into for transmission via network observing the given conditions.
In the test from the statement you can do in the following manner. at b = 7 you can divide into two segments: 2|421|32 (note that one of the segments contains the fifth, sixth and first levels); at b = 4 you can divide into four segments: 2|4|21|3|2; at b = 6 you can divide into three segments: 24|21|32|.
Input: 6 32 4 2 1 3 2746 | Output: 243
Expert
2
1,847
387
212
5
176
D
176D
D. Hyper String
2,500
dp
Paul ErdΕ‘s's prediction came true. Finally an alien force landed on the Earth. In contrary to our expectation they didn't asked the humans to compute the value of a Ramsey number (maybe they had solved it themselves). They asked another question which seemed as hard as calculating Ramsey numbers. Aliens threatened that...
The first line of input contains the single integer n (1 ≀ n ≀ 2000) β€” the number of base strings. The next n lines contains values of base strings. Each base string is made of lowercase Latin letters. A base string cannot be empty string and the sum of lengths of all n base strings doesn't exceed 106. The next line co...
Print the length of longest common sub-sequence of Hyper String t and string s. If there is no common sub-sequence print 0.
The length of string s is the number of characters in it. If the length of string s is marked as |s|, then string s can be represented as s = s1s2... s|s|.A non-empty string y = s[p1p2... p|y|] = sp1sp2... sp|y| (1 ≀ p1 < p2 < ... < p|y| ≀ |s|) is a subsequence of string s. For example, ""coders"" is a subsequence of "...
Input: 2cbadgh21 2aedfhr | Output: 3
Expert
1
923
695
123
1
930
E
930E
E. Coins Exhibition
2,900
data structures; dp; math
Arkady and Kirill visited an exhibition of rare coins. The coins were located in a row and enumerated from left to right from 1 to k, each coin either was laid with its obverse (front) side up, or with its reverse (back) side up.Arkady and Kirill made some photos of the coins, each photo contained a segment of neighbor...
The first line contains three integers k, n and m (1 ≀ k ≀ 109, 0 ≀ n, m ≀ 105) β€” the total number of coins, the number of photos made by Arkady, and the number of photos made by Kirill, respectively.The next n lines contain the descriptions of Arkady's photos, one per line. Each of these lines contains two integers l ...
Print the only line β€” the number of ways to choose the side for each coin modulo 109 + 7 = 1000000007.
In the first example the following ways are possible ('O' β€” obverse, 'R' β€” reverse side): OROOR, ORORO, ORORR, RROOR, RRORO, RRORR, ORROR, ORRRO. In the second example the information is contradictory: the second coin should have obverse and reverse sides up at the same time, that is impossible. So, the answer is 0.
Input: 5 2 21 33 52 24 5 | Output: 8
Master
3
991
688
102
9
1,400
D
1400D
D. Zigzags
1,900
brute force; combinatorics; data structures; math; two pointers
You are given an array \(a_1, a_2 \dots a_n\). Calculate the number of tuples \((i, j, k, l)\) such that: \(1 \le i < j < k < l \le n\); \(a_i = a_k\) and \(a_j = a_l\);
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(4 \le n \le 3000\)) β€” the size of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) β€”...
For each test case, print the number of described tuples.
In the first test case, for any four indices \(i < j < k < l\) are valid, so the answer is the number of tuples.In the second test case, there are \(2\) valid tuples: \((1, 2, 4, 6)\): \(a_1 = a_4\) and \(a_2 = a_6\); \((1, 3, 4, 6)\): \(a_1 = a_4\) and \(a_3 = a_6\).
Input: 2 5 2 2 2 2 2 6 1 3 3 1 2 3 | Output: 5 2
Hard
5
169
411
57
14
1,553
C
1553C
C. Penalty
1,200
bitmasks; brute force; dp; greedy
Consider a simplified penalty phase at the end of a football match.A penalty phase consists of at most \(10\) kicks, the first team takes the first kick, the second team takes the second kick, then the first team takes the third kick, and so on. The team that scores more goals wins; if both teams score the same number ...
The first line contains one integer \(t\) (\(1 \le t \le 1\,000\)) β€” the number of test cases.Each test case is represented by one line containing the string \(s\), consisting of exactly \(10\) characters. Each character is either 1, 0, or ?.
For each test case, print one integer β€” the minimum possible number of kicks in the penalty phase.
Consider the example test:In the first test case, consider the situation when the \(1\)-st, \(5\)-th and \(7\)-th kicks score goals, and kicks \(2\), \(3\), \(4\) and \(6\) are unsuccessful. Then the current number of goals for the first team is \(3\), for the second team is \(0\), and the referee sees that the second ...
Input: 4 1?0???1001 1111111111 ?????????? 0100000000 | Output: 7 10 6 9
Easy
4
1,634
242
98
15
689
D
689D
D. Friends and Subsequences
2,100
binary search; data structures
Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you) β€” who knows? Every one of them has an integer sequences a and b of length n. Being given a query of the form of pair of integers (l, r),...
The first line contains only integer n (1 ≀ n ≀ 200 000).The second line contains n integer numbers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the sequence a.The third line contains n integer numbers b1, b2, ..., bn ( - 109 ≀ bi ≀ 109) β€” the sequence b.
Print the only integer number β€” the number of occasions the robot will count, thus for how many pairs is satisfied.
The occasions in the first sample case are:1.l = 4,r = 4 since max{2} = min{2}.2.l = 4,r = 5 since max{2, 1} = min{2, 3}.There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1.
Input: 61 2 3 2 1 46 7 1 2 3 2 | Output: 2
Hard
2
689
248
115
6
2,094
E
2094E
E. Boneca Ambalabu
1,200
bitmasks
Boneca Ambalabu gives you a sequence of \(n\) integers \(a_1,a_2,\ldots,a_n\).Output the maximum value of \((a_k\oplus a_1)+(a_k\oplus a_2)+\ldots+(a_k\oplus a_n)\) among all \(1 \leq k \leq n\). Note that \(\oplus\) denotes the bitwise XOR operation.
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) – the number of independent test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n\leq 2\cdot 10^5\)) – the length of the array.The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(0 \leq a_i < 2^...
For each test case, output the maximum value on a new line.
In the first test case, the best we can do is \((18\oplus18)+(18\oplus18)+(18\oplus18)=0\).In the second test case, we choose \(k=5\) to get \((16\oplus1)+(16\oplus2)+(16\oplus4)+(16\oplus8)+(16\oplus16)=79\).
Input: 5318 18 1851 2 4 8 1658 13 4 5 156625 676 729 784 841 90011 | Output: 0 79 37 1555 0
Easy
1
251
419
59
20
2,049
A
2049A
A. MEX Destruction
800
greedy; implementation
Evirir the dragon snuck into a wizard's castle and found a mysterious contraption, and their playful instincts caused them to play with (destroy) it...Evirir the dragon found an array \(a_1, a_2, \ldots, a_n\) of \(n\) non-negative integers.In one operation, they can choose a non-empty subarray\(^{\text{βˆ—}}\) \(b\) of ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 200\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 50\)), the length of \(a\).The second line of each test case contains \(n\) spa...
For each test case, output a single integer on a line, the minimum number of operations needed to make \(a\) contain only zeros.
In the first test case, Evirir can choose the subarray \(b = [1, 2, 3]\) and replace it with \(\operatorname{mex}(1, 2, 3) = 0\), changing \(a\) from \([0, \underline{1, 2, 3}]\) to \([0, 0]\) (where the chosen subarray is underlined). Therefore, the answer is \(1\).In the second test case, \(a\) already contains only ...
Input: 1040 1 2 360 0 0 0 0 051 0 1 0 153 1 4 1 543 2 1 079 100 0 89 12 2 340 3 9 070 7 0 2 0 7 01020 1 | Output: 1 0 2 1 1 2 1 2 0 1
Beginner
2
1,041
476
128
20
90
A
90A
A. Cableway
1,000
greedy; math
A group of university students wants to get to the top of a mountain to have a picnic there. For that they decided to use a cableway.A cableway is represented by some cablecars, hanged onto some cable stations by a cable. A cable is scrolled cyclically between the first and the last cable stations (the first of them is...
The first line contains three integers r, g and b (0 ≀ r, g, b ≀ 100). It is guaranteed that r + g + b > 0, it means that the group consists of at least one student.
Print a single number β€” the minimal time the students need for the whole group to ascend to the top of the mountain.
Let's analyze the first sample.At the moment of time 0 a red cablecar comes and one student from the r group get on it and ascends to the top at the moment of time 30.At the moment of time 1 a green cablecar arrives and two students from the g group get on it; they get to the top at the moment of time 31.At the moment ...
Input: 1 3 2 | Output: 34
Beginner
2
1,305
165
116
0
1,979
F
1979F
F. Kostyanych's Theorem
2,900
brute force; constructive algorithms; graphs; interactive
This is an interactive problem.Kostyanych has chosen a complete undirected graph\(^{\dagger}\) with \(n\) vertices, and then removed exactly \((n - 2)\) edges from it. You can ask queries of the following type: ""? \(d\)"" β€” Kostyanych tells you the number of vertex \(v\) with a degree at least \(d\). Among all possibl...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. The description of the test cases follows.The only line of each test case contains a single integer \(n\) (\(2 \le n \le 10^5\)) β€” the number of vertices in the graph.It is guaran...
In the first test case, the original graph looks as follows: Consider the queries: There are no vertices with a degree of at least \(3\) in the graph, so ""\(0\ 0\)"" is reported. There are four vertices with a degree of at least \(2\), and all of them have a degree of exactly \(2\): \(1\), \(2\), \(3\), \(4\). Vertex ...
Input: 3 4 0 0 1 4 2 3 4 1 0 4 2 2 1 0 | Output: ? 3 ? 2 ? 1 ! 4 3 1 2 ? 3 ? 0 ! 4 1 2 3 ? 0 ! 2 1
Master
4
1,330
392
0
19
1,266
H
1266H
H. Red-Blue Graph
3,400
dp; graphs; math; matrices; meet-in-the-middle
There is a directed graph on \(n\) vertices numbered \(1\) through \(n\) where each vertex (except \(n\)) has two outgoing arcs, red and blue. At any point in time, exactly one of the arcs is active for each vertex. Initially, all blue arcs are active and there is a token located at vertex \(1\). In one second, the ver...
The first line contains a single integer \(n\) (\(2 \leq n \leq 58\)) β€” the number of vertices.\(n-1\) lines follow, \(i\)-th of contains two space separated integers \(b_i\) and \(r_i\) (\(1 \leq b_i, r_i \leq n\)) representing a blue arc \((i, b_i)\) and red arc \((i, r_i)\), respectively. It is guaranteed that verte...
Output \(q\) lines, each containing answer to a single query.If the state in the \(i\)-th query is unreachable, output the integer \(-1\). Otherwise, output \(t_i\) β€” the first time when the state appears (measured in seconds, starting from the initial state of the graph which appears in time \(0\)).
The graph in the first example is depticed in the figure below.The first \(19\) queries denote the journey of the token. On the \(19\)-th move the token would reach the vertex \(6\). The last two queries show states that are unreachable.
Input: 6 2 1 5 5 2 1 6 3 4 3 21 1 BBBBB 1 RBBBB 2 BBBBB 5 BRBBB 3 BRBBR 1 BRRBR 1 RRRBR 2 BRRBR 5 BBRBR 4 BBRBB 3 BBRRB 2 BBBRB 5 BRBRB 3 BRBRR 1 BRRRR 1 RRRRR 2 BRRRR 5 BBRRR 4 BBRRB 2 BRBBB 4 BRBBR | Output: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 -1 -1
Master
5
1,298
744
301
12
1,626
C
1626C
C. Monsters And Spells
1,700
binary search; data structures; dp; greedy; implementation; math; two pointers
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters.The level he's currently on contains \(n\) monsters. The \(i\)-th of them appears \(k_i\) seconds after the start of the level and has \(h_i\) health points. As an additi...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of testcases.The first line of the testcase contains a single integer \(n\) (\(1 \le n \le 100\)) β€” the number of monsters in the level.The second line of the testcase contains \(n\) integers \(k_1 < k_2 < \dots < k_n\) (\(1 \le k_i \le ...
For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters.
In the first testcase of the example, Monocarp can cast spells \(3, 4, 5\) and \(6\) seconds from the start with damages \(1, 2, 3\) and \(4\), respectively. The damage dealt at \(6\) seconds is \(4\), which is indeed greater than or equal to the health of the monster that appears.In the second testcase of the example,...
Input: 316424 52 235 7 92 1 2 | Output: 10 6 7
Medium
7
1,383
681
112
16
1,528
C
1528C
C. Trees of Tranquillity
2,300
data structures; dfs and similar; greedy; trees
Soroush and Keshi each have a labeled and rooted tree on \(n\) vertices. Both of their trees are rooted from vertex \(1\).Soroush and Keshi used to be at war. After endless decades of fighting, they finally became allies to prepare a Codeforces round. To celebrate this fortunate event, they decided to make a memorial g...
The first line contains an integer \(t\) \((1\le t\le 3 \cdot 10^5)\) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains an integer \(n\) \((2\le n\le 3 \cdot 10^5)\).The second line of each test case contains \(n-1\) integers \(a_2, \ldots, a_n\) \((1 \le a_...
For each test case print a single integer β€” the size of the maximum clique in the memorial graph.
In the first and third test cases, you can pick any vertex.In the second test case, one of the maximum cliques is \(\{2, 3, 4, 5\}\).In the fourth test case, one of the maximum cliques is \(\{3, 4, 6\}\).
Input: 4 4 1 2 3 1 2 3 5 1 2 3 4 1 1 1 1 6 1 1 1 1 2 1 2 1 2 2 7 1 1 3 4 4 5 1 2 1 4 2 5 | Output: 1 4 1 3
Expert
4
1,043
695
97
15
1,554
E
1554E
E. You
2,600
dfs and similar; dp; math; number theory
You are given a tree with \(n\) nodes. As a reminder, a tree is a connected undirected graph without cycles.Let \(a_1, a_2, \ldots, a_n\) be a sequence of integers. Perform the following operation exactly \(n\) times: Select an unerased node \(u\). Assign \(a_u :=\) number of unerased nodes adjacent to \(u\). Then, era...
The first line contains a single integer \(t\) (\(1 \le t \le 10\,000\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 10^5\)).Each of the next \(n - 1\) lines contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\)) indicating there is an edge between ve...
For each test case, print \(n\) integers in a single line, where for each \(k\) from \(1\) to \(n\), the \(k\)-th integer denotes the answer when \(\operatorname{gcd}\) equals to \(k\).
In the first test case, If we delete the nodes in order \(1 \rightarrow 2 \rightarrow 3\) or \(1 \rightarrow 3 \rightarrow 2\), then the obtained sequence will be \(a = [2, 0, 0]\) which has \(\operatorname{gcd}\) equals to \(2\). If we delete the nodes in order \(2 \rightarrow 1 \rightarrow 3\), then the obtained sequ...
Input: 2 3 2 1 1 3 2 1 2 | Output: 3 1 0 2 0
Expert
4
812
485
185
15
679
B
679B
B. Bear and Tower of Cubes
2,200
binary search; dp; greedy
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length.A block with side a has volume a3. A tower consisting of blocks with sides a1, a2, ..., ak has the total volume a13 + a23 + ... + ak3.Li...
The only line of the input contains one integer m (1 ≀ m ≀ 1015), meaning that Limak wants you to choose X between 1 and m, inclusive.
Print two integers β€” the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks.
In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42.In more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The rem...
Input: 48 | Output: 9 42
Hard
3
898
134
146
6
48
B
48B
B. Land Lot
1,200
brute force; implementation
Vasya has a beautiful garden where wonderful fruit trees grow and yield fantastic harvest every year. But lately thieves started to sneak into the garden at nights and steal the fruit too often. Vasya can’t spend the nights in the garden and guard the fruit because there’s no house in the garden! Vasya had been saving ...
The first line contains two integers n and m (1 ≀ n, m ≀ 50) which represent the garden location. The next n lines contain m numbers 0 or 1, which describe the garden on the scheme. The zero means that a tree doesn’t grow on this square and the 1 means that there is a growing tree. The last line contains two integers a...
Print the minimum number of trees that needs to be chopped off to select a land lot a Γ— b in size to build a house on. It is guaranteed that at least one lot location can always be found, i. e. either a ≀ n and b ≀ m, or a ≀ m ΠΈ b ≀ n.
In the second example the upper left square is (1,1) and the lower right is (3,2).
Input: 2 21 01 11 1 | Output: 0
Easy
2
1,225
598
235
0
2,046
C
2046C
C. Adventurers
2,100
binary search; data structures; greedy; sortings; ternary search; two pointers
Once, four Roman merchants met in a Roman mansion to discuss their trading plans. They faced the following problem: they traded the same type of goods, and if they traded in the same city, they would inevitably incur losses. They decided to divide up the cities between them where they would trade.The map of Rome can be...
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 a single integer \(n\) (\(4 \le n \le 10^5\)) β€” the number of cities on the map.Each of the next \(n\) lines contain...
For each test case, in the first line, print a single integer \(k\) (\(0 \le k \le \frac{n}{4}\)) β€” the maximum possible number of cities that each merchant can get at a minimum.In the second line, print two integers \(x_0\) and \(y_0\) (\(|x_0|, |y_0| \le 10^9\)) β€” the coordinates of the dividing point. If there are m...
Input: 441 11 22 12 240 00 00 00 081 22 12 -11 -2-1 -2-2 -1-2 1-1 271 11 21 31 42 13 14 1 | Output: 1 2 2 0 0 0 2 1 0 0 0 0
Hard
6
1,009
642
363
20
750
C
750C
C. New Year and Rating
1,600
binary search; greedy; math
Every Codeforces user has rating, described with one integer, possibly negative or zero. Users are divided into two divisions. The first division is for users with rating 1900 or higher. Those with rating 1899 or lower belong to the second division. In every contest, according to one's performance, his or her rating ch...
The first line of the input contains a single integer n (1 ≀ n ≀ 200 000).The i-th of next n lines contains two integers ci and di ( - 100 ≀ ci ≀ 100, 1 ≀ di ≀ 2), describing Limak's rating change after the i-th contest and his division during the i-th contest contest.
If Limak's current rating can be arbitrarily big, print ""Infinity"" (without quotes). If the situation is impossible, print ""Impossible"" (without quotes). Otherwise print one integer, denoting the maximum possible value of Limak's current rating, i.e. rating after the n contests.
In the first sample, the following scenario matches all information Limak remembers and has maximum possible final rating: Limak has rating 1901 and belongs to the division 1 in the first contest. His rating decreases by 7. With rating 1894 Limak is in the division 2. His rating increases by 5. Limak has rating 1899 an...
Input: 3-7 15 28 2 | Output: 1907
Medium
3
878
269
283
7
2,027
E2
2027E2
E2. Bit Game (Hard Version)
3,100
bitmasks; dp; math
This is the hard version of this problem. The only difference is that you need to output the number of choices of games where Bob wins in this version, where the number of stones in each pile are not fixed. You must solve both versions to be able to hack.Alice and Bob are playing a familiar game where they take turns r...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows.The first line of each test case contains \(n\) (\(1 \le n \le 10^4\)) β€” the number of piles.The second line of each test case contains \(n\) integers \(a_1, a...
Output a single integer, the number of games where Bob wins, modulo \(10^9 + 7\).
In the first test case, no matter which values of \(x_2\) and \(x_3\) we choose, the second and third piles will always be chosen exactly once before no more stones can be taken from them. If \(x_1 = 2\), then no stones can be taken from it, so Bob will make the last move. If \(x_1 = 1\) or \(x_1 = 3\), then exactly on...
Input: 731 2 33 2 21134555 4 7 8 64 4 5 5 546 4 8 812 13 14 12392856133 46637598 1234567829384774 73775896 87654321265 12110 314677810235 275091182 428565855 72062973174522416 889934149 3394714 230851724 | Output: 4 4 0 6552 722019507 541 665443265
Master
3
1,442
555
81
20
286
D
286D
D. Tourists
2,600
data structures; sortings
A double tourist path, located at a park in Ultima Thule, is working by the following principle: We introduce the Cartesian coordinate system. At some points of time there are two tourists going (for a walk) from points ( - 1, 0) and (1, 0) simultaneously. The first one is walking from ( - 1, 0), the second one is walk...
The first line contains two space-separated integers n and m (1 ≀ n, m ≀ 105) β€” the number of pairs of tourists and the number of built walls. The next m lines contain three space-separated integers li, ri and ti each (0 ≀ li < ri ≀ 109, 0 ≀ ti ≀ 109) β€” the wall ends and the time it appeared. The last line contains n d...
For each pair of tourists print on a single line a single integer β€” the time in seconds when the two tourists from the corresponding pair won't see each other. Print the numbers in the order in which the they go in the input.
Input: 2 21 4 33 6 50 1 | Output: 24
Expert
2
1,186
493
225
2
1,628
D2
1628D2
D2. Game on Sum (Hard Version)
2,400
combinatorics; dp; games; math
This is the hard version of the problem. The difference is the constraints on \(n\), \(m\) and \(t\). You can make hacks only if all versions of the problem are solved.Alice and Bob are given the numbers \(n\), \(m\) and \(k\), and play a game as follows:The game has a score that Alice tries to maximize, and Bob tries ...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^5\)) β€” the number of test cases. The description of test cases follows.Each test case consists of a single line containing the three integers, \(n\), \(m\), and \(k\) (\(1 \le m \le n \le 10^6, 0 \le k < 10^9 + 7\)) β€” the number of turns, how...
For each test case output a single integer number β€” the score of the optimal game modulo \(10^9 + 7\).Formally, let \(M = 10^9 + 7\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal...
In the first test case, the entire game has \(3\) turns, and since \(m = 3\), Bob has to add in each of them. Therefore Alice should pick the biggest number she can, which is \(k = 2\), every turn.In the third test case, Alice has a strategy to guarantee a score of \(\frac{75}{8} \equiv 375000012 \pmod{10^9 + 7}\).In t...
Input: 73 3 22 1 106 3 106 4 10100 1 14 4 069 4 20 | Output: 6 5 375000012 500000026 958557139 0 49735962
Expert
4
1,002
495
454
16
1,542
D
1542D
D. Priority Queue
2,200
combinatorics; dp; implementation; math; ternary search
You are given a sequence \(A\), where its elements are either in the form + x or -, where \(x\) is an integer.For such a sequence \(S\) where its elements are either in the form + x or -, define \(f(S)\) as follows: iterate through \(S\)'s elements from the first one to the last one, and maintain a multiset \(T\) as yo...
The first line contains an integer \(n\) (\(1\leq n\leq 500\)) β€” the length of \(A\).Each of the next \(n\) lines begins with an operator + or -. If the operator is +, then it's followed by an integer \(x\) (\(1\le x<998\,244\,353\)). The \(i\)-th line of those \(n\) lines describes the \(i\)-th element in \(A\).
Print one integer, which is the answer to the problem, modulo \(998\,244\,353\).
In the first example, the following are all possible pairs of \(B\) and \(f(B)\): \(B=\) {}, \(f(B)=0\). \(B=\) {-}, \(f(B)=0\). \(B=\) {+ 1, -}, \(f(B)=0\). \(B=\) {-, + 1, -}, \(f(B)=0\). \(B=\) {+ 2, -}, \(f(B)=0\). \(B=\) {-, + 2, -}, \(f(B)=0\). \(B=\) {-}, \(f(B)=0\). \(B=\) {-, -}, \(f(B)=0\). \(B=\) {+ 1, + 2},...
Input: 4 - + 1 + 2 - | Output: 16
Hard
5
878
314
80
15
257
C
257C
C. View Angle
1,800
brute force; geometry; math
Flatland has recently introduced a new type of an eye check for the driver's licence. The check goes like that: there is a plane with mannequins standing on it. You should tell the value of the minimum angle with the vertex at the origin of coordinates and with all mannequins standing inside or on the boarder of this a...
The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of mannequins.Next n lines contain two space-separated integers each: xi, yi (|xi|, |yi| ≀ 1000) β€” the coordinates of the i-th mannequin. It is guaranteed that the origin of the coordinates has no mannequin. It is guaranteed that no two mannequins ar...
Print a single real number β€” the value of the sought angle in degrees. The answer will be considered valid if the relative or absolute error doesn't exceed 10 - 6.
Solution for the first sample test is shown below: Solution for the second sample test is shown below: Solution for the third sample test is shown below: Solution for the fourth sample test is shown below:
Input: 22 00 2 | Output: 90.0000000000
Medium
3
466
361
163
2
1,824
D
1824D
D. LuoTianyi and the Function
3,000
data structures
LuoTianyi gives you an array \(a\) of \(n\) integers and the index begins from \(1\).Define \(g(i,j)\) as follows: \(g(i,j)\) is the largest integer \(x\) that satisfies \(\{a_p:i\le p\le j\}\subseteq\{a_q:x\le q\le j\}\) while \(i \le j\); and \(g(i,j)=0\) while \(i>j\). There are \(q\) queries. For each query you are...
The first line contains two integers \(n\) and \(q\) (\(1\le n,q\le 10^6\)) β€” the length of the array \(a\) and the number of queries.The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1\le a_i\le n\)) β€” the elements of the array \(a\).Next \(q\) lines describe a query. The \(i\)-th line contains four in...
Print \(q\) lines where \(i\)-th line contains one integer β€” the answer for the \(i\)-th query.
In the first example:In the first query, the answer is \(g(1,4)+g(1,5)=3+3=6\).\(x=1,2,3\) can satisfies \(\{a_p:1\le p\le 4\}\subseteq\{a_q:x\le q\le 4\}\), \(3\) is the largest integer so \(g(1,4)=3\).In the second query, the answer is \(g(2,3)+g(3,3)=3+3=6\).In the third query, the answer is \(0\), because all \(i >...
Input: 6 4 1 2 2 1 3 4 1 1 4 5 2 3 3 3 3 6 1 2 6 6 6 6 | Output: 6 6 0 6
Master
1
429
417
95
18
717
I
717I
I. Cowboy Beblop at his computer
2,800
geometry
Cowboy Beblop is a funny little boy who likes sitting at his computer. He somehow obtained two elastic hoops in the shape of 2D polygons, which are not necessarily convex. Since there's no gravity on his spaceship, the hoops are standing still in the air. Since the hoops are very elastic, Cowboy Beblop can stretch, rot...
The first line of input contains an integer n (3 ≀ n ≀ 100 000), which denotes the number of edges of the first polygon. The next N lines each contain the integers x, y and z ( - 1 000 000 ≀ x, y, z ≀ 1 000 000) β€” coordinates of the vertices, in the manner mentioned above. The next line contains an integer m (3 ≀ m ≀ 1...
Your output should contain only one line, with the words ""YES"" or ""NO"", depending on whether the two given polygons are well-connected.
On the picture below, the two polygons are well-connected, as the edges of the vertical polygon cross the area of the horizontal one exactly once in one direction (for example, from above to below), and zero times in the other (in this case, from below to above). Note that the polygons do not have to be parallel to any...
Input: 40 0 02 0 02 2 00 2 041 1 -11 1 11 3 11 3 -1 | Output: YES
Master
1
1,497
696
139
7
1,986
E
1986E
E. Beautiful Array
1,700
greedy; math; number theory; sortings
You are given an array of integers \(a_1, a_2, \ldots, a_n\) and an integer \(k\). You need to make it beautiful with the least amount of operations.Before applying operations, you can shuffle the array elements as you like. For one operation, you can do the following: Choose an index \(1 \leq i \leq n\), Make \(a_i = ...
Each test consists of several sets of input data. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of sets of input data. Then follows their description.The first line of each set of input data contains two integers \(n\) and \(k\) (\(1 \leq n \leq 10^5\), \(1 \leq k \leq 10^9\)) β€” t...
For each set of input data, output the minimum number of operations needed to make the array beautiful, or \(-1\) if it is impossible.
In the first set of input data, the array is already beautiful.In the second set of input data, you can shuffle the array before the operations and perform the operation with index \(i = 1\) for \(83966524\) times.In the third set of input data, you can shuffle the array \(a\) and make it equal to \([2, 3, 1]\). Then a...
Input: 111 100000000012 1624323799 7082903233 13 2 14 17 1 5 35 111 2 15 7 107 11 8 2 16 8 16 3113 12 1 1 3 3 11 12 22 45 777 777 1500 7410 21 2 1 2 1 2 1 2 1 211 21 2 1 2 1 2 1 2 1 2 113 32 3 9 14 17 10 22 20 18 30 1 4 285 12 3 5 3 5 | Output: 0 83966524 1 4 6 1 48 -1 0 14 0
Medium
4
540
648
134
19
2,032
D
2032D
D. Genokraken
1,800
constructive algorithms; data structures; graphs; greedy; implementation; interactive; trees; two pointers
This is an interactive problem.Upon clearing the Waterside Area, Gretel has found a monster named Genokraken, and she's keeping it contained for her scientific studies.The monster's nerve system can be structured as a tree\(^{\dagger}\) of \(n\) nodes (really, everything should stop resembling trees all the time\(\ldot...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(4 \le n \le 10^4\)) β€” the number of nodes in Genokraken's nerve system...
In the first test case, Genokraken's nerve system forms the following tree: The answer to ""? 2 3"" is \(1\). This means that the simple path between nodes \(2\) and \(3\) contains node \(0\). In the second test case, Genokraken's nerve system forms the following tree: The answer to ""? 2 3"" is \(1\). This means that ...
Input: 3 4 1 5 1 0 9 | Output: ? 2 3 ! 0 0 1 ? 2 3 ? 2 4 ! 0 0 1 2 ! 0 0 0 1 3 5 6 7
Medium
8
2,204
405
0
20
1,825
A
1825A
A. LuoTianyi and the Palindrome String
800
greedy; strings
LuoTianyi gives you a palindrome\(^{\dagger}\) string \(s\), and she wants you to find out the length of the longest non-empty subsequence\(^{\ddagger}\) of \(s\) which is not a palindrome string. If there is no such subsequence, output \(-1\) instead.\(^{\dagger}\) A palindrome is a string that reads the same backward...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. The description of test cases follows.The first and the only line of each test case contains a single string \(s\) (\(1 \le |s| \le 50\)) consisting of lowercase English letters β€”...
For each test case, output a single integer β€” the length of the longest non-empty subsequence which is not a palindrome string. If there is no such subsequence, output \(-1\).
In the first test case, ""abcaba"" is a subsequence of ""abacaba"" as we can delete the third letter of ""abacaba"" to get ""abcaba"", and ""abcaba"" is not a palindrome string. We can prove that ""abcaba"" is an example of the longest subsequences of ""abacaba"" that isn't palindrome, so that the answer is \(6\).In th...
Input: 4abacabaaaacodeforcesecrofedoclol | Output: 6 -1 18 2
Beginner
2
769
408
175
18
1,583
C
1583C
C. Omkar and Determination
1,700
data structures; dp
The problem statement looms below, filling you with determination.Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in ce...
The first line contains two integers \(n, m\) (\(1 \leq n, m \leq 10^6\), \(nm \leq 10^6\)) β€” the dimensions of the grid \(a\).\(n\) lines follow. The \(y\)-th line contains \(m\) characters, the \(x\)-th of which is 'X' if the cell on the intersection of the the \(y\)-th row and \(x\)-th column is filled and ""."" if ...
For each query, output one line containing ""YES"" if the subgrid specified by the query is determinable and ""NO"" otherwise. The output is case insensitive (so ""yEs"" and ""No"" will also be accepted).
For each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as ""E"" if it is exitable and ""N"" otherwise.For the first query: ..X EEN... EEE... EEE... EEEFor the second query: X N. E. E. ENote that you can exit the grid by going left from an...
Input: 4 5 ..XXX ...X. ...X. ...X. 5 1 3 3 3 4 5 5 5 1 5 | Output: YES YES NO YES NO
Medium
2
1,021
676
204
15
1,811
F
1811F
F. Is It Flower?
2,100
dfs and similar; graphs; implementation
Vlad found a flowerbed with graphs in his yard and decided to take one for himself. Later he found out that in addition to the usual graphs, \(k\)-flowers also grew on that flowerbed. A graph is called a \(k\)-flower if it consists of a simple cycle of length \(k\), through each vertex of which passes its own simple cy...
The first line of input contains the single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases in the test.The descriptions of the cases follow. An empty string is written before each case.The first line of each case contains two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m \le \min(2 ...
Output \(t\) lines, each of which is the answer to the corresponding test case. As an answer, output ""YES"" if Vlad's graph is a \(k\)-flower for some \(k\), and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answe...
Input: 59 121 23 12 31 64 16 43 83 55 89 72 97 28 121 23 12 31 64 16 43 83 55 88 72 87 24 31 24 23 16 86 36 45 35 23 23 12 12 45 72 42 53 43 54 14 51 5 | Output: YES NO NO NO NO
Hard
3
682
800
323
18
250
A
250A
A. Paper Work
1,000
greedy
Polycarpus has been working in the analytic department of the ""F.R.A.U.D."" company for as much as n days. Right now his task is to make a series of reports about the company's performance for the last n days. We know that the main information in a day report is value ai, the company's profit on the i-th day. If ai is...
The first line contains integer n (1 ≀ n ≀ 100), n is the number of days. The second line contains a sequence of integers a1, a2, ..., an (|ai| ≀ 100), where ai means the company profit on the i-th day. It is possible that the company has no days with the negative ai.
Print an integer k β€” the required minimum number of folders. In the second line print a sequence of integers b1, b2, ..., bk, where bj is the number of day reports in the j-th folder.If there are multiple ways to sort the reports into k days, print any of them.
Here goes a way to sort the reports from the first sample into three folders: 1 2 3 -4 -5 | -6 5 -5 | -6 -7 6 In the second sample you can put all five reports in one folder.
Input: 111 2 3 -4 -5 -6 5 -5 -6 -7 6 | Output: 35 3 3
Beginner
1
1,228
268
261
2
2,059
A
2059A
A. Milya and Two Arrays
800
constructive algorithms; greedy; sortings
An array is called good if for any element \(x\) that appears in this array, it holds that \(x\) appears at least twice in this array. For example, the arrays \([1, 2, 1, 1, 2]\), \([3, 3]\), and \([1, 2, 4, 1, 2, 4]\) are good, while the arrays \([1]\), \([1, 2, 1]\), and \([2, 3, 4, 4]\) are not good.Milya has two go...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 50\)) β€” the length of the arrays \(a\) and \(b\).The seco...
For each test case, output \(Β«\)YES\(Β»\) (without quotes) if it is possible to obtain at least \(3\) distinct elements in array \(c\), and \(Β«\)NO\(Β»\) otherwise.You can output each letter in any case (for example, \(Β«\)YES\(Β»\), \(Β«\)Yes\(Β»\), \(Β«\)yes\(Β»\), \(Β«\)yEs\(Β»\) will be recognized as a positive answer).
In the first test case, you can swap the second and third elements. Then the array \(a = [1, 1, 2, 2]\), \(b = [1, 2, 1, 2]\), and then \(c = [2, 3, 3, 4]\). In the second test case, you can leave the elements unchanged. Then \(c = [2, 3, 4, 4, 3, 2]\).In the third test case, the array \(a\) will not change from rearra...
Input: 541 2 1 21 2 1 261 2 3 3 2 11 1 1 1 1 131 1 11 1 161 52 52 3 1 359 4 3 59 3 44100 1 100 12 2 2 2 | Output: YES YES NO YES NO
Beginner
3
657
597
315
20
491
B
491B
B. New York Hotel
2,100
greedy; math
Think of New York as a rectangular grid consisting of N vertical avenues numerated from 1 to N and M horizontal streets numerated 1 to M. C friends are staying at C hotels located at some street-avenue crossings. They are going to celebrate birthday of one of them in the one of H restaurants also located at some street...
The first line contains two integers N ΠΈ M β€” size of the city (1 ≀ N, M ≀ 109). In the next line there is a single integer C (1 ≀ C ≀ 105) β€” the number of hotels friends stayed at. Following C lines contain descriptions of hotels, each consisting of two coordinates x and y (1 ≀ x ≀ N, 1 ≀ y ≀ M). The next line contains...
In the first line output the optimal distance. In the next line output index of a restaurant that produces this optimal distance. If there are several possibilities, you are allowed to output any of them.
Input: 10 1021 13 321 104 4 | Output: 62
Hard
2
614
519
204
4
212
E
212E
E. IT Restaurants
1,500
dfs and similar; dp; trees
Π‘ity N. has a huge problem with roads, food and IT-infrastructure. In total the city has n junctions, some pairs of them are connected by bidirectional roads. The road network consists of n - 1 roads, you can get from any junction to any other one by these roads. Yes, you're right β€” the road network forms an undirected...
The first input line contains integer n (3 ≀ n ≀ 5000) β€” the number of junctions in the city. Next n - 1 lines list all roads one per line. Each road is given as a pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the indexes of connected junctions. Consider the junctions indexed from 1 to n.It is guaranteed that the given ro...
Print on the first line integer z β€” the number of sought pairs. Then print all sought pairs (a, b) in the order of increasing of the first component a.
The figure below shows the answers to the first test case. The junctions with ""iMac D0naldz"" restaurants are marked red and ""Burger Bing"" restaurants are marked blue.
Input: 51 22 33 44 5 | Output: 31 32 23 1
Medium
3
1,430
384
151
2
1,008
A
1008A
A. Romaji
900
implementation; strings
Vitya has just started learning Berlanese language. It is known that Berlanese uses the Latin alphabet. Vowel letters are ""a"", ""o"", ""u"", ""i"", and ""e"". Other letters are consonant.In Berlanese, there has to be a vowel after every consonant, but there can be any letter after any vowel. The only exception is a c...
The first line of the input contains the string \(s\) consisting of \(|s|\) (\(1\leq |s|\leq 100\)) lowercase Latin letters.
Print ""YES"" (without quotes) if there is a vowel after every consonant except ""n"", otherwise print ""NO"".You can print each letter in any case (upper or lower).
In the first and second samples, a vowel goes after each consonant except ""n"", so the word is Berlanese.In the third sample, the consonant ""c"" goes after the consonant ""r"", and the consonant ""s"" stands on the end, so the word is not Berlanese.
Input: sumimasen | Output: YES
Beginner
2
628
124
165
10
2,046
F1
2046F1
F1. Yandex Cuneiform (Easy Version)
3,300
constructive algorithms; data structures; greedy
This is the easy version of the problem. The difference between the versions is that in this version, there are no question marks. You can hack only if you solved all versions of this problem. For a long time, no one could decipher Sumerian cuneiform. However, it has finally succumbed to pressure! Today, you have the c...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5 \cdot 10^4\)). The description of the test cases follows. Each test case consists of a single line containing a template of length \(n\) (\(3 \leq n < 2 \cdot 10^5\), \(n \bmod 3 = 0\)), consisting only of ch...
For each test case, output a single line containing 'NO' if it is not possible to obtain a cuneiform from the given template.Otherwise, output 'YES' on the first line, and on the second line, any obtainable cuneiform. After that, you need to output the sequence of operations that leads to the cuneiform you printed.A se...
In the second example, the string is transformed like this: \("""" \to \mathtt{YDX} \to \mathtt{YDXDYX}\).
Input: 4YDXYDXDYXYDXDYYDXYXYX | Output: YES YDX X 0 D 0 Y 0 YES YDXDYX X 0 Y 0 D 1 X 2 D 3 Y 4 YES YDX Y 0 D 1 X 2 NO
Master
3
1,177
435
1,016
20
1,821
A
1821A
A. Matching
800
combinatorics; math
An integer template is a string consisting of digits and/or question marks.A positive (strictly greater than \(0\)) integer matches the integer template if it is possible to replace every question mark in the template with a digit in such a way that we get the decimal representation of that integer without any leading ...
The first line contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β€” the number of test cases.Each test case consists of one line containing the string \(s\) (\(1 \le |s| \le 5\)) consisting of digits and/or question marks β€” the integer template for the corresponding test case.
For each test case, print one integer β€” the number of positive (strictly greater than \(0\)) integers that match the template.
Input: 8???09031??7?5?9??99 | Output: 90 9 0 1 0 100 90 100
Beginner
2
669
284
126
18
924
D
924D
D. Contact ATC
2,500
Arkady the air traffic controller is now working with n planes in the air. All planes move along a straight coordinate axis with Arkady's station being at point 0 on it. The i-th plane, small enough to be represented by a point, currently has a coordinate of xi and is moving with speed vi. It's guaranteed that xiΒ·vi < ...
The first line contains two integers n and w (1 ≀ n ≀ 100 000, 0 ≀ w < 105) β€” the number of planes and the maximum wind speed.The i-th of the next n lines contains two integers xi and vi (1 ≀ |xi| ≀ 105, w + 1 ≀ |vi| ≀ 105, xiΒ·vi < 0) β€” the initial position and speed of the i-th plane.Planes are pairwise distinct, that...
Output a single integer β€” the number of unordered pairs of planes that can contact Arkady at the same moment.
In the first example, the following 3 pairs of planes satisfy the requirements: (2, 5) passes the station at time 3 / 4 with vwind = 1; (3, 4) passes the station at time 2 / 5 with vwind = 1 / 2; (3, 5) passes the station at time 4 / 7 with vwind = - 1 / 4. In the second example, each of the 3 planes with negative coor...
Input: 5 1-3 2-3 3-1 21 -33 -5 | Output: 3
Expert
0
1,240
393
109
9
1,240
F
1240F
F. Football
3,100
graphs
There are \(n\) football teams in the world. The Main Football Organization (MFO) wants to host at most \(m\) games. MFO wants the \(i\)-th game to be played between the teams \(a_i\) and \(b_i\) in one of the \(k\) stadiums. Let \(s_{ij}\) be the numbers of games the \(i\)-th team played in the \(j\)-th stadium. MFO d...
The first line contains three integers \(n\), \(m\), \(k\) (\(3 \leq n \leq 100\), \(0 \leq m \leq 1\,000\), \(1 \leq k \leq 1\,000\)) β€” the number of teams, the number of games, and the number of stadiums.The second line contains \(n\) integers \(w_1, w_2, \ldots, w_n\) (\(1 \leq w_i \leq 1\,000\)) β€” the amount of mon...
For each game in the same order, print \(t_i\) (\(1 \leq t_i \leq k\)) β€” the number of the stadium, in which \(a_i\) and \(b_i\) will play the game. If the \(i\)-th game should not be played, \(t_i\) should be equal to \(0\).If there are multiple answers, print any.
One of possible solutions to the example is shown below:
Input: 7 11 3 4 7 8 10 10 9 3 6 2 6 1 7 6 4 3 4 6 3 1 5 3 7 5 7 3 4 2 1 4 | Output: 3 2 1 1 3 1 2 1 2 3 2
Master
1
950
607
266
12
616
A
616A
A. Comparing Two Long Integers
900
implementation; strings
You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal.The input size is very large so don't use the reading of symbols one by one. Instead of that use the reading of a whole line or token.As input/output can reach huge siz...
The first line contains a non-negative integer a.The second line contains a non-negative integer b.The numbers a, b may contain leading zeroes. Each of them contains no more than 106 digits.
Print the symbol ""<"" if a < b and the symbol "">"" if a > b. If the numbers are equal print the symbol ""="".
Input: 910 | Output: <
Beginner
2
607
190
111
6
666
B
666B
B. World Tour
2,000
graphs; shortest paths
A famous sculptor Cicasso goes to a world tour!Well, it is not actually a world-wide. But not everyone should have the opportunity to see works of sculptor, shouldn't he? Otherwise there will be no any exclusivity. So Cicasso will entirely hold the world tour in his native country β€” Berland.Cicasso is very devoted to h...
In the first line there is a pair of integers n and m (4 ≀ n ≀ 3000, 3 ≀ m ≀ 5000) β€” a number of cities and one-way roads in Berland.Each of the next m lines contains a pair of integers ui, vi (1 ≀ ui, vi ≀ n) β€” a one-way road from the city ui to the city vi. Note that ui and vi are not required to be distinct. Moreove...
Print four integers β€” numbers of cities which Cicasso will visit according to optimal choice of the route. Numbers of cities should be printed in the order that Cicasso will visit them. If there are multiple solutions, print any of them.
Let d(x, y) be the shortest distance between cities x and y. Then in the example d(2, 1) = 3, d(1, 8) = 7, d(8, 7) = 3. The total distance equals 13.
Input: 8 91 22 33 44 14 55 66 77 88 5 | Output: 2 1 8 7
Hard
2
2,098
387
237
6
2,118
A
2118A
A. Equal Subsequences
800
constructive algorithms; greedy
We call a bitstring\(^{\text{βˆ—}}\) perfect if it has the same number of \(\mathtt{101}\) and \(\mathtt{010}\) subsequences\(^{\text{†}}\). Construct a perfect bitstring of length \(n\) where the number of \(\mathtt{1}\) characters it contains is exactly \(k\).It can be proven that the construction is always possible. I...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 100\), \(0 \le k \le n\)) β€” the size of the bitstring and the number of \...
For each test case, output the constructed bitstring. If there are multiple solutions, output any of them.
In the first test case, the number of \(\mathtt{101}\) and \(\mathtt{010}\) subsequences is the same, both being \(1\), and the sequence contains exactly two \(\mathtt{1}\) characters.In the second test case, the number of \(\mathtt{101}\) and \(\mathtt{010}\) subsequences is the same, both being \(2\), and the sequenc...
Input: 54 25 35 56 21 1 | Output: 1010 10110 11111 100010 1
Beginner
2
642
362
106
21
632
A
632A
A. Grandma Laura and Apples
1,200
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also...
The first line contains two integers n and p (1 ≀ n ≀ 40, 2 ≀ p ≀ 1000) β€” the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the s...
Print the only integer a β€” the total money grandma should have at the end of the day.Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer.
Input: 2 10halfhalfplus | Output: 15
Easy
0
876
510
263
6
442
A
442A
A. Borya and Hanabi
1,700
bitmasks; brute force; implementation
Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game.Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding n cards. The game is somewhat complicated by the fact that everybody sees Borya's cards except ...
The first line contains integer n (1 ≀ n ≀ 100) β€” the number of Borya's cards. The next line contains the descriptions of n cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters β€” R, G, B, Y, W). The second ch...
Print a single integer β€” the minimum number of hints that the other players should make.
In the first sample Borya already knows for each card that it is a green three.In the second sample we can show all fours and all red cards.In the third sample you need to make hints about any four colors.
Input: 2G3 G3 | Output: 0
Medium
3
1,118
430
88
4
1,766
A
1766A
A. Extremely Round
800
brute force; implementation
Let's call a positive integer extremely round if it has only one non-zero digit. For example, \(5000\), \(4\), \(1\), \(10\), \(200\) are extremely round integers; \(42\), \(13\), \(666\), \(77\), \(101\) are not.You are given an integer \(n\). You have to calculate the number of extremely round integers \(x\) such tha...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.Then, \(t\) lines follow. The \(i\)-th of them contains one integer \(n\) (\(1 \le n \le 999999\)) β€” the description of the \(i\)-th test case.
For each test case, print one integer β€” the number of extremely round integers \(x\) such that \(1 \le x \le n\).
Input: 594213100111 | Output: 9 13 10 19 19
Beginner
2
340
235
113
17
1,019
D
1019D
D. Large Triangle
2,700
binary search; geometry; sortings
There is a strange peculiarity: if you connect the cities of Rostov, Taganrog and Shakhty, peculiarly, you get a triangleΒ«Unbelievable But TrueΒ»Students from many different parts of Russia and abroad come to Summer Informatics School. You marked the hometowns of the SIS participants on a map.Now you decided to prepare ...
The first line of input contains two integers \(n\) and \(S\) (\(3 \le n \le 2000\), \(1 \le S \le 2 \cdot 10^{18}\)) β€” the number of cities on the map and the area of the triangle to be found.The next \(n\) lines contain descriptions of the cities, one per line. Each city is described by its integer coordinates \(x_i\...
If the solution doesn't exist β€” print Β«NoΒ».Otherwise, print Β«YesΒ», followed by three pairs of coordinates \((x, y)\) β€” the locations of the three cities, which form the triangle of area \(S\).
Input: 3 70 03 00 4 | Output: No
Master
3
482
495
192
10
843
A
843A
A. Sorting by Subsequences
1,400
dfs and similar; dsu; implementation; math; sortings
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.Sorting integers in a subsequence is a...
The first line of input data contains integer n (1 ≀ n ≀ 105) β€” the length of the sequence.The second line of input data contains n different integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci ≀ n), then ci integers l1, l2, ..., lci (1 ≀ l...
In the first sample output:After sorting the first subsequence we will get sequence 1 2 3 6 5 4.Sorting the second subsequence changes nothing.After sorting the third subsequence we will get sequence 1 2 3 4 5 6.Sorting the last subsequence changes nothing.
Input: 63 2 1 6 5 4 | Output: 42 1 31 22 4 61 5
Easy
5
561
283
536
8
1,344
A
1344A
A. Hilbert's Hotel
1,600
math; number theory; sortings
Hilbert's Hotel is a very unusual hotel since the number of rooms is infinite! In fact, there is exactly one room for every integer, including zero and negative integers. Even stranger, the hotel is currently at full capacity, meaning there is exactly one guest in every room. The hotel's manager, David Hilbert himself,...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) β€” the number of test cases. Next \(2t\) lines contain descriptions of test cases.The first line of each test case contains a single integer \(n\) (\(1\le n\le 2\cdot 10^5\)) β€” the length of the array.The secon...
For each test case, output a single line containing ""YES"" if there is exactly one guest assigned to each room after the shuffling process, or ""NO"" otherwise. You can print each letter in any case (upper or lower).
In the first test case, every guest is shifted by \(14\) rooms, so the assignment is still unique.In the second test case, even guests move to the right by \(1\) room, and odd guests move to the left by \(1\) room. We can show that the assignment is still unique.In the third test case, every fourth guest moves to the r...
Input: 6 1 14 2 1 -1 4 5 5 5 1 3 3 2 1 2 0 1 5 -239 -2 -100 -3 -11 | Output: YES YES YES NO NO YES
Medium
3
1,126
514
217
13
287
A
287A
A. IQ Test
1,100
brute force; implementation
In the city of Ultima Thule job applicants are often offered an IQ test. The test is as follows: the person gets a piece of squared paper with a 4 Γ— 4 square painted on it. Some of the square's cells are painted black and others are painted white. Your task is to repaint at most one cell the other color so that the pic...
Four lines contain four characters each: the j-th character of the i-th line equals ""."" if the cell in the i-th row and the j-th column of the square is painted white, and ""#"", if the cell is black.
Print ""YES"" (without the quotes), if the test can be passed and ""NO"" (without the quotes) otherwise.
In the first test sample it is enough to repaint the first cell in the second row. After such repainting the required 2 Γ— 2 square is on the intersection of the 1-st and 2-nd row with the 1-st and 2-nd column.
Input: ####.#..####.... | Output: YES
Easy
2
736
202
104
2
1,411
E
1411E
E. Poman Numbers
2,300
bitmasks; greedy; math; strings
You've got a string \(S\) consisting of \(n\) lowercase English letters from your friend. It turned out that this is a number written in poman numerals. The poman numeral system is long forgotten. All that's left is the algorithm to transform number from poman numerals to the numeral system familiar to us. Characters o...
The first line contains two integers \(n\) and \(T\) (\(2 \leq n \leq 10^5\), \(-10^{15} \leq T \leq 10^{15}\)).The second line contains a string \(S\) consisting of \(n\) lowercase English letters.
Print ""Yes"" if it is possible to get the desired value. Otherwise, print ""No"".You can print each letter in any case (upper or lower).
In the second example, you cannot get \(-7\). But you can get \(1\), for example, as follows: First choose \(m = 1\), then \(f(\)abc\() = -f(\)a\() + f(\)bc\()\) \(f(\)a\() = 2^0 = 1\) \(f(\)bc\() = -f(\)b\() + f(\)c\() = -2^1 + 2^2 = 2\) In the end \(f(\)abc\() = -1 + 2 = 1\)
Input: 2 -1 ba | Output: Yes
Expert
4
1,053
198
137
14
799
C
799C
C. Fountains
1,800
binary search; data structures; implementation
Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There are n available fountains, for each fountain its beauty and cost are known. There are two types of money in the game: coins and diamonds, so each fountain cost can be either in coins or diamonds. No money changes between the types are allow...
The first line contains three integers n, c and d (2 ≀ n ≀ 100 000, 0 ≀ c, d ≀ 100 000) β€” the number of fountains, the number of coins and diamonds Arkady has.The next n lines describe fountains. Each of these lines contain two integers bi and pi (1 ≀ bi, pi ≀ 100 000) β€” the beauty and the cost of the i-th fountain, an...
Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0.
In the first example Arkady should build the second fountain with beauty 4, which costs 3 coins. The first fountain he can't build because he don't have enough coins. Also Arkady should build the third fountain with beauty 5 which costs 6 diamonds. Thus the total beauty of built fountains is 9.In the second example the...
Input: 3 7 610 8 C4 3 C5 6 D | Output: 9
Medium
3
424
451
115
7
1,331
D
1331D
D. Again?
0
*special; implementation
The only line of the input contains a 7-digit hexadecimal number. The first ""digit"" of the number is letter A, the rest of the ""digits"" are decimal digits 0-9.
Output a single integer.
Input: A278832 | Output: 0
Beginner
2
0
163
24
13
1,520
G
1520G
G. To Go Or Not To Go?
2,200
brute force; dfs and similar; graphs; greedy; implementation; shortest paths
Dima overslept the alarm clock, which was supposed to raise him to school.Dima wonders if he will have time to come to the first lesson. To do this, he needs to know the minimum time it will take him to get from home to school.The city where Dima lives is a rectangular field of \(n \times m\) size. Each cell \((i, j)\)...
The first line contains three integers \(n\), \(m\) and \(w\) (\(2 \le n, m \le 2 \cdot 10^3\), \(1 \le w \le 10^9\)), where \(n\) and \(m\) are city size, \(w\) is time during which Dima moves between unoccupied cells.The next \(n\) lines each contain \(m\) numbers (\(-1 \le a_{ij} \le 10^9\)) β€” descriptions of cells....
Output the minimum time it will take for Dima to get to school. If he cannot get to school at all, then output ""-1"".
Explanation for the first sample:
Input: 5 5 1 0 -1 0 1 -1 0 20 0 0 -1 -1 -1 -1 -1 -1 3 0 0 0 0 -1 0 0 0 0 | Output: 14
Hard
6
1,140
387
118
15
1,663
D
1663D
D. Is it rated - 3
0
*special; combinatorics; dp; math
The first line contains a string \(S\).The second line contains an integer \(X\).Constraints \(|S|=3\) \(0 \le X \le 10^9\) The input is guaranteed to be valid.
Beginner
4
0
160
0
16
1,675
C
1675C
C. Detective Task
1,100
implementation
Polycarp bought a new expensive painting and decided to show it to his \(n\) friends. He hung it in his room. \(n\) of his friends entered and exited there one by one. At one moment there was no more than one person in the room. In other words, the first friend entered and left first, then the second, and so on.It is k...
The first number \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases in the test.The following is a description of test cases.The first line of each test case contains one string \(s\) (length does not exceed \(2 \cdot 10^5\)) β€” a description of the friends' answers, where \(s_i\) indicates the answer of the \(i\)-...
Output one positive (strictly more zero) number – the number of people who could steal the picture based on the data shown.
In the first case, the answer is \(1\) since we had exactly \(1\) friend.The second case is similar to the first.In the third case, the suspects are the third and fourth friends (we count from one). It can be shown that no one else could be the thief.In the fourth case, we know absolutely nothing, so we suspect everyon...
Input: 8011110000?????1?1??0?00?0?????11??0?? | Output: 1 1 2 5 4 1 1 3
Easy
1
1,049
653
123
16
606
A
606A
A. Magic Spheres
1,200
implementation
Carl is a beginner magician. He has a blue, b violet and c orange magic spheres. In one move he can transform two spheres of the same color into one sphere of any other color. To make a spell that has never been seen before, he needs at least x blue, y violet and z orange spheres. Can he get them (possible, in multiple...
The first line of the input contains three integers a, b and c (0 ≀ a, b, c ≀ 1 000 000) β€” the number of blue, violet and orange spheres that are in the magician's disposal.The second line of the input contains three integers, x, y and z (0 ≀ x, y, z ≀ 1 000 000) β€” the number of blue, violet and orange spheres that he ...
If the wizard is able to obtain the required numbers of spheres, print ""Yes"". Otherwise, print ""No"".
In the first sample the wizard has 4 blue and 4 violet spheres. In his first action he can turn two blue spheres into one violet one. After that he will have 2 blue and 5 violet spheres. Then he turns 4 violet spheres into 2 orange spheres and he ends up with 2 blue, 1 violet and 2 orange spheres, which is exactly what...
Input: 4 4 02 1 2 | Output: Yes
Easy
1
330
333
104
6
1,738
E
1738E
E. Balance Addicts
2,300
combinatorics; dp; math; two pointers
Given an integer sequence \(a_1, a_2, \dots, a_n\) of length \(n\), your task is to compute the number, modulo \(998244353\), of ways to partition it into several non-empty continuous subsequences such that the sums of elements in the subsequences form a balanced sequence.A sequence \(s_1, s_2, \dots, s_k\) of length \...
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^5\)) β€” the number of test cases. The following lines contain the description of each test case.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^5\)), indicating the length of the sequence \...
For each test case, output the number of partitions with respect to which the sum of elements in each subsequence is balanced, modulo \(998244353\).
For the first test case, there is only one way to partition a sequence of length \(1\), which is itself and is, of course, balanced. For the second test case, there are \(2\) ways to partition it: The sequence \([1, 1]\) itself, then \(s = [2]\) is balanced; Partition into two subsequences \([1\,|\,1]\), then \(s = [1,...
Input: 61100000000021 140 0 1 051 2 3 2 151 3 5 7 9320 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | Output: 1 2 3 4 2 150994942
Expert
4
1,796
566
148
17
770
B
770B
B. Maximize Sum of Digits
1,300
*special; implementation; math
Anton has the integer x. He is interested what positive integer, which doesn't exceed x, has the maximum sum of digits.Your task is to help Anton and to find the integer that interests him. If there are several such integers, determine the biggest of them.
The first line contains the positive integer x (1 ≀ x ≀ 1018) β€” the integer which Anton has.
Print the positive integer which doesn't exceed x and has the maximum sum of digits. If there are several such integers, print the biggest of them. Printed integer must not contain leading zeros.
Input: 100 | Output: 99
Easy
3
256
92
195
7
2,065
A
2065A
A. Skibidus and Amog'u
800
brute force; constructive algorithms; greedy; implementation; strings
Skibidus lands on a foreign planet, where the local Amog tribe speaks the Amog'u language. In Amog'u, there are two forms of nouns, which are singular and plural.Given that the root of the noun is transcribed as \(S\), the two forms are transcribed as: Singular: \(S\) \(+\) ""us"" Plural: \(S\) \(+\) ""i"" Here, \(+\) ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows. The only line of each test case contains a string \(W\), which is a transcribed Amog'u noun in singular form. It is guaranteed that \(W\) consists of only lowe...
For each test case, output the transcription of the corresponding plural noun on a separate line.
Input: 9ussusfunguscactussussusamoguschungusntarsusskibidus | Output: i si fungi cacti sussi amogi chungi ntarsi skibidi
Beginner
5
848
396
97
20