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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
718 |
E
|
718E
|
E. Matvey's Birthday
| 3,300 |
bitmasks; graphs
|
Today is Matvey's birthday. He never knows what to ask as a present so friends gave him a string s of length n. This string consists of only first eight English letters: 'a', 'b', ..., 'h'.First question that comes to mind is: who might ever need some string? Matvey is a special boy so he instantly found what to do with this string. He used it to build an undirected graph where vertices correspond to position in the string and there is an edge between distinct positions a and b (1 β€ a, b β€ n) if at least one of the following conditions hold: a and b are neighbouring, i.e. |a - b| = 1. Positions a and b contain equal characters, i.e. sa = sb. Then Matvey decided to find the diameter of this graph. Diameter is a maximum distance (length of the shortest path) among all pairs of vertices. Also, Matvey wants to find the number of pairs of vertices such that the distance between them is equal to the diameter of the graph. As he is very cool and experienced programmer he managed to solve this problem very fast. Will you do the same?
|
The first line of the input contains a single integer n (2 β€ n β€ 100 000) β the length of the string.The second line contains the string s itself. It's guaranteed that s consists of only first eight letters of English alphabet.
|
Print two integers β the diameter of the graph and the number of pairs of positions with the distance equal to the diameter.
|
Consider the second sample. The maximum distance is 2. It's obtained for pairs (1, 4), (2, 4), (4, 6) and (4, 7).
|
Input: 3abc | Output: 2 1
|
Master
| 2 | 1,041 | 227 | 124 | 7 |
424 |
D
|
424D
|
D. Biathlon Track
| 2,300 |
binary search; brute force; constructive algorithms; data structures; dp
|
Recently an official statement of the world Olympic Committee said that the Olympic Winter Games 2030 will be held in Tomsk. The city officials decided to prepare for the Olympics thoroughly and to build all the necessary Olympic facilities as early as possible. First, a biathlon track will be built.To construct a biathlon track a plot of land was allocated, which is a rectangle divided into n Γ m identical squares. Each of the squares has two coordinates: the number of the row (from 1 to n), where it is located, the number of the column (from 1 to m), where it is located. Also each of the squares is characterized by its height. During the sports the biathletes will have to move from one square to another. If a biathlete moves from a higher square to a lower one, he makes a descent. If a biathlete moves from a lower square to a higher one, he makes an ascent. If a biathlete moves between two squares with the same height, then he moves on flat ground.The biathlon track should be a border of some rectangular area of the allocated land on which biathletes will move in the clockwise direction. It is known that on one move on flat ground an average biathlete spends tp seconds, an ascent takes tu seconds, a descent takes td seconds. The Tomsk Administration wants to choose the route so that the average biathlete passes it in as close to t seconds as possible. In other words, the difference between time ts of passing the selected track and t should be minimum.For a better understanding you can look at the first sample of the input data. In this sample n = 6, m = 7, and the administration wants the track covering time to be as close to t = 48 seconds as possible, also, tp = 3, tu = 6 and td = 2. If we consider the rectangle shown on the image by arrows, the average biathlete can move along the boundary in a clockwise direction in exactly 48 seconds. The upper left corner of this track is located in the square with the row number 4, column number 3 and the lower right corner is at square with row number 6, column number 7. Among other things the administration wants all sides of the rectangle which boundaries will be the biathlon track to consist of no less than three squares and to be completely contained within the selected land.You are given the description of the given plot of land and all necessary time values. You are to write the program to find the most suitable rectangle for a biathlon track. If there are several such rectangles, you are allowed to print any of them.
|
The first line of the input contains three integers n, m and t (3 β€ n, m β€ 300, 1 β€ t β€ 109) β the sizes of the land plot and the desired distance covering time.The second line also contains three integers tp, tu and td (1 β€ tp, tu, td β€ 100) β the time the average biathlete needs to cover a flat piece of the track, an ascent and a descent respectively.Then n lines follow, each line contains m integers that set the heights of each square of the given plot of land. Each of the height values is a positive integer, not exceeding 106.
|
In a single line of the output print four positive integers β the number of the row and the number of the column of the upper left corner and the number of the row and the number of the column of the lower right corner of the rectangle that is chosen for the track.
|
Input: 6 7 483 6 25 4 8 3 3 7 94 1 6 8 7 1 11 6 4 6 4 8 67 2 6 1 6 9 41 9 8 6 3 9 24 5 6 8 4 3 7 | Output: 4 3 6 7
|
Expert
| 5 | 2,511 | 536 | 265 | 4 |
|
659 |
A
|
659A
|
A. Round House
| 1,000 |
implementation; math
|
Vasya lives in a round building, whose entrances are numbered sequentially by integers from 1 to n. Entrance n and entrance 1 are adjacent.Today Vasya got bored and decided to take a walk in the yard. Vasya lives in entrance a and he decided that during his walk he will move around the house b entrances in the direction of increasing numbers (in this order entrance n should be followed by entrance 1). The negative value of b corresponds to moving |b| entrances in the order of decreasing numbers (in this order entrance 1 is followed by entrance n). If b = 0, then Vasya prefers to walk beside his entrance. Illustration for n = 6, a = 2, b = - 5. Help Vasya to determine the number of the entrance, near which he will be at the end of his walk.
|
The single line of the input contains three space-separated integers n, a and b (1 β€ n β€ 100, 1 β€ a β€ n, - 100 β€ b β€ 100) β the number of entrances at Vasya's place, the number of his entrance and the length of his walk, respectively.
|
Print a single integer k (1 β€ k β€ n) β the number of the entrance where Vasya will be at the end of his walk.
|
The first example is illustrated by the picture in the statements.
|
Input: 6 2 -5 | Output: 3
|
Beginner
| 2 | 749 | 234 | 109 | 6 |
620 |
F
|
620F
|
F. Xors on Segments
| 2,800 |
data structures; strings; trees
|
You are given an array with n integers ai and m queries. Each query is described by two integers (lj, rj).Let's define the function . The function is defined for only u β€ v.For each query print the maximal value of the function f(ax, ay) over all lj β€ x, y β€ rj, ax β€ ay.
|
The first line contains two integers n, m (1 β€ n β€ 5Β·104, 1 β€ m β€ 5Β·103) β the size of the array and the number of the queries.The second line contains n integers ai (1 β€ ai β€ 106) β the elements of the array a.Each of the next m lines contains two integers lj, rj (1 β€ lj β€ rj β€ n) β the parameters of the j-th query.
|
For each query print the value aj on a separate line β the maximal value of the function f(ax, ay) over all lj β€ x, y β€ rj, ax β€ ay.
|
Input: 6 31 2 3 4 5 61 62 53 4 | Output: 777
|
Master
| 3 | 271 | 318 | 132 | 6 |
|
661 |
F
|
661F
|
F. Primes in Interval
| 1,900 |
*special
|
You are given two integers a and b (a β€ b). How many prime numbers are there on the interval from a to b, inclusive?
|
The input contains two integers a and b (2 β€ a β€ b β€ 1 000 000), separated by a single space.
|
Output a single integer β the number of primes between a and b, inclusive.
|
Input: 10 20 | Output: 4
|
Hard
| 1 | 116 | 93 | 74 | 6 |
|
1,033 |
B
|
1033B
|
B. Square Difference
| 1,100 |
math; number theory
|
Alice has a lovely piece of cloth. It has the shape of a square with a side of length \(a\) centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length \(b\) centimeters (where \(b < a\)). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).Alice would like to know whether the area of her cloth expressed in square centimeters is prime. Could you help her to determine it?
|
The first line contains a number \(t\) (\(1 \leq t \leq 5\)) β the number of test cases.Each of the next \(t\) lines describes the \(i\)-th test case. It contains two integers \(a\) and \(b~(1 \leq b < a \leq 10^{11})\) β the side length of Alice's square and the side length of the square that Bob wants.
|
Print \(t\) lines, where the \(i\)-th line is the answer to the \(i\)-th test case. Print ""YES"" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print ""NO"".You can print each letter in an arbitrary case (upper or lower).
|
The figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is \(6^2 - 5^2 = 36 - 25 = 11\), which is prime, so the answer is ""YES"". In the second case, the area is \(16^2 - 13^2 = 87\), which is divisible by \(3\). In the third case, the area of the remaining piece is \(61690850361^2 - 24777622630^2 = 3191830435068605713421\). This number is not prime because \(3191830435068605713421 = 36913227731 \cdot 86468472991 \).In the last case, the area is \(34^2 - 33^2 = 67\).
|
Input: 46 516 1361690850361 2477762263034 33 | Output: YESNONOYES
|
Easy
| 2 | 534 | 305 | 256 | 10 |
311 |
A
|
311A
|
A. The Closest Pair
| 1,300 |
constructive algorithms; implementation
|
Currently Tiny is learning Computational Geometry. When trying to solve a problem called ""The Closest Pair Of Points In The Plane"", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is .The pseudo code of the unexpected code is as follows:input nfor i from 1 to n input the i-th point's coordinates into p[i]sort array p[] by increasing of x coordinate first and increasing of y coordinate secondd=INF //here INF is a number big enoughtot=0for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that ""break"" is only to be //out of the loop ""for j"" d=min(d,distance(p[i],p[j]))output dHere, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
|
A single line which contains two space-separated integers n and k (2 β€ n β€ 2000, 1 β€ k β€ 109).
|
If there doesn't exist such a data which let the given code get TLE, print ""no solution"" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| β€ 109) representing the coordinates of the i-th point.The conditions below must be held: All the points must be distinct. |xi|, |yi| β€ 109. After running the given code, the value of tot should be larger than k.
|
Input: 4 3 | Output: 0 00 11 01 1
|
Easy
| 2 | 1,162 | 94 | 399 | 3 |
|
1,187 |
C
|
1187C
|
C. Vasya And Array
| 1,800 |
constructive algorithms; greedy; implementation
|
Vasya has an array \(a_1, a_2, \dots, a_n\).You don't know this array, but he told you \(m\) facts about this array. The \(i\)-th fact is a triple of numbers \(t_i\), \(l_i\) and \(r_i\) (\(0 \le t_i \le 1, 1 \le l_i < r_i \le n\)) and it means: if \(t_i=1\) then subbarray \(a_{l_i}, a_{l_i + 1}, \dots, a_{r_i}\) is sorted in non-decreasing order; if \(t_i=0\) then subbarray \(a_{l_i}, a_{l_i + 1}, \dots, a_{r_i}\) is not sorted in non-decreasing order. A subarray is not sorted if there is at least one pair of consecutive elements in this subarray such that the former is greater than the latter. For example if \(a = [2, 1, 1, 3, 2]\) then he could give you three facts: \(t_1=1, l_1=2, r_1=4\) (the subarray \([a_2, a_3, a_4] = [1, 1, 3]\) is sorted), \(t_2=0, l_2=4, r_2=5\) (the subarray \([a_4, a_5] = [3, 2]\) is not sorted), and \(t_3=0, l_3=3, r_3=5\) (the subarray \([a_3, a_5] = [1, 3, 2]\) is not sorted).You don't know the array \(a\). Find any array which satisfies all the given facts.
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 1000, 1 \le m \le 1000\)).Each of the next \(m\) lines contains three integers \(t_i\), \(l_i\) and \(r_i\) (\(0 \le t_i \le 1, 1 \le l_i < r_i \le n\)).If \(t_i = 1\) then subbarray \(a_{l_i}, a_{l_i + 1}, \dots , a_{r_i}\) is sorted. Otherwise (if \(t_i = 0\)) subbarray \(a_{l_i}, a_{l_i + 1}, \dots , a_{r_i}\) is not sorted.
|
If there is no array that satisfies these facts in only line print NO (in any letter case).If there is a solution, print YES (in any letter case). In second line print \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the array \(a\), satisfying all the given facts. If there are multiple satisfying arrays you can print any of them.
|
Input: 7 4 1 1 3 1 2 5 0 5 6 1 6 7 | Output: YES 1 2 2 3 5 4 4
|
Medium
| 3 | 1,005 | 397 | 350 | 11 |
|
394 |
B
|
394B
|
B. Very Beautiful Number
| 0 |
math
|
Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a ""very beautiful number"". But the problem is, he's left his paper with the number in the teachers' office.The teacher remembers that the ""very beautiful number"" was strictly positive, didn't contain any leading zeroes, had the length of exactly p decimal digits, and if we move the last digit of the number to the beginning, it grows exactly x times. Besides, the teacher is sure that among all such numbers the ""very beautiful number"" is minimal possible.The teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the ""very beautiful number"". Help to restore the justice, find the ""very beautiful number"" for us!
|
The single line contains integers p, x (1 β€ p β€ 106, 1 β€ x β€ 9).
|
If the teacher's made a mistake and such number doesn't exist, then print on a single line ""Impossible"" (without the quotes). Otherwise, print the ""very beautiful number"" without leading zeroes.
|
Sample 1: 142857Β·5 = 714285.Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is ""Impossible"".
|
Input: 6 5 | Output: 142857
|
Beginner
| 1 | 855 | 64 | 198 | 3 |
1,860 |
F
|
1860F
|
F. Evaluate RBS
| 2,900 |
data structures; geometry; implementation; math; sortings
|
You are given \(2n\) tuples of values \((a, b, c)\), where \(a\) and \(b\) are positive integers and \(c\) is a bracket '(' or ')'. Exactly \(n\) tuples have \(c = \)'(' and the other \(n\) tuples have \(c =\) ')'.You are asked to choose two positive values \(x\) and \(y\) (\(x > 0\) and \(y > 0\); not necessarily integers) and sort the tuples in the increasing value of \(a \cdot x + b \cdot y\). If several tuples have the same value, you can place them in any order among themselves.Is it possible to choose such \(x\) and \(y\) that taking brackets \(c\) from the tuples in the resulting order produces a regular bracket sequence?A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters ""1"" and ""+"" between the original characters of the sequence.
|
The first line contains a single integer \(t\) (\(1 \le t \le 1500\)) β the number of testcases.The first line of each testcase contains a single integer \(n\) (\(1 \le n \le 1500\)).The \(i\)-th of the next \(2n\) lines contains two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le 10^6\)) and a character \(c_i\) (\(c_i = \)'(' or \(c_i = \)')'). Exactly \(n\) tuples have \(c_i = \)'(' and the other \(n\) tuples have \(c_i =\) ')'.The sum of \(n\) over all testcases doesn't exceed \(1500\).
|
For each testcase, print ""YES"" if it's possible to choose such \(x\) and \(y\) that taking brackets \(c\) from the tuples in the resulting order produces a regular bracket sequence. Otherwise, print ""NO"".
|
In the first testcase, you can choose \(x = 10, y = 0.1\). The values for tuples will be \(10 \cdot 1 + 0.1 \cdot 4 = 10.4\) and \(10 \cdot 2 + 0.1 \cdot 3 = 20.3\). Thus, the first tuple will go first, then the second one, making the bracket sequence ""()"", which is regular.In the second testcase, you can't choose positive \(x\) and \(y\) such that the opening brackets gets assigned a value less or equal to the value of the closing bracket.In the fourth testcase, you can choose \(x = 0.6, y = 0.55\). The bracket sequence is ""(()(()))"".
|
Input: 411 4 (2 3 )11 2 )3 4 (416 5 (12 3 (19 6 )4 10 )3 10 )19 11 (19 7 )7 14 (416 8 (11 9 )20 10 )20 19 )2 13 (18 7 (15 19 )5 6 ( | Output: YES NO NO YES
|
Master
| 5 | 834 | 497 | 208 | 18 |
2,126 |
A
|
2126A
|
A. Only One Digit
| 800 |
brute force; implementation; math
|
You are given an integer \(x\). You need to find the smallest non-negative integer \(y\) such that the numbers \(x\) and \(y\) share at least one common digit. In other words, there must exist a decimal digit \(d\) that appears in both the representation of the number \(x\) and the number \(y\).
|
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first line of each test case contains one integer \(x\) (\(1 \le x \le 1000\)).
|
For each test case, output one integer \(y\) β the minimum non-negative number that satisfies the condition.
|
In the first test case, the numbers \(6\) and \(6\) share the common digit '6'. Moreover, there is no natural number smaller than this that shares a common digit.In the second test case, the numbers \(6\) and \(96\) share the common digit '6'.
|
Input: 569678122696 | Output: 6 6 7 1 6
|
Beginner
| 3 | 296 | 174 | 108 | 21 |
101 |
E
|
101E
|
E. Candies and Stones
| 2,500 |
divide and conquer; dp
|
Little Gerald and his coach Mike play an interesting game. At the beginning of the game there is a pile consisting of n candies and a pile consisting of m stones. Gerald and Mike move in turns, Mike goes first. During his move Mike checks how many candies and stones Gerald has eaten. Let Gerald eat a candies and b stones. Then Mike awards Gerald f(a, b) prize points. Gerald during his move either eats a candy from the pile of candies or a stone from the pile of stones. As Mike sees that Gerald has eaten everything apart one candy and one stone, he awards points for the last time and the game ends. Gerald is not allowed to eat all the candies, and he is not allowed to eat all the stones too. Tell Gerald how to play to get the largest possible number of points: it is required to find one of the possible optimal playing strategies for Gerald.
|
The first line contains three integers n, m, p (1 β€ n, m β€ 20000, 1 β€ p β€ 109). The second line contains n integers x0, x1, ..., xn - 1 (0 β€ xi β€ 20000). The third line contains m integers y0, y1, ..., ym - 1 (0 β€ yi β€ 20000). The value of f(a, b) is calculated as a remainder of the division of the sum xa + yb by number p.
|
Print on the first line the only number: the maximal number of points Gerald can earn. Print on the second line a sting consisting of n + m - 2 characters, each of which is either a ""C"" or ""S"", the i-th character should be ""C"" if Gerald's i-th move should be eating a candy and ""S"" if he should eat a stone.
|
In the first test if Gerald's first move is eating a stone, he will receive a point for it and if he eats a candy, he will get zero pints. In any way Gerald will get 0 points before his first move, and 1 after his second one. This, the maximum number of points Gerald can get equals to 2, and for that he should first eat a stone, then a candy.
|
Input: 2 2 100 00 1 | Output: 2SC
|
Expert
| 2 | 851 | 324 | 315 | 1 |
557 |
B
|
557B
|
B. Pasha and Tea
| 1,500 |
constructive algorithms; implementation; math; sortings
|
Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water.It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; Pasha pours the same amount of water to each girl; Pasha pours the same amount of water to each boy; if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does.Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends.
|
The first line of the input contains two integers, n and w (1 β€ n β€ 105, 1 β€ w β€ 109) β the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters.The second line of the input contains the sequence of integers ai (1 β€ ai β€ 109, 1 β€ i β€ 2n) β the capacities of Pasha's tea cups in milliliters.
|
Print a single real number β the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.
|
Pasha also has candies that he is going to give to girls but that is another task...
|
Input: 2 41 1 1 1 | Output: 3
|
Medium
| 4 | 1,020 | 383 | 248 | 5 |
1,265 |
E
|
1265E
|
E. Beautiful Mirrors
| 2,100 |
data structures; dp; math; probabilities
|
Creatnx has \(n\) mirrors, numbered from \(1\) to \(n\). Every day, Creatnx asks exactly one mirror ""Am I beautiful?"". The \(i\)-th mirror will tell Creatnx that he is beautiful with probability \(\frac{p_i}{100}\) for all \(1 \le i \le n\).Creatnx asks the mirrors one by one, starting from the \(1\)-st mirror. Every day, if he asks \(i\)-th mirror, there are two possibilities: The \(i\)-th mirror tells Creatnx that he is beautiful. In this case, if \(i = n\) Creatnx will stop and become happy, otherwise he will continue asking the \(i+1\)-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the \(1\)-st mirror again. You need to calculate the expected number of days until Creatnx becomes happy.This number should be found by modulo \(998244353\). Formally, let \(M = 998244353\). 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 to \(p \cdot q^{-1} \bmod M\). In other words, output such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{M}\).
|
The first line contains one integer \(n\) (\(1\le n\le 2\cdot 10^5\)) β the number of mirrors.The second line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq 100\)).
|
Print the answer modulo \(998244353\) in a single line.
|
In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability \(\frac{1}{2}\). So, the expected number of days until Creatnx becomes happy is \(2\).
|
Input: 1 50 | Output: 2
|
Hard
| 4 | 1,168 | 186 | 55 | 12 |
1,679 |
E
|
1679E
|
E. Typical Party in Dorm
| 2,400 |
bitmasks; combinatorics; dp; strings
|
Today is a holiday in the residence hall β Oleh arrived, in honor of which the girls gave him a string. Oleh liked the gift a lot, so he immediately thought up and offered you, his best friend, the following problem.You are given a string \(s\) of length \(n\), which consists of the first \(17\) lowercase Latin letters {\(a\), \(b\), \(c\), \(\ldots\), \(p\), \(q\)} and question marks. And \(q\) queries. Each query is defined by a set of pairwise distinct lowercase first \(17\) letters of the Latin alphabet, which can be used to replace the question marks in the string \(s\).The answer to the query is the sum of the number of distinct substrings that are palindromes over all strings that can be obtained from the original string \(s\) by replacing question marks with available characters. The answer must be calculated modulo \(998\,244\,353\).Pay attention! Two substrings are different when their start and end positions in the string are different. So, the number of different substrings that are palindromes for the string aba will be \(4\): a, b, a, aba.Consider examples of replacing question marks with letters. For example, from the string aba??ee when querying {\(a\), \(b\)} you can get the strings ababaee or abaaaee but you cannot get the strings pizza, abaee, abacaba, aba?fee, aba47ee, or abatree.Recall that a palindrome is a string that reads the same from left to right as from right to left.
|
The first line contains a single integer \(n\) (\(1 \le n \le 1\,000\)) β the length of the string \(s\).The second line contains the string \(s\), which consists of \(n\) lowercase Latin letters and question marks. It is guaranteed that all letters in the string belong to the set {\(a\), \(b\), \(c\), \(\ldots\), \(p\), \(q\)}.The third line contains a single integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) β the number of queries.This is followed by \(q\) lines, each containing a single line \(t\) β a set of characters that can replace question marks (\(1 \le |t| \le 17\)). It is guaranteed that all letters in the string belong to the set {\(a\), \(b\), \(c\), \(\ldots\), \(p\), \(q\)} and occur at most once.
|
For each query print one number β the total numbers of palindromic substrings in all strings that can be obtained from the string \(s\), modulo \(998\,244\,353\).
|
Consider the first example and the first query in it. We can get only one string as a result of replacing the question marks β abaaaba. It has the following palindrome substrings: a β substring [\(1\); \(1\)]. b β substring [\(2\); \(2\)]. a β substring [\(3\); \(3\)]. a β substring [\(4\); \(4\)]. a β substring [\(5\); \(5\)]. b β substring [\(6\); \(6\)]. a β substring [\(7\); \(7\)]. aa β substring [\(3\); \(4\)]. aa β substring [\(4\); \(5\)]. aba β substring [\(1\); \(3\)]. aaa β substring [\(3\); \(5\)]. aba β substring [\(5\); \(7\)]. baaab β substring [\(2\); \(6\)]. abaaaba β substring [\(1\); \(7\)]. In the third request, we get 4 lines: abaaaba, abababa, abbaaba, abbbaba.
|
Input: 7 ab??aba 8 a b ab abc abcd abcde abcdef abcdefg | Output: 14 13 55 105 171 253 351 465
|
Expert
| 4 | 1,419 | 716 | 162 | 16 |
479 |
D
|
479D
|
D. Long Jumps
| 1,700 |
binary search; greedy; implementation
|
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l).Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 β€ i β€ j β€ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler.
|
The first line contains four positive space-separated integers n, l, x, y (2 β€ n β€ 105, 2 β€ l β€ 109, 1 β€ x < y β€ l) β the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly.The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin.
|
In the first line print a single non-negative integer v β the minimum number of marks that you need to add on the ruler.In the second line print v space-separated integers p1, p2, ..., pv (0 β€ pi β€ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them.
|
In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark.In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks.In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills.
|
Input: 3 250 185 2300 185 250 | Output: 1230
|
Medium
| 3 | 1,526 | 375 | 379 | 4 |
2,103 |
C
|
2103C
|
C. Median Splits
| 1,600 |
binary search; greedy; implementation; sortings
|
The median of an array \(b_1, b_2, \ldots b_m\), written as \(\operatorname{med}(b_1, b_2, \ldots, b_m)\), is the \(\left\lceil \frac{m}{2} \right\rceil\)-th\(^{\text{β}}\) smallest element of array \(b\).You are given an array of integers \(a_1, a_2, \ldots, a_n\) and an integer \(k\). You need to determine whether there exists a pair of indices \(1 \le l < r < n\) such that:$$$\(\operatorname{med}(\operatorname{med}(a_1, a_2, \ldots, a_l), \operatorname{med}(a_{l+1}, a_{l+2}, \ldots, a_r), \operatorname{med}(a_{r+1}, a_{r+2}, \ldots, a_n)) \le k.\)\(In other words, determine whether it is possible to split the array into three contiguous subarrays\)^{\text{β }}\( such that the median of the three subarray medians is less than or equal to \)k\(.\)^{\text{β}}$$$\(\lceil x \rceil\) is the ceiling function which returns the least integer greater than or equal to \(x\).\(^{\text{β }}\)An array \(x\) is a subarray of an array \(y\) if \(x\) can be obtained from \(y\) by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(k\) (\(3 \le n \le 2 \cdot 10^5\), \(1 \le k \le 10^9\)) β the length of the array \(a\) and the constant \(k\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each testcase, output ""YES"" if such a split exists, and ""NO"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
In the first and second test case, the only possible partition of the array into three contiguous subarrays is \([3]\), \([2]\), \([1]\). Their respective medians are \(3\), \(2\), and \(1\). The median of the three subarray medians is \(\operatorname{med}(3, 2, 1) = 2\). Therefore, the answer for the first test case is ""YES"" since \(2\le 2\), while the answer for the second test case is ""NO"" since \(2 > 1\).In the third test case, it can be proven that no partition satisfies the constraints.In the fourth test case, one of the partitions satisfying the constraints is \([10, 7]\), \([12, 16, 3, 15]\), \([6, 11]\). The respective medians of subarrays are \(7\), \(12\), and \(6\). The median of the three subarray medians is \(\operatorname{med}(7, 12, 6) = 7 \le k\), hence this partition satisfies the constraints.In the fifth test case, one of the partitions satisfying the constraints is \([7, 11]\), \([12, 4]\), \([9, 17]\). The respective medians of the subarrays are \(7\), \(4\), and \(9\). The median of the three subarray medians is \(\operatorname{med}(7, 4, 9) = 7 \le k\), hence this partition satisfies the constraints.In the sixth test case, the only possible partition of the array into three contiguous subarrays is \([1000]\), \([10^9]\), \([1000]\). The respective medians of the subarrays are \(1000\), \(10^9\), and \(1000\). The median of the three subarray medians is \(\operatorname{med}(1000, 10^9, 1000) = 1000 \le k\), hence this partition satisfies the constraints.
|
Input: 63 23 2 13 13 2 16 38 5 3 1 6 48 710 7 12 16 3 15 6 116 87 11 12 4 9 173 5000000001000 1000000000 1000 | Output: YES NO NO YES YES YES
|
Medium
| 4 | 1,113 | 576 | 240 | 21 |
2,066 |
F
|
2066F
|
F. Curse
| 3,300 |
constructive algorithms; dp; math
|
You are given two arrays of integers: \(a_1, a_2, \ldots, a_n\) and \(b_1, b_2, \ldots, b_m\).You need to determine if it is possible to transform array \(a\) into array \(b\) using the following operation several (possibly, zero) times. Among all non-empty subarrays\(^{\text{β}}\) of \(a\), choose any with the maximum sum, and replace this subarray with an arbitrary non-empty integer array.If it is possible, you need to construct any possible sequence of operations. Constraint: in your answer, the sum of the lengths of the arrays used as replacements must not exceed \(n + m\) across all operations. The numbers must not exceed \(10^9\) in absolute value.\(^{\text{β}}\)An array \(a\) is a subarray of an array \(b\) if \(a\) can be obtained from \(b\) by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
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 two integers \(n, m\) (\(1 \le n, m \le 500\)) β the lengths of arrays \(a\) and \(b\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^6 \le a_i \le 10^6\)) β the elements of array \(a\).The third line of each test case contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(-10^6 \le b_i \le 10^6\)) β the elements of array \(b\).It is guaranteed that the sum of the values of \(n\) across all test cases does not exceed \(500\).It is guaranteed that the sum of the values of \(m\) across all test cases does not exceed \(500\).
|
For each test case, output \(-1\) if it is impossible to transform array \(a\) into array \(b\).Otherwise, in the first line, output the number of operations \(0 \leq q \leq n + m\). Then output the operations in the following format in the order they are performed.In the first line of each operation, print three numbers \(l, r, k\) (\(1 \leq l \leq r \leq |a|\)). In the second line, print \(k\) integers \(c_1 \ldots c_k\), which means replacing the segment \(a_l, \ldots, a_r\) with the array \(c_1, \ldots, c_k\).The sum of \(k\) across all \(q\) operations must not exceed \(n + m\). Additionally, it must hold that \(-10^9 \leq c_i \leq 10^9\).You do not need to minimize the number of operations.
|
In the first test, the initial array is modified as follows:$$$\( [2, -3, 2, 0] \to [2, -3, -3] \to [-3, -3, -3] \to [-3, -7, -3] \to [-3, -7, 0] \)$$$You may choose to output empty lines or not. Empty lines in the example are added for convenience.
|
Input: 34 32 -3 2 0-3 -7 02 1-2 -225 4-5 9 -3 5 -9-6 6 -1 -9 | Output: 4 3 4 1 -3 1 1 1 -3 2 2 1 -7 3 3 1 0 -1 3 2 4 1 -5 1 1 1 -6 2 2 2 6 -1
|
Master
| 3 | 897 | 773 | 705 | 20 |
72 |
F
|
72F
|
F. Oil
| 1,900 |
*special; greedy; math
|
After the nationalization of the oil industry, Dr. Mosaddegh wants to dig some oil wells to extract all the oil in Persian Gulf. But Persian Gulf is huge and has an infinite amount of oil. So Dr. Mosaddegh works only on a rectangular plane of size n Γ m of the Persian Gulf. Each of the cells in this rectangle either contains an infinite amount of oil or nothing.Two cells are considered adjacent if and only if they have a common edge, a path is a sequence c1, c2, ..., cx of cells so that all of them contain oil and for each i, ci is adjacent to ci - 1 and ci + 1 (if they exist). Two cells are considered connected to each other if and only if there exists a path between them. If we dig a well in a certain cell, we can extract oil from all the cells that are connected to it by oil paths. It is not allowed to dig wells on empty cells.Dr. Mosaddegh also knows that in Persian Gulf, the empty cells form rows and columns. I. e. if some cell is empty, then it's column is completely empty or it's row is completely empty, or both.Help Dr. Mosaddegh find out how many wells he has to dig to access all the oil in that region.
|
In the first line there are two positive integers n and m (1 β€ n, m β€ 100).In the second line there is an integer t (0 β€ t β€ n), the number of empty rows. t distinct positive integers follow, these are the numbers of empty rows and are in range [1, n].In the second line there is an integer s (0 β€ s β€ m) that shows the number of columns not having any oil. s distinct positive integers follow, these are the numbers of empty columns and are in range of [1, m].Note that rows are numbered from 1 to n (from top to bottom) and columns are numbered from 1 to m (from left to right).
|
A single integer, the minimum number of wells that Dr. Mossadegh has to dig.This is actually finding how many regions are made by removing the given rows and columns.
|
Input: 2 31 21 2 | Output: 2
|
Hard
| 3 | 1,129 | 580 | 166 | 0 |
|
1,540 |
B
|
1540B
|
B. Tree Array
| 2,300 |
brute force; combinatorics; dp; graphs; math; probabilities; trees
|
You are given a tree consisting of \(n\) nodes. You generate an array from the tree by marking nodes one by one.Initially, when no nodes are marked, a node is equiprobably chosen and marked from the entire tree. After that, until all nodes are marked, a node is equiprobably chosen and marked from the set of unmarked nodes with at least one edge to a marked node. It can be shown that the process marks all nodes in the tree. The final array \(a\) is the list of the nodes' labels in order of the time each node was marked.Find the expected number of inversions in the array that is generated by the tree and the aforementioned process.The number of inversions in an array \(a\) is the number of pairs of indices \((i, j)\) such that \(i < j\) and \(a_i > a_j\). For example, the array \([4, 1, 3, 2]\) contains \(4\) inversions: \((1, 2)\), \((1, 3)\), \((1, 4)\), \((3, 4)\).
|
The first line contains a single integer \(n\) (\(2 \le n \le 200\)) β the number of nodes in the tree.The next \(n - 1\) lines each contains two integers \(x\) and \(y\) (\(1 \le x, y \le n\); \(x \neq y\)), denoting an edge between node \(x\) and \(y\).It's guaranteed that the given edges form a tree.
|
Output the expected number of inversions in the generated array 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 to \(p \cdot q^{-1} \bmod M\). In other words, output such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{M}\).
|
This is the tree from the first sample: For the first sample, the arrays are almost fixed. If node \(2\) is chosen initially, then the only possible array is \([2, 1, 3]\) (\(1\) inversion). If node \(3\) is chosen initially, then the only possible array is \([3, 1, 2]\) (\(2\) inversions). If node \(1\) is chosen initially, the arrays \([1, 2, 3]\) (\(0\) inversions) and \([1, 3, 2]\) (\(1\) inversion) are the only possibilities and equiprobable. In total, the expected number of inversions is \(\frac{1}{3}\cdot 1 + \frac{1}{3} \cdot 2 + \frac{1}{3} \cdot (\frac{1}{2} \cdot 0 + \frac{1}{2} \cdot 1) = \frac{7}{6}\). \(166666669 \cdot 6 = 7 \pmod {10^9 + 7}\), so the answer is \(166666669\).This is the tree from the second sample: This is the tree from the third sample:
|
Input: 3 1 2 1 3 | Output: 166666669
|
Expert
| 7 | 878 | 304 | 432 | 15 |
1,175 |
G
|
1175G
|
G. Yet Another Partiton Problem
| 3,000 |
data structures; divide and conquer; dp; geometry; two pointers
|
You are given array \(a_1, a_2, \dots, a_n\). You need to split it into \(k\) subsegments (so every element is included in exactly one subsegment).The weight of a subsegment \(a_l, a_{l+1}, \dots, a_r\) is equal to \((r - l + 1) \cdot \max\limits_{l \le i \le r}(a_i)\). The weight of a partition is a total weight of all its segments.Find the partition of minimal weight.
|
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^4\), \(1 \le k \le \min(100, n)\)) β the length of the array \(a\) and the number of subsegments in the partition.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2 \cdot 10^4\)) β the array \(a\).
|
Print single integer β the minimal weight among all possible partitions.
|
The optimal partition in the first example is next: \(6\) \(1\) \(7\) \(\bigg|\) \(4\).The optimal partition in the second example is next: \(6\) \(\bigg|\) \(1\) \(\bigg|\) \(7\) \(4\).One of the optimal partitions in the third example is next: \(5\) \(\bigg|\) \(1\) \(5\) \(\bigg|\) \(1\) \(\bigg|\) \(5\).
|
Input: 4 2 6 1 7 4 | Output: 25
|
Master
| 5 | 372 | 309 | 72 | 11 |
313 |
C
|
313C
|
C. Ilya and Matrix
| 1,400 |
constructive algorithms; greedy; implementation; sortings
|
Ilya is a very good-natured lion. He likes maths. Of all mathematical objects, his favourite one is matrices. Now he's faced a complicated matrix problem he needs to solve.He's got a square 2n Γ 2n-sized matrix and 4n integers. You need to arrange all these numbers in the matrix (put each number in a single individual cell) so that the beauty of the resulting matrix with numbers is maximum.The beauty of a 2n Γ 2n-sized matrix is an integer, obtained by the following algorithm: Find the maximum element in the matrix. Let's denote it as m. If n = 0, then the beauty of the matrix equals m. Otherwise, a matrix can be split into 4 non-intersecting 2n - 1 Γ 2n - 1-sized submatrices, then the beauty of the matrix equals the sum of number m and other four beauties of the described submatrices. As you can see, the algorithm is recursive.Help Ilya, solve the problem and print the resulting maximum beauty of the matrix.
|
The first line contains integer 4n (1 β€ 4n β€ 2Β·106). The next line contains 4n integers ai (1 β€ ai β€ 109) β the numbers you need to arrange in the 2n Γ 2n-sized matrix.
|
On a single line print the maximum value of the beauty of the described matrix.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
Consider the second sample. You need to arrange the numbers in the matrix as follows:1 23 4Then the beauty of the matrix will equal: 4 + 1 + 2 + 3 + 4 = 14.
|
Input: 113 | Output: 13
|
Easy
| 4 | 922 | 168 | 226 | 3 |
130 |
J
|
130J
|
J. Date calculation
| 1,800 |
*special
|
In Gregorian calendar a typical year consists of 365 days and 12 months. The numbers of days in the months are: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31. If year index is divisible by 400, or divisible by 4 but not by 100, the year becomes leap year, with one extra day in the second month (the one which typically has 28 days).You are given the index of the year and the index of the day in the year. Find out the date of this day (day and month it will fall upon).
|
The first line of input contains the year index, between 1600 and 2400, inclusive. The second line contains the day index, between 1 and 366, inclusive. It is guaranteed that the day index will be valid for this year, i.e., day 366 will occur only in a leap year.
|
Output the index of the day and the index of the month, separated with a space.
|
All indexes are 1-based.
|
Input: 2011324 | Output: 20 11
|
Medium
| 1 | 472 | 263 | 79 | 1 |
1,916 |
B
|
1916B
|
B. Two Divisors
| 1,000 |
constructive algorithms; math; number theory
|
A certain number \(1 \le x \le 10^9\) is chosen. You are given two integers \(a\) and \(b\), which are the two largest divisors of the number \(x\). At the same time, the condition \(1 \le a < b < x\) is satisfied.For the given numbers \(a\), \(b\), you need to find the value of \(x\).\(^{\dagger}\) The number \(y\) is a divisor of the number \(x\) if there is an integer \(k\) such that \(x = y \cdot k\).
|
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then follows the description of the test cases.The only line of each test cases contains two integers \(a\), \(b\) (\(1 \le a < b \le 10^9\)).It is guaranteed that \(a\), \(b\) are the two largest divisors for some number \(1 \le x \le 10^9\).
|
For each test case, output the number \(x\), such that \(a\) and \(b\) are the two largest divisors of the number \(x\).If there are several answers, print any of them.
|
For the first test case, all divisors less than \(6\) are equal to \([1, 2, 3]\), among them the two largest will be \(2\) and \(3\).For the third test case, all divisors less than \(33\) are equal to \([1, 3, 11]\), among them the two largest will be \(3\) and \(11\).For the fifth test case, all divisors less than \(20\) are equal to \([1, 2, 4, 5, 10]\), among them the two largest will be \(5\) and \(10\).For the sixth test case, all divisors less than \(12\) are equal to \([1, 2, 3, 4, 6]\), among them the two largest will be \(4\) and \(6\).
|
Input: 82 31 23 111 55 104 63 9250000000 500000000 | Output: 6 4 33 25 20 12 27 1000000000
|
Beginner
| 3 | 408 | 383 | 168 | 19 |
1,692 |
H
|
1692H
|
H. Gambling
| 1,700 |
data structures; dp; greedy; math
|
Marian is at a casino. The game at the casino works like this.Before each round, the player selects a number between \(1\) and \(10^9\). After that, a dice with \(10^9\) faces is rolled so that a random number between \(1\) and \(10^9\) appears. If the player guesses the number correctly their total money is doubled, else their total money is halved. Marian predicted the future and knows all the numbers \(x_1, x_2, \dots, x_n\) that the dice will show in the next \(n\) rounds. He will pick three integers \(a\), \(l\) and \(r\) (\(l \leq r\)). He will play \(r-l+1\) rounds (rounds between \(l\) and \(r\) inclusive). In each of these rounds, he will guess the same number \(a\). At the start (before the round \(l\)) he has \(1\) dollar.Marian asks you to determine the integers \(a\), \(l\) and \(r\) (\(1 \leq a \leq 10^9\), \(1 \leq l \leq r \leq n\)) such that he makes the most money at the end.Note that during halving and multiplying there is no rounding and there are no precision errors. So, for example during a game, Marian could have money equal to \(\dfrac{1}{1024}\), \(\dfrac{1}{128}\), \(\dfrac{1}{2}\), \(1\), \(2\), \(4\), etc. (any value of \(2^t\), where \(t\) is an integer of any sign).
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2\cdot 10^5\)) β the number of rounds.The second line of each test case contains \(n\) integers \(x_1, x_2, \dots, x_n\) (\(1 \leq x_i \leq 10^9\)), where \(x_i\) is the number that will fall on the dice in the \(i\)-th round.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot10^5\).
|
For each test case, output three integers \(a\), \(l\), and \(r\) such that Marian makes the most amount of money gambling with his strategy. If there are multiple answers, you may output any of them.
|
For the first test case, the best choice is \(a=4\), \(l=1\), \(r=5\), and the game would go as follows. Marian starts with one dollar. After the first round, he ends up with \(2\) dollars because the numbers coincide with the chosen one. After the second round, he ends up with \(4\) dollars because the numbers coincide again. After the third round, he ends up with \(2\) dollars because he guesses \(4\) even though \(3\) is the correct choice. After the fourth round, he ends up with \(4\) dollars again. In the final round, he ends up \(8\) dollars because he again guessed correctly. There are many possible answers for the second test case, but it can be proven that Marian will not end up with more than \(2\) dollars, so any choice with \(l = r\) with the appropriate \(a\) is acceptable.
|
Input: 454 4 3 4 4511 1 11 1 1111000000000108 8 8 9 9 6 6 9 6 6 | Output: 4 1 5 1 2 2 1000000000 1 1 6 6 10
|
Medium
| 4 | 1,214 | 496 | 200 | 16 |
1,728 |
G
|
1728G
|
G. Illumination
| 2,700 |
binary search; bitmasks; brute force; combinatorics; dp; math; two pointers
|
Consider a segment \([0, d]\) of the coordinate line. There are \(n\) lanterns and \(m\) points of interest in this segment.For each lantern, you can choose its power β an integer between \(0\) and \(d\) (inclusive). A lantern with coordinate \(x\) illuminates the point of interest with coordinate \(y\) if \(|x - y|\) is less than or equal to the power of the lantern.A way to choose the power values for all lanterns is considered valid if every point of interest is illuminated by at least one lantern.You have to process \(q\) queries. Each query is represented by one integer \(f_i\). To answer the \(i\)-th query, you have to: add a lantern on coordinate \(f_i\); calculate the number of valid ways to assign power values to all lanterns, and print it modulo \(998244353\); remove the lantern you just added.
|
The first line contains three integers \(d\), \(n\) and \(m\) (\(4 \le d \le 3 \cdot 10^5\); \(1 \le n \le 2 \cdot 10^5\); \(1 \le m \le 16\)) β the size of the segment, the number of lanterns and the number of points of interest, respectively.The second line contains \(n\) integers \(l_1, l_2, \dots, l_n\) (\(1 \le l_i \le d - 1\)), where \(l_i\) is the coordinate of the \(i\)-th lantern.The third line contains \(m\) integers \(p_1, p_2, \dots, p_m\) (\(1 \le p_i \le d - 1\)), where \(p_i\) is the coordinate of the \(i\)-th point of interest.The fourth line contains one integer \(q\) (\(1 \le q \le 5 \cdot 10^5\)) β the number of queries.The fifth line contains \(q\) integers \(f_1, f_2, \dots, f_q\) (\(1 \le f_i \le d - 1\)), where \(f_i\) is the integer representing the \(i\)-th query.Additional constraint on the input: during the processing of each query, no coordinate contains more than one object (i. e. there cannot be two or more lanterns with the same coordinate, two or more points of interest with the same coordinate, or a lantern and a point of interest with the same coordinate).
|
For each query, print one integer β the answer to it, taken modulo \(998244353\).
|
Input: 6 1 1 4 3 3 2 1 5 | Output: 48 47 47
|
Master
| 7 | 815 | 1,106 | 81 | 17 |
|
802 |
E
|
802E
|
E. Marmots (medium)
| 2,200 |
math
|
Good job! Now that Heidi is able to distinguish between Poisson and uniform distributions, she is in a good position to actually estimate the populations.Can you help Heidi estimate each village's population?
|
Same as the easy version.
|
Output one line per village, in the same order as provided in the input, containing your (integer) population estimate.Your answer is considered correct if it is an integer that falls into the interval , where P is the real population of the village, used to create the distribution (either Poisson or uniform) from which the marmots drew their answers.
|
Hard
| 1 | 208 | 25 | 353 | 8 |
||
1,041 |
A
|
1041A
|
A. Heist
| 800 |
greedy; implementation; sortings
|
There was an electronic store heist last night.All keyboards which were in the store yesterday were numbered in ascending order from some integer number \(x\). For example, if \(x = 4\) and there were \(3\) keyboards in the store, then the devices had indices \(4\), \(5\) and \(6\), and if \(x = 10\) and there were \(7\) of them then the keyboards had indices \(10\), \(11\), \(12\), \(13\), \(14\), \(15\) and \(16\).After the heist, only \(n\) keyboards remain, and they have indices \(a_1, a_2, \dots, a_n\). Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither \(x\) nor the number of keyboards in the store before the heist.
|
The first line contains single integer \(n\) \((1 \le n \le 1\,000)\) β the number of keyboards in the store that remained after the heist.The second line contains \(n\) distinct integers \(a_1, a_2, \dots, a_n\) \((1 \le a_i \le 10^{9})\) β the indices of the remaining keyboards. The integers \(a_i\) are given in arbitrary order and are pairwise distinct.
|
Print the minimum possible number of keyboards that have been stolen if the staff remember neither \(x\) nor the number of keyboards in the store before the heist.
|
In the first example, if \(x=8\) then minimum number of stolen keyboards is equal to \(2\). The keyboards with indices \(9\) and \(11\) were stolen during the heist.In the second example, if \(x=4\) then nothing was stolen during the heist.
|
Input: 410 13 12 8 | Output: 2
|
Beginner
| 3 | 679 | 358 | 163 | 10 |
1,549 |
B
|
1549B
|
B. Gregor and the Pawn Game
| 800 |
dfs and similar; dp; flows; graph matchings; graphs; greedy; implementation
|
There is a chessboard of size \(n\) by \(n\). The square in the \(i\)-th row from top and \(j\)-th column from the left is labelled \((i,j)\).Currently, Gregor has some pawns in the \(n\)-th row. There are also enemy pawns in the \(1\)-st row. On one turn, Gregor moves one of his pawns. A pawn can move one square up (from \((i,j)\) to \((i-1,j)\)) if there is no pawn in the destination square. Additionally, a pawn can move one square diagonally up (from \((i,j)\) to either \((i-1,j-1)\) or \((i-1,j+1)\)) if and only if there is an enemy pawn in that square. The enemy pawn is also removed.Gregor wants to know what is the maximum number of his pawns that can reach row \(1\)?Note that only Gregor takes turns in this game, and the enemy pawns never move. Also, when Gregor's pawn reaches row \(1\), it is stuck and cannot make any further moves.
|
The first line of the input contains one integer \(t\) (\(1\le t\le 2\cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.Each test case consists of three lines. The first line contains a single integer \(n\) (\(2\le n\le 2\cdot{10}^{5}\)) β the size of the chessboard.The second line consists of a string of binary digits of length \(n\), where a \(1\) in the \(i\)-th position corresponds to an enemy pawn in the \(i\)-th cell from the left, and \(0\) corresponds to an empty cell.The third line consists of a string of binary digits of length \(n\), where a \(1\) in the \(i\)-th position corresponds to a Gregor's pawn in the \(i\)-th cell from the left, and \(0\) corresponds to an empty cell.It is guaranteed that the sum of \(n\) across all test cases is less than \(2\cdot{10}^{5}\).
|
For each test case, print one integer: the maximum number of Gregor's pawns which can reach the \(1\)-st row.
|
In the first example, Gregor can simply advance all \(3\) of his pawns forward. Thus, the answer is \(3\).In the second example, Gregor can guarantee that all \(4\) of his pawns reach the enemy row, by following the colored paths as demonstrated in the diagram below. Remember, only Gregor takes turns in this ""game""! In the third example, Gregor's only pawn is stuck behind the enemy pawn, and cannot reach the end.In the fourth example, Gregor has no pawns, so the answer is clearly \(0\).
|
Input: 4 3 000 111 4 1111 1111 3 010 010 5 11001 00000 | Output: 3 4 0 0
|
Beginner
| 7 | 851 | 809 | 109 | 15 |
754 |
E
|
754E
|
E. Dasha and cyclic table
| 2,600 |
bitmasks; brute force; fft; strings; trees
|
Dasha is fond of challenging puzzles: Rubik's Cube 3 Γ 3 Γ 3, 4 Γ 4 Γ 4, 5 Γ 5 Γ 5 and so on. This time she has a cyclic table of size n Γ m, and each cell of the table contains a lowercase English letter. Each cell has coordinates (i, j) (0 β€ i < n, 0 β€ j < m). The table is cyclic means that to the right of cell (i, j) there is the cell , and to the down there is the cell .Dasha has a pattern as well. A pattern is a non-cyclic table of size r Γ c. Each cell is either a lowercase English letter or a question mark. Each cell has coordinates (i, j) (0 β€ i < r, 0 β€ j < c).The goal of the puzzle is to find all the appearance positions of the pattern in the cyclic table.We say that the cell (i, j) of cyclic table is an appearance position, if for every pair (x, y) such that 0 β€ x < r and 0 β€ y < c one of the following conditions holds: There is a question mark in the cell (x, y) of the pattern, or The cell of the cyclic table equals to the cell (x, y) of the pattern. Dasha solved this puzzle in no time, as well as all the others she ever tried. Can you solve it?.
|
The first line contains two integers n and m (1 β€ n, m β€ 400) β the cyclic table sizes.Each of the next n lines contains a string of m lowercase English characters β the description of the cyclic table.The next line contains two integers r and c (1 β€ r, c β€ 400) β the sizes of the pattern.Each of the next r lines contains a string of c lowercase English letter and/or characters '?' β the description of the pattern.
|
Print n lines. Each of the n lines should contain m characters. Each of the characters should equal '0' or '1'.The j-th character of the i-th (0-indexed) line should be equal to '1', in case the cell (i, j) is an appearance position, otherwise it should be equal to '0'.
|
Input: 5 7qcezchshhedywqwikywqyqckrqztbqexcxz3 2??yw?q | Output: 00001000001001000000000000000000000
|
Expert
| 5 | 1,074 | 418 | 270 | 7 |
|
1,001 |
E
|
1001E
|
E. Distinguish Bell states
| 1,600 |
*special
|
You are given two qubits which are guaranteed to be in one of the Bell states: Your task is to perform necessary operations and measurements to figure out which state it was and to return the index of that state (0 for , 1 for etc.). The state of the qubits after the operations does not matter.
|
You have to implement an operation which takes an array of two qubits as an input and returns an integer. Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (qs : Qubit[]) : Int { body { // your code here } }}
|
Medium
| 1 | 295 | 305 | 0 | 10 |
|||
813 |
E
|
813E
|
E. Army Creation
| 2,200 |
binary search; data structures
|
As you might remember from our previous rounds, Vova really likes computer games. Now he is playing a strategy game known as Rage of Empires.In the game Vova can hire n different warriors; ith warrior has the type ai. Vova wants to create a balanced army hiring some subset of warriors. An army is called balanced if for each type of warrior present in the game there are not more than k warriors of this type in the army. Of course, Vova wants his army to be as large as possible.To make things more complicated, Vova has to consider q different plans of creating his army. ith plan allows him to hire only warriors whose numbers are not less than li and not greater than ri.Help Vova to determine the largest size of a balanced army for each plan.Be aware that the plans are given in a modified way. See input section for details.
|
The first line contains two integers n and k (1 β€ n, k β€ 100000).The second line contains n integers a1, a2, ... an (1 β€ ai β€ 100000).The third line contains one integer q (1 β€ q β€ 100000).Then q lines follow. ith line contains two numbers xi and yi which represent ith plan (1 β€ xi, yi β€ n).You have to keep track of the answer to the last plan (let's call it last). In the beginning last = 0. Then to restore values of li and ri for the ith plan, you have to do the following: li = ((xi + last) mod n) + 1; ri = ((yi + last) mod n) + 1; If li > ri, swap li and ri.
|
Print q numbers. ith number must be equal to the maximum size of a balanced army when considering ith plan.
|
In the first example the real plans are: 1 2 1 6 6 6 2 4 4 6
|
Input: 6 21 1 1 2 2 251 64 31 12 62 6 | Output: 24132
|
Hard
| 2 | 832 | 566 | 107 | 8 |
717 |
B
|
717B
|
B. R3D3βs Summer Adventure
| 2,700 |
dp; greedy
|
R3D3 spent some time on an internship in MDCS. After earning enough money, he decided to go on a holiday somewhere far, far away. He enjoyed suntanning, drinking alcohol-free cocktails and going to concerts of popular local bands. While listening to ""The White Buttons"" and their hit song ""Dacan the Baker"", he met another robot for whom he was sure is the love of his life. Well, his summer, at least. Anyway, R3D3 was too shy to approach his potential soulmate, so he decided to write her a love letter. However, he stumbled upon a problem. Due to a terrorist threat, the Intergalactic Space Police was monitoring all letters sent in the area. Thus, R3D3 decided to invent his own alphabet, for which he was sure his love would be able to decipher.There are n letters in R3D3βs alphabet, and he wants to represent each letter as a sequence of '0' and '1', so that no letterβs sequence is a prefix of another letter's sequence. Since the Intergalactic Space Communications Service has lately introduced a tax for invented alphabets, R3D3 must pay a certain amount of money for each bit in his alphabetβs code (check the sample test for clarifications). He is too lovestruck to think clearly, so he asked you for help.Given the costs c0 and c1 for each '0' and '1' in R3D3βs alphabet, respectively, you should come up with a coding for the alphabet (with properties as above) with minimum total cost.
|
The first line of input contains three integers n (2 β€ n β€ 108), c0 and c1 (0 β€ c0, c1 β€ 108) β the number of letters in the alphabet, and costs of '0' and '1', respectively.
|
Output a single integer β minimum possible total a cost of the whole alphabet.
|
There are 4 letters in the alphabet. The optimal encoding is ""00"", ""01"", ""10"", ""11"". There are 4 zeroes and 4 ones used, so the total cost is 4Β·1 + 4Β·2 = 12.
|
Input: 4 1 2 | Output: 12
|
Master
| 2 | 1,404 | 174 | 78 | 7 |
111 |
E
|
111E
|
E. Petya and Rectangle
| 2,900 |
Little Petya loves playing with rectangles. Mom bought Petya a rectangle divided into cells n Γ m in size (containing n rows, m columns). Petya marked two different cells of the rectangle and now he is solving the following task:Let's define a simple path between those two cells as a sequence of distinct cells a1, a2, ..., ak, where a1 and ak are the two marked cells. Besides, ai and ai + 1 are side-neighboring cells of the path (1 β€ i < k). Let's denote the path length as number k (the sequence length). Petya's task is to find the longest simple path's length and to print the path. Help him.
|
The first line contains space-separated integers n and m (4 β€ n, m β€ 1000) β the number of rows and the number of columns in the rectangle, correspondingly. The second line contains space-separated integers x1 and y1 β the coordinates of the first marked cell. The third line contains space-separated integers x2 y2 β the coordinates of the second marked cell (1 < x1, x2 < n, 1 < y1, y2 < m, x1 β x2, y1 β y2).The coordinates of a marked cell are a pair of integers x y, where x represents the row's number and y represents the column's number. The rows are numbered from top to bottom with consecutive integers from 1 to n. The columns are numbered from the left to the right by consecutive integers from 1 to m.It is guaranteed that the marked cells are not positioned in one row or column.
|
In the first line print the length of the found path β k. In the next lines print k pairs of integers, one per line β coordinates of the cells that constitute the found path in the order, in which they follow in the path (the path must go from cell (x1, y1) to cell (x2, y2)). If there are several solutions, print any of them.
|
The statement test is described in the picture:
|
Input: 4 42 23 3 | Output: 152 21 21 12 13 14 14 24 34 43 42 41 41 32 33 3
|
Master
| 0 | 599 | 793 | 327 | 1 |
|
93 |
A
|
93A
|
A. Frames
| 1,700 |
implementation
|
Throughout Igor K.'s life he has had many situations worthy of attention. We remember the story with the virus, the story of his mathematical career and of course, his famous programming achievements. However, one does not always adopt new hobbies, one can quit something as well.This time Igor K. got disappointed in one of his hobbies: editing and voicing videos. Moreover, he got disappointed in it so much, that he decided to destroy his secret archive for good. Igor K. use Pindows XR operation system which represents files and folders by small icons. At that, m icons can fit in a horizontal row in any window.Igor K.'s computer contains n folders in the D: disk's root catalog. The folders are numbered from 1 to n in the order from the left to the right and from top to bottom (see the images). At that the folders with secret videos have numbers from a to b inclusive. Igor K. wants to delete them forever, at that making as few frame selections as possible, and then pressing Shift+Delete exactly once. What is the minimum number of times Igor K. will have to select the folder in order to select folders from a to b and only them? Let us note that if some selected folder is selected repeatedly, then it is deselected. Each selection possesses the shape of some rectangle with sides parallel to the screen's borders.
|
The only line contains four integers n, m, a, b (1 β€ n, m β€ 109, 1 β€ a β€ b β€ n). They are the number of folders in Igor K.'s computer, the width of a window and the numbers of the first and the last folders that need to be deleted.
|
Print a single number: the least possible number of times Igor K. will have to select the folders using frames to select only the folders with numbers from a to b.
|
The images below illustrate statement tests.The first test:In this test we can select folders 3 and 4 with out first selection, folders 5, 6, 7, 8 with our second selection and folder 9 with our third, last selection.The second test:In this test we can first select all folders in the first row (2, 3, 4, 5), then β all other ones.
|
Input: 11 4 3 9 | Output: 3
|
Medium
| 1 | 1,328 | 231 | 163 | 0 |
724 |
B
|
724B
|
B. Batch Sort
| 1,500 |
brute force; greedy; implementation; math
|
You are given a table consisting of n rows and m columns.Numbers in each row form a permutation of integers from 1 to m.You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order.You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order.
|
The first line of the input contains two integers n and m (1 β€ n, m β€ 20) β the number of rows and the number of columns in the given table. Each of next n lines contains m integers β elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m.
|
If there is a way to obtain the identity permutation in each row by following the given rules, print ""YES"" (without quotes) in the only line of the output. Otherwise, print ""NO"" (without quotes).
|
In the first sample, one can act in the following way: Swap second and third columns. Now the table is 1 2 3 4 1 4 3 2 In the second row, swap the second and the fourth elements. Now the table is 1 2 3 4 1 2 3 4
|
Input: 2 41 3 2 41 3 4 2 | Output: YES
|
Medium
| 4 | 637 | 292 | 199 | 7 |
1,325 |
A
|
1325A
|
A. EhAb AnD gCd
| 800 |
constructive algorithms; greedy; number theory
|
You are given a positive integer \(x\). Find any such \(2\) positive integers \(a\) and \(b\) such that \(GCD(a,b)+LCM(a,b)=x\).As a reminder, \(GCD(a,b)\) is the greatest integer that divides both \(a\) and \(b\). Similarly, \(LCM(a,b)\) is the smallest integer such that both \(a\) and \(b\) divide it.It's guaranteed that the solution always exists. If there are several such pairs \((a, b)\), you can output any of them.
|
The first line contains a single integer \(t\) \((1 \le t \le 100)\) β the number of testcases.Each testcase consists of one line containing a single integer, \(x\) \((2 \le x \le 10^9)\).
|
For each testcase, output a pair of positive integers \(a\) and \(b\) (\(1 \le a, b \le 10^9)\) such that \(GCD(a,b)+LCM(a,b)=x\). It's guaranteed that the solution always exists. If there are several such pairs \((a, b)\), you can output any of them.
|
In the first testcase of the sample, \(GCD(1,1)+LCM(1,1)=1+1=2\).In the second testcase of the sample, \(GCD(6,4)+LCM(6,4)=2+12=14\).
|
Input: 2 2 14 | Output: 1 1 6 4
|
Beginner
| 3 | 424 | 188 | 251 | 13 |
1,945 |
D
|
1945D
|
D. Seraphim the Owl
| 1,300 |
dp; greedy
|
The guys lined up in a queue of \(n\) people, starting with person number \(i = 1\), to ask Serafim the Owl about the meaning of life. Unfortunately, Kirill was very busy writing the legend for this problem, so he arrived a little later and stood at the end of the line after the \(n\)-th person. Kirill is completely dissatisfied with this situation, so he decided to bribe some people ahead of him.For the \(i\)-th person in the queue, Kirill knows two values: \(a_i\) and \(b_i\). If at the moment Kirill is standing at position \(i\), then he can choose any position \(j\) such that \(j < i\) and exchange places with the person at position \(j\). In this case, Kirill will have to pay him \(a_j\) coins. And for each \(k\) such that \(j < k < i\), Kirill will have to pay \(b_k\) coins to the person at position \(k\). Kirill can perform this action any number of times.Kirill is thrifty, so he wants to spend as few coins as possible, but he doesn't want to wait too long, so Kirill believes he should be among the first \(m\) people in line.Help Kirill determine the minimum number of coins he will have to spend in order to not wait too long.
|
Each test consists of several sets of input data. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then follows the description of the test case.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le m \le n \le 200\,000\)) β the number of people in the queue besides Kirill and the maximum allowable final position of Kirill, respectively.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) separated by spaces (\(1 \le a_i \le 10^9\)).The third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) separated by spaces (\(1 \le b_i \le 10^9\)).It is guaranteed that the sum of the values of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the minimum number of coins Kirill needs to spend.
|
Input: 44 27 3 6 94 3 8 56 26 9 7 1 8 35 8 8 1 4 17 77 2 9 2 6 5 99 1 10 7 1 4 92 12 31 1 | Output: 14 22 9 3
|
Easy
| 2 | 1,150 | 739 | 96 | 19 |
|
505 |
C
|
505C
|
C. Mr. Kitayuta, the Treasure Hunter
| 1,900 |
dfs and similar; dp; two pointers
|
The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The islands are evenly spaced along a line, numbered from 0 to 30000 from the west to the east. These islands are known to contain many treasures. There are n gems in the Shuseki Islands in total, and the i-th gem is located on island pi.Mr. Kitayuta has just arrived at island 0. With his great jumping ability, he will repeatedly perform jumps between islands to the east according to the following process: First, he will jump from island 0 to island d. After that, he will continue jumping according to the following rule. Let l be the length of the previous jump, that is, if his previous jump was from island prev to island cur, let l = cur - prev. He will perform a jump of length l - 1, l or l + 1 to the east. That is, he will jump to island (cur + l - 1), (cur + l) or (cur + l + 1) (if they exist). The length of a jump must be positive, that is, he cannot perform a jump of length 0 when l = 1. If there is no valid destination, he will stop jumping. Mr. Kitayuta will collect the gems on the islands visited during the process. Find the maximum number of gems that he can collect.
|
The first line of the input contains two space-separated integers n and d (1 β€ n, d β€ 30000), denoting the number of the gems in the Shuseki Islands and the length of the Mr. Kitayuta's first jump, respectively.The next n lines describe the location of the gems. The i-th of them (1 β€ i β€ n) contains a integer pi (d β€ p1 β€ p2 β€ ... β€ pn β€ 30000), denoting the number of the island that contains the i-th gem.
|
Print the maximum number of gems that Mr. Kitayuta can collect.
|
In the first sample, the optimal route is 0 β 10 (+1 gem) β 19 β 27 (+2 gems) β ...In the second sample, the optimal route is 0 β 8 β 15 β 21 β 28 (+1 gem) β 36 (+1 gem) β 45 (+1 gem) β 55 (+1 gem) β 66 (+1 gem) β 78 (+1 gem) β ...In the third sample, the optimal route is 0 β 7 β 13 β 18 (+1 gem) β 24 (+2 gems) β 30 (+1 gem) β ...
|
Input: 4 1010212727 | Output: 3
|
Hard
| 3 | 1,175 | 409 | 63 | 5 |
113 |
C
|
113C
|
C. Double Happiness
| 2,200 |
brute force; math; number theory
|
On the math lesson a teacher asked each pupil to come up with his own lucky numbers. As a fan of number theory Peter chose prime numbers. Bob was more original. He said that number t is his lucky number, if it can be represented as: t = a2 + b2, where a, b are arbitrary positive integers.Now, the boys decided to find out how many days of the interval [l, r] (l β€ r) are suitable for pair programming. They decided that the day i (l β€ i β€ r) is suitable for pair programming if and only if the number i is lucky for Peter and lucky for Bob at the same time. Help the boys to find the number of such days.
|
The first line of the input contains integer numbers l, r (1 β€ l, r β€ 3Β·108).
|
In the only line print the number of days on the segment [l, r], which are lucky for Peter and Bob at the same time.
|
Input: 3 5 | Output: 1
|
Hard
| 3 | 605 | 77 | 116 | 1 |
|
1,184 |
A1
|
1184A1
|
A1. Heidi Learns Hashing (Easy)
| 1,200 |
brute force; math; number theory
|
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.The first hashing function she designed is as follows.Given two positive integers \((x, y)\) she defines \(H(x,y):=x^2+2xy+x+1\).Now, Heidi wonders if the function is reversible. That is, given a positive integer \(r\), can you find a pair \((x, y)\) (of positive integers) such that \(H(x, y) = r\)?If multiple such pairs exist, output the one with smallest possible \(x\). If there is no such pair, output ""NO"".
|
The first and only line contains an integer \(r\) (\(1 \le r \le 10^{12}\)).
|
Output integers \(x, y\) such that \(H(x,y) = r\) and \(x\) is smallest possible, or ""NO"" if no such pair exists.
|
Input: 19 | Output: 1 8
|
Easy
| 3 | 957 | 76 | 115 | 11 |
|
222 |
E
|
222E
|
E. Decoding Genome
| 1,900 |
dp; matrices
|
Recently a top secret mission to Mars has taken place. As a result, scientists managed to obtain some information about the Martian DNA. Now we know that any Martian DNA contains at most m different nucleotides, numbered from 1 to m. Special characteristics of the Martian DNA prevent some nucleotide pairs from following consecutively in this chain. For example, if the nucleotide 1 and nucleotide 2 can not follow consecutively in the Martian DNA, then the chain of nucleotides [1, 2] is not a valid chain of Martian DNA, but the chain of nucleotides [2, 1] can be a valid chain (if there is no corresponding restriction). The number of nucleotide pairs that can't follow in the DNA chain consecutively, is k. The needs of gene research required information about the quantity of correct n-long chains of the Martian DNA. Your task is to write a program that will calculate this value.
|
The first line contains three space-separated integers n, m, k (1 β€ n β€ 1015, 1 β€ m β€ 52, 0 β€ k β€ m2).Next k lines contain two characters each, without a space between them, representing a forbidden nucleotide pair. The first character represents the first nucleotide in the forbidden pair, the second character represents the second nucleotide.The nucleotides with assigned numbers from 1 to 26 are represented by English alphabet letters from ""a"" to ""z"" (1 is an ""a"", 2 is a ""b"", ..., 26 is a ""z""). Nucleotides with assigned numbers from 27 to 52 are represented by English alphabet letters from ""A"" to ""Z"" (27 is an ""A"", 28 is a ""B"", ..., 52 is a ""Z"").It is guaranteed that each forbidden pair occurs at most once in the input. It is guaranteed that nucleotide's numbers in all forbidden pairs cannot be more than m. Note that order is important in nucleotide pairs.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
|
Print a single integer β the sought number modulo 1000000007 (109 + 7).
|
In the second test case all possible three-nucleotide DNAs are permitted. Each nucleotide can take one of three values, thus in total there are 27 distinct three nucleotide DNAs.In the third test sample we cannot make any DNA of two nucleotides β the only possible nucleotide ""a"" cannot occur two times consecutively.
|
Input: 3 3 2abba | Output: 17
|
Hard
| 2 | 887 | 1,032 | 71 | 2 |
1,917 |
E
|
1917E
|
E. Construct Matrix
| 2,500 |
constructive algorithms; math
|
You are given an even integer \(n\) and an integer \(k\). Your task is to construct a matrix of size \(n \times n\) consisting of numbers \(0\) and \(1\) in such a way that the following conditions are true, or report that it is impossible: the sum of all the numbers in the matrix is exactly \(k\); the bitwise \(\texttt{XOR}\) of all the numbers in the row \(i\) is the same for each \(i\); the bitwise \(\texttt{XOR}\) of all the numbers in the column \(j\) is the same for each \(j\).
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 130\)) β the number of test cases. The description of the test cases follows.Each test case is described by a single line, which contains two integers \(n\) and \(k\) (\(2 \leq n \leq 1000\), \(n\) is even, \(0 \leq k \leq n^2\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2000\).
|
For each test case, output \(\texttt{Yes}\) if it's possible to construct a matrix that satisfies all of the problem's conditions, and \(\texttt{No}\) otherwise.If it is possible to construct a matrix, the \(i\)-th of the next \(n\) lines should contain \(n\) integers representing the elements in the \(i\)-th row of the matrix.
|
In the first example, all conditions are satisfied: the sum of all the numbers in the matrix is exactly \(0\); the bitwise \(\texttt{XOR}\) of all the numbers in the row \(i\) is \(0\) for each \(i\); the bitwise \(\texttt{XOR}\) of all the numbers in the column \(j\) is \(0\) for each \(j\). In the third example, it can be shown that it's impossible to find a matrix satisfying all the problem's conditions.
|
Input: 5 4 0 6 6 6 5 4 2 6 36 | Output: Yes 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Yes 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 No No Yes 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 1 1 1
|
Expert
| 2 | 488 | 421 | 329 | 19 |
1,444 |
B
|
1444B
|
B. Divide and Sum
| 1,900 |
combinatorics; math; sortings
|
You are given an array \(a\) of length \(2n\). Consider a partition of array \(a\) into two subsequences \(p\) and \(q\) of length \(n\) each (each element of array \(a\) should be in exactly one subsequence: either in \(p\) or in \(q\)).Let's sort \(p\) in non-decreasing order, and \(q\) in non-increasing order, we can denote the sorted versions by \(x\) and \(y\), respectively. Then the cost of a partition is defined as \(f(p, q) = \sum_{i = 1}^n |x_i - y_i|\).Find the sum of \(f(p, q)\) over all correct partitions of array \(a\). Since the answer might be too big, print its remainder modulo \(998244353\).
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 150\,000\)).The second line contains \(2n\) integers \(a_1, a_2, \ldots, a_{2n}\) (\(1 \leq a_i \leq 10^9\)) β elements of array \(a\).
|
Print one integer β the answer to the problem, modulo \(998244353\).
|
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence \(p\) are different.In the first example, there are two correct partitions of the array \(a\): \(p = [1]\), \(q = [4]\), then \(x = [1]\), \(y = [4]\), \(f(p, q) = |1 - 4| = 3\); \(p = [4]\), \(q = [1]\), then \(x = [4]\), \(y = [1]\), \(f(p, q) = |4 - 1| = 3\). In the second example, there are six valid partitions of the array \(a\): \(p = [2, 1]\), \(q = [2, 1]\) (elements with indices \(1\) and \(2\) in the original array are selected in the subsequence \(p\)); \(p = [2, 2]\), \(q = [1, 1]\); \(p = [2, 1]\), \(q = [1, 2]\) (elements with indices \(1\) and \(4\) are selected in the subsequence \(p\)); \(p = [1, 2]\), \(q = [2, 1]\); \(p = [1, 1]\), \(q = [2, 2]\); \(p = [2, 1]\), \(q = [2, 1]\) (elements with indices \(3\) and \(4\) are selected in the subsequence \(p\)).
|
Input: 1 1 4 | Output: 6
|
Hard
| 3 | 615 | 199 | 68 | 14 |
1,774 |
D
|
1774D
|
D. Same Count One
| 1,600 |
brute force; constructive algorithms; greedy; implementation; two pointers
|
ChthollyNotaSeniorious received a special gift from AquaMoon: \(n\) binary arrays of length \(m\). AquaMoon tells him that in one operation, he can choose any two arrays and any position \(pos\) from \(1\) to \(m\), and swap the elements at positions \(pos\) in these arrays.He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of \(1\)s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it!If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output \(-1\).
|
The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 2\cdot 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \leq n \leq 10^5\), \(2 \leq m \leq 10^5\)).The \(i\)-th of the following \(n\) lines contains \(m\) integers \(a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}\) \((0 \le a_{i, j} \le 1)\) β the elements of the \(i\)-th array. It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(10^6\).
|
For each test case, if the objective is not achievable, output \(-1\). Otherwise, in the first line output \(k\) \((0 \le k \le mn)\) β the minimum number of operations required.The \(i\)-th of the following \(k\) lines should contain \(3\) integers, \(x_i, y_i, z_i\) \((1 \le x_i, y_i \le n, 1 \le z_i \le m)\), which describe an operation that swap \(a_{x_i, z_i}, a_{y_i, z_i}\): swap the \(z_i\)-th number of the \(x_i\)-th and \(y_i\)-th arrays.
|
In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become \([0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]\), each of them contains exactly two \(1\)s.
|
Input: 33 41 1 1 00 0 1 01 0 0 14 31 0 00 1 10 0 10 0 02 20 00 1 | Output: 1 2 1 1 1 4 2 2 -1
|
Medium
| 5 | 644 | 542 | 451 | 17 |
1,774 |
B
|
1774B
|
B. Coloring
| 1,500 |
constructive algorithms; greedy; math
|
Cirno_9baka has a paper tape with \(n\) cells in a row on it. As he thinks that the blank paper tape is too dull, he wants to paint these cells with \(m\) kinds of colors. For some aesthetic reasons, he thinks that the \(i\)-th color must be used exactly \(a_i\) times, and for every \(k\) consecutive cells, their colors have to be distinct. Help Cirno_9baka to figure out if there is such a way to paint the cells.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10\,000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains three integers \(n\), \(m\), \(k\) (\(1 \leq k \leq n \leq 10^9\), \(1 \leq m \leq 10^5\), \(m \leq n\)). Here \(n\) denotes the number of cells, \(m\) denotes the number of colors, and \(k\) means that for every \(k\) consecutive cells, their colors have to be distinct.The second line of each test case contains \(m\) integers \(a_1, a_2, \cdots , a_m\) (\(1 \leq a_i \leq n\)) β the numbers of times that colors have to be used. It's guaranteed that \(a_1 + a_2 + \ldots + a_m = n\).It is guaranteed that the sum of \(m\) over all test cases does not exceed \(10^5\).
|
For each test case, output ""YES"" if there is at least one possible coloring scheme; otherwise, output ""NO"".You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", and ""yEs"" will all be recognized as positive answers).
|
In the first test case, there is no way to color the cells satisfying all the conditions.In the second test case, we can color the cells as follows: \((1, 2, 1, 2, 3, 4, 3, 4, 5, 6, 5, 6)\). For any \(2\) consecutive cells, their colors are distinct.
|
Input: 212 6 21 1 1 1 1 712 6 22 2 2 2 2 2 | Output: NO YES
|
Medium
| 3 | 416 | 753 | 246 | 17 |
1,829 |
B
|
1829B
|
B. Blank Space
| 800 |
implementation
|
You are given a binary array \(a\) of \(n\) elements, a binary array is an array consisting only of \(0\)s and \(1\)s. A blank space is a segment of consecutive elements consisting of only \(0\)s. Your task is to find the length of the longest blank space.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 100\)) β the length of the array.The second line of each test case contains \(n\) space-separated integers \(a_i\) (\(0 \leq a_i \leq 1\)) β the elements of the array.
|
For each test case, output a single integer β the length of the longest blank space.
|
Input: 551 0 0 1 040 1 1 11031 1 191 0 0 0 1 0 0 0 1 | Output: 2 1 1 0 3
|
Beginner
| 1 | 256 | 348 | 84 | 18 |
|
2,010 |
A
|
2010A
|
A. Alternating Sum of Numbers
| 800 |
brute force; implementation; math
|
You are given a sequence of integers. Output the alternating sum of this sequence. In other words, output \(a_1 - a_2 + a_3 - a_4 + a_5 - \dots\). That is, the signs of plus and minus alternate, starting with a plus.
|
The first line of the test contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Then follow \(t\) test cases.The first line of each test case contains one integer \(n\) (\(1 \le n \le 50\)) β the length of the sequence. The second line of the test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 100\)).
|
Output \(t\) lines. For each test case, output the required alternating sum of the numbers.
|
Input: 441 2 3 1711002100 10053 1 4 1 5 | Output: -15 100 0 10
|
Beginner
| 3 | 216 | 351 | 91 | 20 |
|
44 |
F
|
44F
|
F. BerPaint
| 2,700 |
geometry; graphs
|
Anfisa the monkey got disappointed in word processors as they aren't good enough at reflecting all the range of her emotions, that's why she decided to switch to graphics editors. Having opened the BerPaint, she saw a white rectangle W Γ H in size which can be painted on. First Anfisa learnt to navigate the drawing tool which is used to paint segments and quickly painted on that rectangle a certain number of black-colored segments. The resulting picture didn't seem bright enough to Anfisa, that's why she turned her attention to the ""fill"" tool which is used to find a point on the rectangle to paint and choose a color, after which all the area which is the same color as the point it contains, is completely painted the chosen color. Having applied the fill several times, Anfisa expressed her emotions completely and stopped painting. Your task is by the information on the painted segments and applied fills to find out for every color the total area of the areas painted this color after all the fills.
|
The first input line has two integers W and H (3 β€ W, H β€ 104) β the sizes of the initially white rectangular painting area. The second line contains integer n β the number of black segments (0 β€ n β€ 100). On the next n lines are described the segments themselves, each of which is given by coordinates of their endpoints x1, y1, x2, y2 (0 < x1, x2 < W, 0 < y1, y2 < H). All segments have non-zero length. The next line contains preset number of fills m (0 β€ m β€ 100). Each of the following m lines defines the fill operation in the form of ""x y color"", where (x, y) are the coordinates of the chosen point (0 < x < W, 0 < y < H), and color β a line of lowercase Latin letters from 1 to 15 symbols in length, determining the color. All coordinates given in the input are integers. Initially the rectangle is ""white"" in color, whereas the segments are drawn ""black"" in color.
|
For every color present in the final picture print on the single line the name of the color and the total area of areas painted that color with an accuracy of 10 - 6. Print the colors in any order.
|
Initially the black segments painted by Anfisa can also be painted a color if any of the chosen points lays on the segment. The segments have areas equal to 0. That is why if in the final picture only parts of segments is painted some color, then the area, painted the color is equal to 0.
|
Input: 4 561 1 1 31 3 3 33 3 3 13 1 1 11 3 3 11 1 3 322 1 red2 2 blue | Output: blue 0.00000000white 20.00000000
|
Master
| 2 | 1,014 | 880 | 197 | 0 |
1,254 |
E
|
1254E
|
E. Send Tree to Charlie
| 3,300 |
combinatorics; dfs and similar; dsu; trees
|
Christmas was knocking on the door, and our protagonist, Bob, was preparing a spectacular present for his long-time second best friend Charlie. As chocolate boxes are lame, he decided to decorate a tree instead. Bob's tree can be represented as an undirected connected graph with \(n\) nodes (numbered \(1\) to \(n\)) and \(n-1\) edges. Initially, Bob placed a decoration with label \(i\) on node \(i\), for each \(1 \le i \le n\). However, as such a simple arrangement is lame, he decided to shuffle the decorations a bit. Formally, Bob did the following steps: First, he listed the \(n-1\) edges in some order. Then, he considered the edges one by one in that order. For each edge \((u, v)\), he swapped the decorations of node \(u\) with the one of node \(v\).After finishing, Bob seemed satisfied with the arrangement, so he went to sleep.The next morning, Bob wakes up only to find out that his beautiful arrangement has been ruined! Last night, Bob's younger brother Bobo dropped some of the decorations on the floor while he was playing with the tree. Fortunately, no decorations were lost, so Bob can repair the tree in no time. However, he completely forgets how the tree looked like yesterday. Therefore, given the labels of the decorations still on the tree, Bob wants to know the number of possible configurations of the tree. As the result can be quite large, Bob will be happy if you can output the result modulo \(1000000007\) (\(10^9+7\)). Note that, it is possible that there exists no possible configurations.
|
The first line contains a single integer \(n\) (\(2 \leq n \leq 500\,000\)) β the number of nodes.Each of the next \(n - 1\) lines contains two integers \(u\) and \(v\) (\(1 \leq u, v \leq n\)), denoting that there is an edge connecting two nodes \(u\) and \(v\). It is guaranteed that the given edges form a tree.The last line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le n\)). For each \(i\), \(a_i = 0\) means that the decoration of node \(i\) has been dropped on the floor. Otherwise, \(a_i\) is the label of the decoration of node \(i\). It is guaranteed that no label appears more than once.
|
Output the number of possible configurations modulo \(1000000007\) (\(10^9+7\)).
|
In the first example, the possible configurations of the tree are \([2, 4, 1, 3]\) and \([3, 4, 2, 1]\).In the second example, note that while there are \(4! = 24\) possible permutations of the edges, each of them results in a possible configuration, there are only \(12\) different configurations.In the third example, it is easy to see that the decoration \(1\) cannot stay at node \(1\) after the swaps.
|
Input: 4 3 4 2 4 4 1 0 4 0 0 | Output: 2
|
Master
| 4 | 1,527 | 619 | 80 | 12 |
1,931 |
F
|
1931F
|
F. Chat Screenshots
| 1,700 |
combinatorics; dfs and similar; graphs
|
There are \(n\) people in the programming contest chat. Chat participants are ordered by activity, but each person sees himself at the top of the list.For example, there are \(4\) participants in the chat, and their order is \([2, 3, 1, 4]\). Then \(1\)-st user sees the order \([1, 2, 3, 4]\). \(2\)-nd user sees the order \([2, 3, 1, 4]\). \(3\)-rd user sees the order \([3, 2, 1, 4]\). \(4\)-th user sees the order \([4, 2, 3, 1]\). \(k\) people posted screenshots in the chat, which show the order of participants shown to this user. The screenshots were taken within a short period of time, and the order of participants has not changed. Your task is to determine whether there is a certain order that all screenshots correspond to.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of input test cases. The descriptions of test cases follow.The first line of the description of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5, n \cdot k \le 2 \cdot 10^5\)) β the number of chat participants and the number of participants who posted screenshots.The following \(k\) lines contain descriptions of screenshots posted by the participants.The \(i\)-th row contains \(n\) integers \(a_{ij}\) each (\(1 \le a_{ij} \le n\), all \(a_{ij}\) are different) β the order of participants shown to the participant \(a_{i0}\), where \(a_{i0}\) β the author of the screenshot. You can show that in the screenshot description it will always be at the top of the list.It is guaranteed that the sum of \(n \cdot k\) for all test cases does not exceed \(2 \cdot 10^5\). It is also guaranteed that all the authors of the screenshots are different.
|
Output \(t\) lines, each of which is the answer to the corresponding test case. As an answer, output ""YES"" if there exists at least one order of participants, under which all \(k\) screenshots could have been obtained. Otherwise, output ""NO"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
Input: 105 11 2 3 4 54 41 2 3 42 3 1 43 2 1 44 2 3 16 21 3 5 2 4 66 3 5 2 1 43 31 2 32 3 13 2 110 21 2 3 4 5 6 7 8 9 1010 9 8 7 6 5 4 3 2 11 115 21 2 3 5 42 1 3 5 43 33 1 22 3 11 3 25 43 5 1 4 22 5 1 4 31 5 4 3 25 1 4 3 23 31 3 22 1 33 2 1 | Output: YES YES YES YES NO YES YES YES YES NO
|
Medium
| 3 | 737 | 965 | 407 | 19 |
|
332 |
E
|
332E
|
E. Binary Key
| 2,400 |
dp; greedy; implementation
|
Let's assume that p and q are strings of positive length, called the container and the key correspondingly, string q only consists of characters 0 and 1. Let's take a look at a simple algorithm that extracts message s from the given container p:i = 0;j = 0;s = <>;while i is less than the length of the string p{ if q[j] == 1, then add to the right of string s character p[i]; increase variables i, j by one; if the value of the variable j equals the length of the string q, then j = 0; }In the given pseudocode i, j are integer variables, s is a string, '=' is an assignment operator, '==' is a comparison operation, '[]' is the operation of obtaining the string character with the preset index, '<>' is an empty string. We suppose that in all strings the characters are numbered starting from zero. We understand that implementing such algorithm is quite easy, so your task is going to be slightly different. You need to construct the lexicographically minimum key of length k, such that when it is used, the algorithm given above extracts message s from container p (otherwise find out that such key doesn't exist).
|
The first two lines of the input are non-empty strings p and s (1 β€ |p| β€ 106, 1 β€ |s| β€ 200), describing the container and the message, correspondingly. The strings can contain any characters with the ASCII codes from 32 to 126, inclusive.The third line contains a single integer k (1 β€ k β€ 2000) β the key's length.
|
Print the required key (string of length k, consisting only of characters 0 and 1). If the key doesn't exist, print the single character 0.
|
String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such integer r (0 β€ r < min(p, q)) that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. Symbols are compared according to their ASCII codes.
|
Input: abacabaaba6 | Output: 100001
|
Expert
| 3 | 1,118 | 317 | 139 | 3 |
1,187 |
G
|
1187G
|
G. Gang Up
| 2,500 |
flows; graphs
|
The leader of some very secretive organization has decided to invite all other members to a meeting. All members of the organization live in the same town which can be represented as \(n\) crossroads connected by \(m\) two-directional streets. The meeting will be held in the leader's house near the crossroad \(1\). There are \(k\) members of the organization invited to the meeting; \(i\)-th of them lives near the crossroad \(a_i\). All members of the organization receive the message about the meeting at the same moment and start moving to the location where the meeting is held. In the beginning of each minute each person is located at some crossroad. He or she can either wait a minute at this crossroad, or spend a minute to walk from the current crossroad along some street to another crossroad (obviously, it is possible to start walking along the street only if it begins or ends at the current crossroad). In the beginning of the first minute each person is at the crossroad where he or she lives. As soon as a person reaches the crossroad number \(1\), he or she immediately comes to the leader's house and attends the meeting.Obviously, the leader wants all other members of the organization to come up as early as possible. But, since the organization is very secretive, the leader does not want to attract much attention. Let's denote the discontent of the leader as follows initially the discontent is \(0\); whenever a person reaches the crossroad number \(1\), the discontent of the leader increases by \(c \cdot x\), where \(c\) is some fixed constant, and \(x\) is the number of minutes it took the person to reach the crossroad number \(1\); whenever \(x\) members of the organization walk along the same street at the same moment in the same direction, \(dx^2\) is added to the discontent, where \(d\) is some fixed constant. This is not cumulative: for example, if two persons are walking along the same street in the same direction at the same moment, then \(4d\) is added to the discontent, not \(5d\). Before sending a message about the meeting, the leader can tell each member of the organization which path they should choose and where they should wait. Help the leader to establish a plan for every member of the organization so they all reach the crossroad \(1\), and the discontent is minimized.
|
The first line of the input contains five integer numbers \(n\), \(m\), \(k\), \(c\) and \(d\) (\(2 \le n \le 50\), \(n - 1 \le m \le 50\), \(1 \le k, c, d \le 50\)) β the number of crossroads, the number of streets, the number of persons invited to the meeting and the constants affecting the discontent, respectively.The second line contains \(k\) numbers \(a_1\), \(a_2\), ..., \(a_k\) (\(2 \le a_i \le n\)) β the crossroads where the members of the organization live.Then \(m\) lines follow, each denoting a bidirectional street. Each line contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\), \(x_i \ne y_i\)) denoting a street connecting crossroads \(x_i\) and \(y_i\). There may be multiple streets connecting the same pair of crossroads.It is guaranteed that every crossroad can be reached from every other crossroad using the given streets.
|
Print one integer: the minimum discontent of the leader after everyone reaches crossroad \(1\).
|
The best course of action in the first test is the following: the first person goes along the street \(2\) to the crossroad \(2\), then goes along the street \(1\) to the crossroad \(1\) and attends the meeting; the second person waits one minute on the crossroad \(3\), then goes along the street \(2\) to the crossroad \(2\), then goes along the street \(1\) to the crossroad \(1\) and attends the meeting; the third person waits two minutes on the crossroad \(3\), then goes along the street \(2\) to the crossroad \(2\), then goes along the street \(1\) to the crossroad \(1\) and attends the meeting; the fourth person waits three minutes on the crossroad \(3\), then goes along the street \(2\) to the crossroad \(2\), then goes along the street \(1\) to the crossroad \(1\) and attends the meeting.
|
Input: 3 2 4 2 3 3 3 3 3 1 2 2 3 | Output: 52
|
Expert
| 2 | 2,328 | 864 | 95 | 11 |
346 |
C
|
346C
|
C. Number Transformation II
| 2,200 |
greedy; math
|
You are given a sequence of positive integers x1, x2, ..., xn and two non-negative integers a and b. Your task is to transform a into b. To do that, you can perform the following moves: subtract 1 from the current a; subtract a mod xi (1 β€ i β€ n) from the current a. Operation a mod xi means taking the remainder after division of number a by number xi.Now you want to know the minimum number of moves needed to transform a into b.
|
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n space-separated integers x1, x2, ..., xn (2 β€ xi β€ 109). The third line contains two integers a and b (0 β€ b β€ a β€ 109, a - b β€ 106).
|
Print a single integer β the required minimum number of moves needed to transform number a into number b.
|
Input: 33 4 530 17 | Output: 6
|
Hard
| 2 | 431 | 218 | 105 | 3 |
|
587 |
B
|
587B
|
B. Duff in Beach
| 2,100 |
dp
|
While Duff was resting in the beach, she accidentally found a strange array b0, b1, ..., bl - 1 consisting of l positive integers. This array was strange because it was extremely long, but there was another (maybe shorter) array, a0, ..., an - 1 that b can be build from a with formula: bi = ai mod n where a mod b denoted the remainder of dividing a by b. Duff is so curious, she wants to know the number of subsequences of b like bi1, bi2, ..., bix (0 β€ i1 < i2 < ... < ix < l), such that: 1 β€ x β€ k For each 1 β€ j β€ x - 1, For each 1 β€ j β€ x - 1, bij β€ bij + 1. i.e this subsequence is non-decreasing. Since this number can be very large, she want to know it modulo 109 + 7.Duff is not a programmer, and Malek is unavailable at the moment. So she asked for your help. Please tell her this number.
|
The first line of input contains three integers, n, l and k (1 β€ n, k, n Γ k β€ 106 and 1 β€ l β€ 1018).The second line contains n space separated integers, a0, a1, ..., an - 1 (1 β€ ai β€ 109 for each 0 β€ i β€ n - 1).
|
Print the answer modulo 1 000 000 007 in one line.
|
In the first sample case, . So all such sequences are: , , , , , , , , and .
|
Input: 3 5 35 9 1 | Output: 10
|
Hard
| 1 | 799 | 212 | 50 | 5 |
1,539 |
B
|
1539B
|
B. Love Song
| 800 |
dp; implementation; strings
|
Petya once wrote a sad love song and shared it to Vasya. The song is a string consisting of lowercase English letters. Vasya made up \(q\) questions about this song. Each question is about a subsegment of the song starting from the \(l\)-th letter to the \(r\)-th letter. Vasya considers a substring made up from characters on this segment and repeats each letter in the subsegment \(k\) times, where \(k\) is the index of the corresponding letter in the alphabet. For example, if the question is about the substring ""abbcb"", then Vasya repeats letter 'a' once, each of the letters 'b' twice, letter 'c"" three times, so that the resulting string is ""abbbbcccbb"", its length is \(10\). Vasya is interested about the length of the resulting string.Help Petya find the length of each string obtained by Vasya.
|
The first line contains two integers \(n\) and \(q\) (\(1\leq n\leq 100\,000\), \(1\leq q \leq 100\,000\)) β the length of the song and the number of questions. The second line contains one string \(s\) β the song, consisting of \(n\) lowercase letters of English letters.Vasya's questions are contained in the next \(q\) lines. Each line contains two integers \(l\) and \(r\) (\(1 \leq l \leq r \leq n\)) β the bounds of the question.
|
Print \(q\) lines: for each question print the length of the string obtained by Vasya.
|
In the first example Vasya is interested in three questions. In the first question Vasya considers the substring ""aba"", that transforms to ""abba"", so the answer is equal to \(4\). In the second question Vasya considers ""baca"", that transforms to ""bbaccca"", so the answer is \(7\). In the third question Vasya considers the string ""abacaba"",that transforms to ""abbacccabba"" of length \(11\).
|
Input: 7 3 abacaba 1 3 2 5 1 7 | Output: 4 7 11
|
Beginner
| 3 | 811 | 435 | 86 | 15 |
1,805 |
E
|
1805E
|
E. There Should Be a Lot of Maximums
| 2,300 |
brute force; data structures; dfs and similar; dp; trees; two pointers
|
You are given a tree (a connected graph without cycles). Each vertex of the tree contains an integer. Let's define the \(\mathrm{MAD}\) (maximum double) parameter of the tree as the maximum integer that occurs in the vertices of the tree at least \(2\) times. If no number occurs in the tree more than once, then we assume \(\mathrm{MAD}=0\).Note that if you remove an edge from the tree, it splits into two trees. Let's compute the \(\mathrm{MAD}\) parameters of the two trees and take the maximum of the two values. Let the result be the value of the deleted edge.For each edge, find its value. Note that we don't actually delete any edges from the tree, the values are to be found independently.
|
The first line contains one integer \(n\) (\(2 \le n \le 10^5\)) β the number of vertices in the tree.Each of the next \(n - 1\) lines contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\)) β the ends of an edge of the tree. It's guaranteed that the given edges form a valid tree.The last line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the numbers in the vertices.
|
For each edge in the input order, print one number β the maximum of the \(\mathrm{MAD}\) parameters of the two trees obtained after removing the given edge from the initial tree.
|
In the first example, after removing edge \((1, 2)\) no number repeats \(2\) times in any of the resulting subtrees, so the answer is \(\max(0, 0)=0\).After removing edge \((2, 3)\), in the bigger subtree, \(1\) is repeated twice, and \(2\) is repeated twice, so the \(\mathrm{MAD}\) of this tree is \(2\).After removing edge \((2, 4)\), in the bigger subtree, only the number \(1\) is repeated, and in the second subtree, only one number appears, so the answer is \(1\).In the second example, if edge \(1 \leftrightarrow 4\) is not removed, then one of the subtrees will have two \(1\), so the answer β \(1\). And if edge \(1 \leftrightarrow 4\) is deleted, both subtrees have no repeating values, so the answer is \(0\).
|
Input: 5 1 2 2 3 2 4 1 5 2 1 3 2 1 | Output: 0 2 1 2
|
Expert
| 6 | 698 | 405 | 178 | 18 |
852 |
H
|
852H
|
H. Bob and stages
| 3,000 |
dp; geometry
|
The citizens of BubbleLand are celebrating their 10th anniversary so they decided to organize a big music festival. Bob got a task to invite N famous singers who would sing on the fest. He was too busy placing stages for their performances that he totally forgot to write the invitation e-mails on time, and unfortunately he only found K available singers. Now there are more stages than singers, leaving some of the stages empty. Bob would not like if citizens of BubbleLand noticed empty stages and found out that he was irresponsible.Because of that he decided to choose exactly K stages that form a convex set, make large posters as edges of that convex set and hold festival inside. While those large posters will make it impossible for citizens to see empty stages outside Bob still needs to make sure they don't see any of the empty stages inside that area.Since lots of people are coming, he would like that the festival area is as large as possible. Help him calculate the maximum area that he could obtain respecting the conditions. If there is no such area, the festival cannot be organized and the answer is 0.00.
|
The first line of input contains two integers N (3 β€ N β€ 200) and K (3 β€ K β€ min(N, 50)), separated with one empty space, representing number of stages and number of singers, respectively.Each of the next N lines contains two integers Xi and Yi (0 β€ Xi, Yi β€ 106) representing the coordinates of the stages. There are no three or more collinear stages.
|
Output contains only one line with one number, rounded to exactly two decimal places: the maximal festival area. Rounding is performed so that 0.5 and more rounds up and everything else rounds down.
|
Example explanation: From all possible convex polygon with 4 vertices and no other vertex inside, the largest is one with points (0, 0), (2, 1), (4, 4) and (1, 5).
|
Input: 5 40 03 02 14 41 5 | Output: 10.00
|
Master
| 2 | 1,125 | 352 | 198 | 8 |
587 |
C
|
587C
|
C. Duff in the Army
| 2,200 |
data structures; trees
|
Recently Duff has been a soldier in the army. Malek is her commander.Their country, Andarz Gu has n cities (numbered from 1 to n) and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.There are also m people living in Andarz Gu (numbered from 1 to m). Each person has and ID number. ID number of i - th person is i and he/she lives in city number ci. Note that there may be more than one person in a city, also there may be no people living in the city. Malek loves to order. That's why he asks Duff to answer to q queries. In each query, he gives her numbers v, u and a.To answer a query:Assume there are x people living in the cities lying on the path from city v to city u. Assume these people's IDs are p1, p2, ..., px in increasing order. If k = min(x, a), then Duff should tell Malek numbers k, p1, p2, ..., pk in this order. In the other words, Malek wants to know a minimums on that path (or less, if there are less than a people).Duff is very busy at the moment, so she asked you to help her and answer the queries.
|
The first line of input contains three integers, n, m and q (1 β€ n, m, q β€ 105).The next n - 1 lines contain the roads. Each line contains two integers v and u, endpoints of a road (1 β€ v, u β€ n, v β u).Next line contains m integers c1, c2, ..., cm separated by spaces (1 β€ ci β€ n for each 1 β€ i β€ m).Next q lines contain the queries. Each of them contains three integers, v, u and a (1 β€ v, u β€ n and 1 β€ a β€ 10).
|
For each query, print numbers k, p1, p2, ..., pk separated by spaces in one line.
|
Graph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):
|
Input: 5 4 51 31 21 44 52 1 4 34 5 61 5 25 5 102 3 35 3 1 | Output: 1 32 2 303 1 2 41 2
|
Hard
| 2 | 1,091 | 414 | 81 | 5 |
1,537 |
E1
|
1537E1
|
E1. Erase and Extend (Easy Version)
| 1,600 |
binary search; brute force; dp; greedy; hashing; implementation; string suffix structures; strings; two pointers
|
This is the easy version of the problem. The only difference is the constraints on \(n\) and \(k\). You can make hacks only if all versions of the problem are solved.You have a string \(s\), and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: \(s:=s+s\), where \(+\) denotes concatenation. You can use each operation any number of times (possibly none).Your task is to find the lexicographically smallest string of length exactly \(k\) that can be obtained by doing these operations on string \(s\).A string \(a\) is lexicographically smaller than a string \(b\) if and only if one of the following holds: \(a\) is a prefix of \(b\), but \(a\ne b\); In the first position where \(a\) and \(b\) differ, the string \(a\) has a letter that appears earlier in the alphabet than the corresponding letter in \(b\).
|
The first line contains two integers \(n\), \(k\) (\(1 \leq n, k \leq 5000\)) β the length of the original string \(s\) and the length of the desired string.The second line contains the string \(s\), consisting of \(n\) lowercase English letters.
|
Print the lexicographically smallest string of length \(k\) that can be obtained by doing the operations on string \(s\).
|
In the first test, it is optimal to make one duplication: ""dbcadabc"" \(\to\) ""dbcadabcdbcadabc"".In the second test it is optimal to delete the last \(3\) characters, then duplicate the string \(3\) times, then delete the last \(3\) characters to make the string have length \(k\).""abcd"" \(\to\) ""abc"" \(\to\) ""ab"" \(\to\) ""a"" \(\to\) ""aa"" \(\to\) ""aaaa"" \(\to\) ""aaaaaaaa"" \(\to\) ""aaaaaaa"" \(\to\) ""aaaaaa"" \(\to\) ""aaaaa"".
|
Input: 8 16 dbcadabc | Output: dbcadabcdbcadabc
|
Medium
| 9 | 865 | 246 | 121 | 15 |
639 |
D
|
639D
|
D. Bear and Contribution
| 2,400 |
data structures; greedy; sortings; two pointers
|
Codeforces is a wonderful platform and one its feature shows how much someone contributes to the community. Every registered user has contribution β an integer number, not necessarily positive. There are n registered users and the i-th of them has contribution ti.Limak is a little polar bear and he's new into competitive programming. He doesn't even have an account in Codeforces but he is able to upvote existing blogs and comments. We assume that every registered user has infinitely many blogs and comments. Limak can spend b minutes to read one blog and upvote it. Author's contribution will be increased by 5. Limak can spend c minutes to read one comment and upvote it. Author's contribution will be increased by 1. Note that it's possible that Limak reads blogs faster than comments.Limak likes ties. He thinks it would be awesome to see a tie between at least k registered users. To make it happen he is going to spend some time on reading and upvoting. After that, there should exist an integer value x that at least k registered users have contribution exactly x.How much time does Limak need to achieve his goal?
|
The first line contains four integers n, k, b and c (2 β€ k β€ n β€ 200 000, 1 β€ b, c β€ 1000) β the number of registered users, the required minimum number of users with the same contribution, time needed to read and upvote a blog, and time needed to read and upvote a comment, respectively.The second line contains n integers t1, t2, ..., tn (|ti| β€ 109) where ti denotes contribution of the i-th registered user.
|
Print the minimum number of minutes Limak will spend to get a tie between at least k registered users.
|
In the first sample, there are 4 registered users and Limak wants a tie between at least 3 of them. Limak should behave as follows. He spends 100 minutes to read one blog of the 4-th user and increase his contribution from 1 to 6. Then he spends 4Β·30 = 120 minutes to read four comments of the 2-nd user and increase his contribution from 2 to 6 (four times it was increaded by 1). In the given scenario, Limak spends 100 + 4Β·30 = 220 minutes and after that each of users 2, 3, 4 has contribution 6.In the second sample, Limak needs 30 minutes to read a blog and 100 minutes to read a comment. This time he can get 3 users with contribution equal to 12 by spending 100 + 3Β·30 = 190 minutes: Spend 2Β·30 = 60 minutes to read two blogs of the 1-st user to increase his contribution from 2 to 12. Spend 30 + 100 minutes to read one blog and one comment of the 3-rd user. His contribution will change from 6 to 6 + 5 + 1 = 12.
|
Input: 4 3 100 3012 2 6 1 | Output: 220
|
Expert
| 4 | 1,125 | 411 | 102 | 6 |
2,077 |
B
|
2077B
|
B. Finding OR Sum
| 1,900 |
bitmasks; constructive algorithms; implementation; interactive; math
|
ALTER EGO - Yuta Imai vs Qlarabelle This is an interactive problem.There are two hidden non-negative integers \(x\) and \(y\) (\(0 \leq x, y < 2^{30}\)). You can ask no more than \(2\) queries of the following form. Pick a non-negative integer \(n\) (\(0 \leq n < 2^{30}\)). The judge will respond with the value of \((n \mathbin{|} x) + (n \mathbin{|} y)\), where \(|\) denotes the bitwise OR operation. After this, the judge will give you another non-negative integer \(m\) (\(0 \leq m < 2^{30}\)). You must answer the correct value of \((m \mathbin{|} x) + (m \mathbin{|} y)\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.
|
In the first test, the interaction proceeds as follows. SolutionJuryExplanation\(\texttt{2}\)There are 2 test cases.\(\texttt{}\)In the first test case, \(x=1\) and \(y=2\).\(\texttt{0}\)\(\texttt{3}\)The solution requests \((0 \mathbin{|} 1) + (0 \mathbin{|} 2)\), and the jury responds with \(3\).\(\texttt{1}\)\(\texttt{4}\)The solution requests \((1 \mathbin{|} 1) + (1 \mathbin{|} 2)\), and the jury responds with \(4\).\(\texttt{!}\)\(\texttt{1}\)The solution requests the value of \(m\), and the jury responds with \(1\).\(\texttt{4}\)The solution knows that \((1 \mathbin{|} x) + (1 \mathbin{|} y)=4\) because of earlier queries.\(\texttt{}\)In the second test case, \(x=0\) and \(y=0\).\(\texttt{0}\)\(\texttt{0}\)The solution requests \((0 \mathbin{|} 0) + (0 \mathbin{|} 0)\), and the jury responds with \(0\).\(\texttt{!}\)\(\texttt{1}\)The solution requests the value of \(m\), and the jury responds with \(1\).\(\texttt{2}\)The solution somehow deduces that \(x=y=0\), so it responds with \((1 \mathbin{|} 0) + (1 \mathbin{|} 0)=2\). Note that the empty lines in the example input and output are for the sake of clarity and do not occur in the real interaction.
|
Input: 2 3 4 1 0 1 | Output: 0 1 ! 4 0 ! 2
|
Hard
| 5 | 580 | 161 | 0 | 20 |
|
1,095 |
B
|
1095B
|
B. Array Stabilization
| 900 |
implementation
|
You are given an array \(a\) consisting of \(n\) integer numbers.Let instability of the array be the following value: \(\max\limits_{i = 1}^{n} a_i - \min\limits_{i = 1}^{n} a_i\).You have to remove exactly one element from this array to minimize instability of the resulting \((n-1)\)-elements array. Your task is to calculate the minimum possible instability.
|
The first line of the input contains one integer \(n\) (\(2 \le n \le 10^5\)) β the number of elements in the array \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^5\)) β elements of the array \(a\).
|
Print one integer β the minimum possible instability of the array if you have to remove exactly one element from the array \(a\).
|
In the first example you can remove \(7\) then instability of the remaining array will be \(3 - 1 = 2\).In the second example you can remove either \(1\) or \(100000\) then instability of the remaining array will be \(100000 - 100000 = 0\) and \(1 - 1 = 0\) correspondingly.
|
Input: 4 1 3 3 7 | Output: 2
|
Beginner
| 1 | 361 | 255 | 129 | 10 |
1,387 |
C
|
1387C
|
C. Viruses
| 2,900 |
*special; dp; shortest paths; string suffix structures
|
The Committee for Research on Binary Viruses discovered a method of replication for a large family of viruses whose genetic codes are sequences of zeros and ones. Each virus originates from a single gene; for simplicity genes are denoted by integers from \(0\) to \(G - 1\). At each moment in time a virus is a sequence of genes. When mutation occurs, one of the genes from the sequence is replaced by a certain sequence of genes, according to the mutation table. The virus stops mutating when it consists only of genes \(0\) and \(1\).For instance, for the following mutation table: $$$\( 2 \to \langle 0\ 1 \rangle \\ 3 \to \langle 2\ 0\ 0\rangle\\ 3 \to \langle 1\ 3\rangle\\ 4 \to \langle 0\ 3\ 1\ 2\rangle\\ 5 \to \langle 2\ 1\rangle\\ 5 \to \langle 5\rangle \)\( a virus that initially consisted of a single gene \)4\(, could have mutated as follows: \)\( \langle 4 \rangle \to \langle \underline{0\ 3\ 1\ 2} \rangle \to \langle 0\ \underline{2\ 0\ 0}\ 1\ 2 \rangle \to \langle 0\ \underline{0\ 1}\ 0\ 0\ 1\ 2 \rangle \to \langle 0\ 0\ 1\ 0\ 0\ 1\ \underline{0\ 1} \rangle \)\( or in another way: \)\( \langle 4 \rangle \to \langle \underline{0\ 3\ 1\ 2} \rangle \to \langle 0\ \underline{1\ 3}\ 1\ 2 \rangle \to \langle 0\ 1\ 3\ 1\ \underline{0\ 1} \rangle \to \langle 0\ 1\ \underline{2\ 0\ 0}\ 1\ 0\ 1 \rangle \to \langle 0\ 1\ \underline{0\ 1}\ 0\ 0\ 1\ 0\ 1 \rangle \)\(Viruses are detected by antibodies that identify the presence of specific continuous fragments of zeros and ones in the viruses' codes. For example, an antibody reacting to a fragment \)\langle 0\ 0\ 1\ 0\ 0 \rangle\( will detect a virus \)\langle 0\ 0\ 1\ 0\ 0\ 1\ 0\ 1 \rangle\(, but it will not detect a virus \)\langle 0\ 1\ 0\ 1\ 0\ 0\ 1\ 0\ 1 \rangle\(.For each gene from \)2\( to \)G-1$$$, the scientists are wondering whether a given set of antibodies is enough to detect all viruses that can emerge through mutations from this gene. If not, they want to know the length of the shortest virus that cannot be detected.It may happen that sometimes scientists don't have any antibodies. Then of course no virus can be detected, so the scientists are only interested in the length of the shortest possible virus that can emerge from the gene mutations.
|
The first line of the input will contain three integers \(G\), \(N\) and \(M\) (\(G > 2\), \(N \geq G - 2\), \(M \geq 0\)) specifying the number of genes, the number of rows in the mutation table, and the number of antibodies.The following \(N\) lines contain descriptions of rows of the mutation table; each line begins with two integers \(a\) and \(k\) (\(2 \leq a < G\), \(k \geq 1\)), followed by a sequence of \(k\) integers \(b_1, b_2, \ldots, b_k\) (\(0 \leq b_i < G\)), that encode the row $$$\( a \to \langle b_1\ b_2\ \ldots\ b_k \rangle \)\(The sum of all values \)k\( does not exceed \)100\(. Every integer from \)2\( to \)G - 1\( appears in the table as \)a\( at least once.The next \)M\( lines contain descriptions of the antibodies; each such line begins with an integer \)\ell\( (\)\ell \geq 1\(), followed by a sequence of \)\ell\( integers \)c_1, c_2, \ldots, c_\ell\( (\)0 \leq c_i \leq 1\(), describing the antibody. The sum of all values \)\ell\( does not exceed \)50$$$.
|
Your program needs to output exactly \(G - 2\) lines, containing the answers for the subsequent genes from \(2\) to \(G - 1\).If all viruses that can mutate from this single gene can be detected by the given set of antibodies, you need to print the word ""YES"". You also need to print this if there are no viruses that could originate from this gene (it can happen when the sequences never stop mutating).Otherwise you need to print the word ""NO"", followed by an integer denoting the minimal length of the undetectable virus. You can assume that for all the prepared input data this value will be smaller than \(2^{63}\).
|
Input: 6 6 2 2 2 0 1 3 3 2 0 0 3 2 1 3 4 4 0 3 1 2 5 2 2 1 5 1 5 2 1 1 5 0 0 1 0 0 | Output: NO 2 NO 4 NO 9 YES
|
Master
| 4 | 2,237 | 992 | 624 | 13 |
|
788 |
A
|
788A
|
A. Functions again
| 1,600 |
dp; two pointers
|
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
|
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
|
Print the only integer β the maximum value of f.
|
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].In the second case maximal value of f is reachable only on the whole array.
|
Input: 51 4 2 3 1 | Output: 3
|
Medium
| 2 | 647 | 172 | 48 | 7 |
1,630 |
B
|
1630B
|
B. Range and Partition
| 1,800 |
binary search; constructive algorithms; data structures; greedy; two pointers
|
Given an array \(a\) of \(n\) integers, find a range of values \([x, y]\) (\(x \le y\)), and split \(a\) into exactly \(k\) (\(1 \le k \le n\)) subarrays in such a way that: Each subarray is formed by several continuous elements of \(a\), that is, it is equal to \(a_l, a_{l+1}, \ldots, a_r\) for some \(l\) and \(r\) (\(1 \leq l \leq r \leq n\)). Each element from \(a\) belongs to exactly one subarray. In each subarray the number of elements inside the range \([x, y]\) (inclusive) is strictly greater than the number of elements outside the range. An element with index \(i\) is inside the range \([x, y]\) if and only if \(x \le a_i \le y\). Print any solution that minimizes \(y - x\).
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 3 \cdot 10^4\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) β the length of the array \(a\) and the number of subarrays required in the partition.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) where \(a_i\) is the \(i\)-th element of the array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot10^5\).
|
For each test case, print \(k+1\) lines.In the first line, print \(x\) and \(y\) β the limits of the found range.Then print \(k\) lines, the \(i\)-th should contain \(l_i\) and \(r_i\) (\(1\leq l_i \leq r_i \leq n\)) β the limits of the \(i\)-th subarray.You can print the subarrays in any order.
|
In the first test, there should be only one subarray, which must be equal to the whole array. There are \(2\) elements inside the range \([1, 2]\) and \(0\) elements outside, if the chosen range is \([1, 1]\), there will be \(1\) element inside (\(a_1\)) and \(1\) element outside (\(a_2\)), and the answer will be invalid.In the second test, it is possible to choose the range \([2, 2]\), and split the array in subarrays \((1, 3)\) and \((4, 4)\), in subarray \((1, 3)\) there are \(2\) elements inside the range (\(a_2\) and \(a_3\)) and \(1\) element outside (\(a_1\)), in subarray \((4, 4)\) there is only \(1\) element (\(a_4\)), and it is inside the range.In the third test, it is possible to choose the range \([5, 5]\), and split the array in subarrays \((1, 4)\), \((5, 7)\) and \((8, 11)\), in the subarray \((1, 4)\) there are \(3\) elements inside the range and \(1\) element outside, in the subarray \((5, 7)\) there are \(2\) elements inside and \(1\) element outside and in the subarray \((8, 11)\) there are \(3\) elements inside and \(1\) element outside.
|
Input: 32 11 24 21 2 2 211 35 5 5 1 5 5 1 5 5 5 1 | Output: 1 2 1 2 2 2 1 3 4 4 5 5 1 1 2 2 3 11
|
Medium
| 5 | 691 | 630 | 296 | 16 |
809 |
B
|
809B
|
B. Glad to see you!
| 2,200 |
binary search; interactive
|
This is an interactive problem. In the output section below you will see the information about flushing the output.On Sunday Leha the hacker took Nura from the house where she lives and went with her to one of the most luxurious restaurants in ViΔkopolis. Upon arrival, they left the car in a huge parking lot near the restaurant and hurried inside the building.In the restaurant a polite waiter immediately brought the menu to Leha and Noora, consisting of n dishes. It is interesting that all dishes in the menu are numbered with integers from 1 to n. After a little thought, the girl ordered exactly k different dishes from available in the menu. To pass the waiting time while the chefs prepare ordered dishes, the girl invited the hacker to play a game that will help them get to know each other better.The game itself is very simple: Noora wants Leha to guess any two dishes among all ordered. At the same time, she is ready to answer only one type of questions. Leha can say two numbers x and y (1 β€ x, y β€ n). After that Noora chooses some dish a for the number x such that, at first, a is among the dishes Noora ordered (x can be equal to a), and, secondly, the value is the minimum possible. By the same rules the girl chooses dish b for y. After that Noora says Β«TAKΒ» to Leha, if , and Β«NIEΒ» otherwise. However, the restaurant is preparing quickly, so Leha has enough time to ask no more than 60 questions. After that he should name numbers of any two dishes Noora ordered.Help Leha to solve this problem!
|
There are two numbers n and k (2 β€ k β€ n β€ 105) in the single line of input denoting the number of dishes in the menu and the number of dishes Noora ordered.
|
If you want to provide an answer, output a string of the form 2 x y (1 β€ x, y β€ n, x β y), if you think the dishes x and y was among dishes ordered by Noora. After that, flush the output and terminate your program.
|
There are three dishes in sample. Noora ordered dished numberes 2 and 3, which Leha should guess. If Noora receive requests for the first dish (x = 1), then she'll choose the second dish (a = 2) as the dish with the minimum value . For the second (x = 2) and the third (x = 3) dishes themselves will be optimal, because in that case . Let Leha asks Noora about the next couple of dishes: x = 1, y = 2, then he'll recieve Β«NIEΒ» answer, because |1 - 2| > |2 - 2| x = 2, y = 1, then he'll recieve Β«TAKΒ» answer, because |2 - 2| β€ |1 - 2| x = 1, y = 3, then he'll recieve Β«NIEΒ» answer, because |1 - 2| > |3 - 3| x = 3, y = 1, then he'll recieve Β«TAKΒ» answer, because |3 - 3| β€ |1 - 2| x = 2, y = 3, then he'll recieve Β«TAKΒ» answer, because |2 - 2| β€ |3 - 3| x = 3, y = 2, then he'll recieve Β«TAKΒ» answer, because |3 - 3| β€ |2 - 2| According to the available information, it is possible to say that Nura ordered dishes with numbers 2 and 3.
|
Input: 3 2NIETAKNIETAKTAKTAK | Output: 1 1 21 2 11 1 31 3 11 2 31 3 22 2 3
|
Hard
| 2 | 1,516 | 157 | 214 | 8 |
1,511 |
E
|
1511E
|
E. Colorings and Dominoes
| 2,100 |
combinatorics; dp; greedy; math
|
You have a large rectangular board which is divided into \(n \times m\) cells (the board has \(n\) rows and \(m\) columns). Each cell is either white or black.You paint each white cell either red or blue. Obviously, the number of different ways to paint them is \(2^w\), where \(w\) is the number of white cells.After painting the white cells of the board, you want to place the maximum number of dominoes on it, according to the following rules: each domino covers two adjacent cells; each cell is covered by at most one domino; if a domino is placed horizontally (it covers two adjacent cells in one of the rows), it should cover only red cells; if a domino is placed vertically (it covers two adjacent cells in one of the columns), it should cover only blue cells. Let the value of the board be the maximum number of dominoes you can place. Calculate the sum of values of the board over all \(2^w\) possible ways to paint it. Since it can be huge, print it modulo \(998\,244\,353\).
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 3 \cdot 10^5\); \(nm \le 3 \cdot 10^5\)) β the number of rows and columns, respectively.Then \(n\) lines follow, each line contains a string of \(m\) characters. The \(j\)-th character in the \(i\)-th string is * if the \(j\)-th cell in the \(i\)-th row is black; otherwise, that character is o.
|
Print one integer β the sum of values of the board over all \(2^w\) possible ways to paint it, taken modulo \(998\,244\,353\).
|
Input: 3 4 **oo oo*o **oo | Output: 144
|
Hard
| 4 | 985 | 366 | 126 | 15 |
|
1,753 |
E
|
1753E
|
E. N Machines
| 3,300 |
binary search; brute force; greedy
|
You have been invited as a production process optimization specialist to some very large company. The company has \(n\) machines at its factory, standing one behind another in the production chain. Each machine can be described in one of the following two ways: \((+,~a_i)\) or \((*,~a_i)\).If a workpiece with the value \(x\) is supplied to the machine of kind \((+,~a_i)\), then the output workpiece has value \(x + a_i\).If a workpiece with the value \(x\) is supplied to the machine of kind \((*,~a_i)\), then the output workpiece has value \(x \cdot a_i\).The whole production process is as follows. The workpiece with the value \(1\) is supplied to the first machine, then the workpiece obtained after the operation of the first machine is supplied to the second machine, then the workpiece obtained after the operation of the second machine is supplied to the third machine, and so on. The company is not doing very well, so now the value of the resulting product does not exceed \(2 \cdot 10^9\).The directors of the company are not satisfied with the efficiency of the production process and have given you a budget of \(b\) coins to optimize it.To optimize production you can change the order of machines in the chain. Namely, by spending \(p\) coins, you can take any machine of kind \((+,~a_i)\) and move it to any place in the chain without changing the order of other machines. Also, by spending \(m\) coins, you can take any machine of kind \((*,~a_i)\) and move it to any place in the chain.What is the maximum value of the resulting product that can be achieved if the total cost of movements that are made should not exceed \(b\) coins?
|
The first line contains four integers \(n\), \(b\), \(p\) and \(m\) (\(1 \le n \le 10^6\), \(1 \le b, p, m \le 10^9\)) β the number of machine at the factory, your budget and costs of movements of both kinds of machines.Each of the following \(n\) lines contains description of a machine. The description begins with one of the following characters: ""+"" or ""*"", that denotes the kind of the machine. Then an integer \(a_i\) follows (\(1 \le a_i \le 2 \cdot 10^9\)).It's guaranteed that the current value of the resulting product does not exceed \(2 \cdot 10^9\).
|
Print one integer β the maximum value of the resulting product that can be achieved if the total cost of movements that are made does not exceed \(b\) coins.
|
In the first example our budget is too low to move machine \((*,~2)\), but we can move both machines \((+,~1)\) to the beginning of the chain. So the final chain will be \((+,~1)\) \((+,~1)\) \((*,~2)\). If the workpiece with the value \(1\) is supplied to the first machine, its value will be changed in the following way: \(1, 2, 3, 6\).In the second example we can move only one machine. Let's move machine \((+,~2)\) to the beginning of the chain. The final chain will be \((+,~2)\) \((*,~2)\) \((+,~1)\) \((*,~3)\). The value of the workpiece will be changed in the following way: \(1, 3, 6, 7, 21\).In the third example we can place machine \((*,~4)\) before the machine \((*,~5)\), and move machine \((+,~3)\) to the beginning of the chain. The final chain will be \((+,~3)\) \((*,~2)\) \((+,~1)\) \((+,~1)\) \((+,~1)\) \((+,~1)\) \((*,~4)\) \((*,~5)\). The value of the workpiece will be changed in the following way: \(1, 4, 8, 9, 10, 11, 12, 48, 240\).
|
Input: 3 2 1 3 * 2 + 1 + 1 | Output: 6
|
Master
| 3 | 1,654 | 566 | 157 | 17 |
1,367 |
B
|
1367B
|
B. Even Array
| 800 |
greedy; math
|
You are given an array \(a[0 \ldots n-1]\) of length \(n\) which consists of non-negative integers. Note that array indices start from zero.An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all \(i\) (\(0 \le i \le n - 1\)) the equality \(i \bmod 2 = a[i] \bmod 2\) holds, where \(x \bmod 2\) is the remainder of dividing \(x\) by 2.For example, the arrays [\(0, 5, 2, 1\)] and [\(0, 17, 0, 3\)] are good, and the array [\(2, 4, 6, 7\)] is bad, because for \(i=1\), the parities of \(i\) and \(a[i]\) are different: \(i \bmod 2 = 1 \bmod 2 = 1\), but \(a[i] \bmod 2 = 4 \bmod 2 = 0\).In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).Find the minimum number of moves in which you can make the array \(a\) good, or say that this is not possible.
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases in the test. Then \(t\) test cases follow.Each test case starts with a line containing an integer \(n\) (\(1 \le n \le 40\)) β the length of the array \(a\).The next line contains \(n\) integers \(a_0, a_1, \ldots, a_{n-1}\) (\(0 \le a_i \le 1000\)) β the initial array.
|
For each test case, output a single integer β the minimum number of moves to make the given array \(a\) good, or -1 if this is not possible.
|
In the first test case, in the first move, you can swap the elements with indices \(0\) and \(1\), and in the second move, you can swap the elements with indices \(2\) and \(3\).In the second test case, in the first move, you need to swap the elements with indices \(0\) and \(1\).In the third test case, you cannot make the array good.
|
Input: 4 4 3 2 7 6 3 3 2 6 1 7 7 4 9 2 1 18 3 0 | Output: 2 1 -1 0
|
Beginner
| 2 | 900 | 367 | 140 | 13 |
550 |
E
|
550E
|
E. Brackets in Implications
| 2,200 |
constructive algorithms; greedy; implementation; math
|
Implication is a function of two logical arguments, its value is false if and only if the value of the first argument is true and the value of the second argument is false. Implication is written by using character '', and the arguments and the result of the implication are written as '0' (false) and '1' (true). According to the definition of the implication: When a logical expression contains multiple implications, then when there are no brackets, it will be calculated from left to fight. For example,. When there are brackets, we first calculate the expression in brackets. For example,.For the given logical expression determine if it is possible to place there brackets so that the value of a logical expression is false. If it is possible, your task is to find such an arrangement of brackets.
|
The first line contains integer n (1 β€ n β€ 100 000) β the number of arguments in a logical expression.The second line contains n numbers a1, a2, ..., an (), which means the values of arguments in the expression in the order they occur.
|
Print ""NO"" (without the quotes), if it is impossible to place brackets in the expression so that its value was equal to 0.Otherwise, print ""YES"" in the first line and the logical expression with the required arrangement of brackets in the second line.The expression should only contain characters '0', '1', '-' (character with ASCII code 45), '>' (character with ASCII code 62), '(' and ')'. Characters '-' and '>' can occur in an expression only paired like that: (""->"") and represent implication. The total number of logical arguments (i.e. digits '0' and '1') in the expression must be equal to n. The order in which the digits follow in the expression from left to right must coincide with a1, a2, ..., an.The expression should be correct. More formally, a correct expression is determined as follows: Expressions ""0"", ""1"" (without the quotes) are correct. If v1, v2 are correct, then v1->v2 is a correct expression. If v is a correct expression, then (v) is a correct expression. The total number of characters in the resulting expression mustn't exceed 106.If there are multiple possible answers, you are allowed to print any of them.
|
Input: 40 1 1 0 | Output: YES(((0)->1)->(1->0))
|
Hard
| 4 | 803 | 235 | 1,150 | 5 |
|
137 |
C
|
137C
|
C. History
| 1,500 |
sortings
|
Polycarpus likes studying at school a lot and he is always diligent about his homework. Polycarpus has never had any problems with natural sciences as his great-great-grandfather was the great physicist Seinstein. On the other hand though, Polycarpus has never had an easy time with history.Everybody knows that the World history encompasses exactly n events: the i-th event had continued from the year ai to the year bi inclusive (ai < bi). Polycarpus easily learned the dates when each of n events started and ended (Polycarpus inherited excellent memory from his great-great-granddad). But the teacher gave him a more complicated task: Polycaprus should know when all events began and ended and he should also find out for each event whether it includes another event. Polycarpus' teacher thinks that an event j includes an event i if aj < ai and bi < bj. Your task is simpler: find the number of events that are included in some other event.
|
The first input line contains integer n (1 β€ n β€ 105) which represents the number of events. Next n lines contain descriptions of the historical events, one event per line. The i + 1 line contains two integers ai and bi (1 β€ ai < bi β€ 109) β the beginning and the end of the i-th event. No two events start or finish in the same year, that is, ai β aj, ai β bj, bi β aj, bi β bj for all i, j (where i β j). Events are given in arbitrary order.
|
Print the only integer β the answer to the problem.
|
In the first example the fifth event is contained in the fourth. Similarly, the fourth event is contained in the third, the third β in the second and the second β in the first.In the second example all events except the first one are contained in the first.In the third example only one event, so the answer is 0.
|
Input: 51 102 93 84 75 6 | Output: 4
|
Medium
| 1 | 945 | 443 | 51 | 1 |
1,696 |
C
|
1696C
|
C. Fishingprince Plays With Array
| 1,400 |
constructive algorithms; greedy; implementation; math
|
Fishingprince is playing with an array \([a_1,a_2,\dots,a_n]\). He also has a magic number \(m\).He can do the following two operations on it: Select \(1\le i\le n\) such that \(a_i\) is divisible by \(m\) (that is, there exists an integer \(t\) such that \(m \cdot t = a_i\)). Replace \(a_i\) with \(m\) copies of \(\frac{a_i}{m}\). The order of the other elements doesn't change. For example, when \(m=2\) and \(a=[2,3]\) and \(i=1\), \(a\) changes into \([1,1,3]\). Select \(1\le i\le n-m+1\) such that \(a_i=a_{i+1}=\dots=a_{i+m-1}\). Replace these \(m\) elements with a single \(m \cdot a_i\). The order of the other elements doesn't change. For example, when \(m=2\) and \(a=[3,2,2,3]\) and \(i=2\), \(a\) changes into \([3,4,3]\). Note that the array length might change during the process. The value of \(n\) above is defined as the current length of the array (might differ from the \(n\) in the input).Fishingprince has another array \([b_1,b_2,\dots,b_k]\). Please determine if he can turn \(a\) into \(b\) using any number (possibly zero) of operations.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1\le n\le 5\cdot 10^4\), \(2\le m\le 10^9\)).The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1\le a_i\le 10^9\)).The third line of each test case contains one integer \(k\) (\(1\le k\le 5\cdot 10^4\)).The fourth line of each test case contains \(k\) integers \(b_1,b_2,\ldots,b_k\) (\(1\le b_i\le 10^9\)).It is guaranteed that the sum of \(n+k\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each testcase, print Yes if it is possible to turn \(a\) into \(b\), and No otherwise. You can print each letter in any case (upper or lower).
|
In the first test case of the sample, we can do the second operation with \(i=2\): \([1,\color{red}{2,2},4,2]\to [1,\color{red}{4},4,2]\).In the second testcase of the sample, we can: do the second operation with \(i=2\): \([1,\color{red}{2,2},8,2,2]\to [1,\color{red}{4},8,2,2]\). do the second operation with \(i=4\): \([1,4,8,\color{red}{2,2}]\to [1,4,8,\color{red}{4}]\). do the first operation with \(i=3\): \([1,4,\color{red}{8},4]\to [1,4,\color{red}{4,4},4]\). do the second operation with \(i=2\): \([1,\color{red}{4,4},4,4]\to [1,\color{red}{8},4,4]\). do the second operation with \(i=3\): \([1,8,\color{red}{4,4}]\to [1,8,\color{red}{8}]\). do the second operation with \(i=2\): \([1,\color{red}{8,8}]\to [1,\color{red}{16}]\).
|
Input: 55 21 2 2 4 241 4 4 26 21 2 2 8 2 221 168 33 3 3 3 3 3 3 346 6 6 68 33 9 6 3 12 12 36 12169 3 2 2 2 3 4 12 4 12 4 12 4 12 4 48 33 9 6 3 12 12 36 12712 2 4 3 4 12 56 | Output: Yes Yes No Yes No
|
Easy
| 4 | 1,065 | 665 | 146 | 16 |
1,364 |
C
|
1364C
|
C. Ehab and Prefix MEXs
| 1,600 |
brute force; constructive algorithms; greedy
|
Given an array \(a\) of length \(n\), find another array, \(b\), of length \(n\) such that: for each \(i\) \((1 \le i \le n)\) \(MEX(\{b_1\), \(b_2\), \(\ldots\), \(b_i\})=a_i\). The \(MEX\) of a set of integers is the smallest non-negative integer that doesn't belong to this set.If such array doesn't exist, determine this.
|
The first line contains an integer \(n\) (\(1 \le n \le 10^5\)) β the length of the array \(a\).The second line contains \(n\) integers \(a_1\), \(a_2\), \(\ldots\), \(a_n\) (\(0 \le a_i \le i\)) β the elements of the array \(a\). It's guaranteed that \(a_i \le a_{i+1}\) for \(1\le i < n\).
|
If there's no such array, print a single line containing \(-1\).Otherwise, print a single line containing \(n\) integers \(b_1\), \(b_2\), \(\ldots\), \(b_n\) (\(0 \le b_i \le 10^6\))If there are multiple answers, print any.
|
In the second test case, other answers like \([1,1,1,0]\), for example, are valid.
|
Input: 3 1 2 3 | Output: 0 1 2
|
Medium
| 3 | 325 | 291 | 224 | 13 |
1,392 |
F
|
1392F
|
F. Omkar and Landslide
| 2,400 |
binary search; constructive algorithms; data structures; greedy; math
|
Omkar is standing at the foot of Celeste mountain. The summit is \(n\) meters away from him, and he can see all of the mountains up to the summit, so for all \(1 \leq j \leq n\) he knows that the height of the mountain at the point \(j\) meters away from himself is \(h_j\) meters. It turns out that for all \(j\) satisfying \(1 \leq j \leq n - 1\), \(h_j < h_{j + 1}\) (meaning that heights are strictly increasing).Suddenly, a landslide occurs! While the landslide is occurring, the following occurs: every minute, if \(h_j + 2 \leq h_{j + 1}\), then one square meter of dirt will slide from position \(j + 1\) to position \(j\), so that \(h_{j + 1}\) is decreased by \(1\) and \(h_j\) is increased by \(1\). These changes occur simultaneously, so for example, if \(h_j + 2 \leq h_{j + 1}\) and \(h_{j + 1} + 2 \leq h_{j + 2}\) for some \(j\), then \(h_j\) will be increased by \(1\), \(h_{j + 2}\) will be decreased by \(1\), and \(h_{j + 1}\) will be both increased and decreased by \(1\), meaning that in effect \(h_{j + 1}\) is unchanged during that minute.The landslide ends when there is no \(j\) such that \(h_j + 2 \leq h_{j + 1}\). Help Omkar figure out what the values of \(h_1, \dots, h_n\) will be after the landslide ends. It can be proven that under the given constraints, the landslide will always end in finitely many minutes.Note that because of the large amount of input, it is recommended that your code uses fast IO.
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^6\)). The second line contains \(n\) integers \(h_1, h_2, \dots, h_n\) satisfying \(0 \leq h_1 < h_2 < \dots < h_n \leq 10^{12}\) β the heights.
|
Output \(n\) integers, where the \(j\)-th integer is the value of \(h_j\) after the landslide has stopped.
|
Initially, the mountain has heights \(2, 6, 7, 8\).In the first minute, we have \(2 + 2 \leq 6\), so \(2\) increases to \(3\) and \(6\) decreases to \(5\), leaving \(3, 5, 7, 8\).In the second minute, we have \(3 + 2 \leq 5\) and \(5 + 2 \leq 7\), so \(3\) increases to \(4\), \(5\) is unchanged, and \(7\) decreases to \(6\), leaving \(4, 5, 6, 8\).In the third minute, we have \(6 + 2 \leq 8\), so \(6\) increases to \(7\) and \(8\) decreases to \(7\), leaving \(4, 5, 7, 7\).In the fourth minute, we have \(5 + 2 \leq 7\), so \(5\) increases to \(6\) and \(7\) decreases to \(6\), leaving \(4, 6, 6, 7\).In the fifth minute, we have \(4 + 2 \leq 6\), so \(4\) increases to \(5\) and \(6\) decreases to \(5\), leaving \(5, 5, 6, 7\).In the sixth minute, nothing else can change so the landslide stops and our answer is \(5, 5, 6, 7\).
|
Input: 4 2 6 7 8 | Output: 5 5 6 7
|
Expert
| 5 | 1,438 | 211 | 106 | 13 |
460 |
D
|
460D
|
D. Little Victor and Set
| 2,300 |
brute force; constructive algorithms; math
|
Little Victor adores the sets theory. Let us remind you that a set is a group of numbers where all numbers are pairwise distinct. Today Victor wants to find a set of integers S that has the following properties: for all x the following inequality holds l β€ x β€ r; 1 β€ |S| β€ k; lets denote the i-th element of the set S as si; value must be as small as possible. Help Victor find the described set.
|
The first line contains three space-separated integers l, r, k (1 β€ l β€ r β€ 1012; 1 β€ k β€ min(106, r - l + 1)).
|
Print the minimum possible value of f(S). Then print the cardinality of set |S|. Then print the elements of the set in any order.If there are multiple optimal sets, you can print any of them.
|
Operation represents the operation of bitwise exclusive OR. In other words, it is the XOR operation.
|
Input: 8 15 3 | Output: 1210 11
|
Expert
| 3 | 397 | 111 | 191 | 4 |
929 |
C
|
929C
|
C. ΠΡΠ°ΡΠΈΠ²Π°Ρ ΠΊΠΎΠΌΠ°Π½Π΄Π°
| 1,700 |
*special; combinatorics; math
|
ΠΠ°Π²ΡΡΠ° Ρ Ρ
ΠΎΠΊΠΊΠ΅ΠΉΠ½ΠΎΠΉ ΠΊΠΎΠΌΠ°Π½Π΄Ρ, ΠΊΠΎΡΠΎΡΠΎΠΉ ΡΡΠΊΠΎΠ²ΠΎΠ΄ΠΈΡ ΠΠ²Π³Π΅Π½ΠΈΠΉ, Π²Π°ΠΆΠ½ΡΠΉ ΠΌΠ°ΡΡ. ΠΠ²Π³Π΅Π½ΠΈΡ Π½ΡΠΆΠ½ΠΎ Π²ΡΠ±ΡΠ°ΡΡ ΡΠ΅ΡΡΡ ΠΈΠ³ΡΠΎΠΊΠΎΠ², ΠΊΠΎΡΠΎΡΡΠ΅ Π²ΡΠΉΠ΄ΡΡ Π½Π° Π»Π΅Π΄ Π² ΡΡΠ°ΡΡΠΎΠ²ΠΎΠΌ ΡΠΎΡΡΠ°Π²Π΅: ΠΎΠ΄ΠΈΠ½ Π²ΡΠ°ΡΠ°ΡΡ, Π΄Π²Π° Π·Π°ΡΠΈΡΠ½ΠΈΠΊΠ° ΠΈ ΡΡΠΈ Π½Π°ΠΏΠ°Π΄Π°ΡΡΠΈΡ
.Π’Π°ΠΊ ΠΊΠ°ΠΊ ΡΡΠΎ ΡΡΠ°ΡΡΠΎΠ²ΡΠΉ ΡΠΎΡΡΠ°Π², ΠΠ²Π³Π΅Π½ΠΈΡ Π±ΠΎΠ»ΡΡΠ΅ Π²ΠΎΠ»Π½ΡΠ΅Ρ, Π½Π°ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΊΡΠ°ΡΠΈΠ²Π° Π±ΡΠ΄Π΅Ρ ΠΊΠΎΠΌΠ°Π½Π΄Π° Π½Π° Π»ΡΠ΄Ρ, ΡΠ΅ΠΌ ΡΠΏΠΎΡΠΎΠ±Π½ΠΎΡΡΠΈ ΠΈΠ³ΡΠΎΠΊΠΎΠ². Π ΠΈΠΌΠ΅Π½Π½ΠΎ, ΠΠ²Π³Π΅Π½ΠΈΠΉ Ρ
ΠΎΡΠ΅Ρ Π²ΡΠ±ΡΠ°ΡΡ ΡΠ°ΠΊΠΎΠΉ ΡΡΠ°ΡΡΠΎΠ²ΡΠΉ ΡΠΎΡΡΠ°Π², ΡΡΠΎΠ±Ρ Π½ΠΎΠΌΠ΅ΡΠ° Π»ΡΠ±ΡΡ
Π΄Π²ΡΡ
ΠΈΠ³ΡΠΎΠΊΠΎΠ² ΠΈΠ· ΡΡΠ°ΡΡΠΎΠ²ΠΎΠ³ΠΎ ΡΠΎΡΡΠ°Π²Π° ΠΎΡΠ»ΠΈΡΠ°Π»ΠΈΡΡ Π½Π΅ Π±ΠΎΠ»Π΅Π΅, ΡΠ΅ΠΌ Π² Π΄Π²Π° ΡΠ°Π·Π°. ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ, ΠΈΠ³ΡΠΎΠΊΠΈ Ρ Π½ΠΎΠΌΠ΅ΡΠ°ΠΌΠΈ 13, 14, 10, 18, 15 ΠΈ 20 ΡΡΡΡΠΎΡΡ ΠΠ²Π³Π΅Π½ΠΈΡ, Π° Π΅ΡΠ»ΠΈ, Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ, Π½Π° Π»Π΅Π΄ Π²ΡΠΉΠ΄ΡΡ ΠΈΠ³ΡΠΎΠΊΠΈ Ρ Π½ΠΎΠΌΠ΅ΡΠ°ΠΌΠΈ 8 ΠΈ 17, ΡΠΎ ΡΡΠΎ Π½Π΅ ΡΡΡΡΠΎΠΈΡ ΠΠ²Π³Π΅Π½ΠΈΡ.ΠΡΠΎ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΈΠ· ΠΈΠ³ΡΠΎΠΊΠΎΠ² Π²Π°ΠΌ ΠΈΠ·Π²Π΅ΡΡΠ½ΠΎ, Π½Π° ΠΊΠ°ΠΊΠΎΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ ΠΎΠ½ ΠΈΠ³ΡΠ°Π΅Ρ (Π²ΡΠ°ΡΠ°ΡΡ, Π·Π°ΡΠΈΡΠ½ΠΈΠΊ ΠΈΠ»ΠΈ Π½Π°ΠΏΠ°Π΄Π°ΡΡΠΈΠΉ), Π° ΡΠ°ΠΊΠΆΠ΅ Π΅Π³ΠΎ Π½ΠΎΠΌΠ΅Ρ. Π Ρ
ΠΎΠΊΠΊΠ΅Π΅ Π½ΠΎΠΌΠ΅ΡΠ° ΠΈΠ³ΡΠΎΠΊΠΎΠ² Π½Π΅ ΠΎΠ±ΡΠ·Π°ΡΠ΅Π»ΡΠ½ΠΎ ΠΈΠ΄ΡΡ ΠΏΠΎΠ΄ΡΡΠ΄. ΠΠΎΡΡΠΈΡΠ°ΠΉΡΠ΅ ΡΠΈΡΠ»ΠΎ ΡΠ°Π·Π»ΠΈΡΠ½ΡΡ
ΡΡΠ°ΡΡΠΎΠ²ΡΡ
ΡΠΎΡΡΠ°Π²ΠΎΠ² ΠΈΠ· ΠΎΠ΄Π½ΠΎΠ³ΠΎ Π²ΡΠ°ΡΠ°ΡΡ, Π΄Π²ΡΡ
Π·Π°ΡΠΈΡΠ½ΠΈΠΊΠΎΠ² ΠΈ ΡΡΠ΅Ρ
Π½Π°ΠΏΠ°Π΄Π°ΡΡΠΈΡ
, ΠΊΠΎΡΠΎΡΡΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π²ΡΠ±ΡΠ°ΡΡ ΠΠ²Π³Π΅Π½ΠΈΠΉ, ΡΡΠΎΠ±Ρ Π²ΡΠΏΠΎΠ»Π½ΡΠ»ΠΎΡΡ Π΅Π³ΠΎ ΡΡΠ»ΠΎΠ²ΠΈΠ΅ ΠΊΡΠ°ΡΠΎΡΡ.
|
ΠΠ΅ΡΠ²Π°Ρ ΡΡΡΠΎΠΊΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ ΡΡΠΈ ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° g, d ΠΈ f (1 β€ g β€ 1 000, 1 β€ d β€ 1 000, 1 β€ f β€ 1 000) β ΡΠΈΡΠ»ΠΎ Π²ΡΠ°ΡΠ°ΡΠ΅ΠΉ, Π·Π°ΡΠΈΡΠ½ΠΈΠΊΠΎΠ² ΠΈ Π½Π°ΠΏΠ°Π΄Π°ΡΡΠΈΡ
Π² ΠΊΠΎΠΌΠ°Π½Π΄Π΅ ΠΠ²Π³Π΅Π½ΠΈΡ. ΠΡΠΎΡΠ°Ρ ΡΡΡΠΎΠΊΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ g ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π», ΠΊΠ°ΠΆΠ΄ΠΎΠ΅ Π² ΠΏΡΠ΅Π΄Π΅Π»Π°Ρ
ΠΎΡ 1 Π΄ΠΎ 100 000 β Π½ΠΎΠΌΠ΅ΡΠ° Π²ΡΠ°ΡΠ°ΡΠ΅ΠΉ.Π’ΡΠ΅ΡΡΡ ΡΡΡΠΎΠΊΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ d ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π», ΠΊΠ°ΠΆΠ΄ΠΎΠ΅ Π² ΠΏΡΠ΅Π΄Π΅Π»Π°Ρ
ΠΎΡ 1 Π΄ΠΎ 100 000 β Π½ΠΎΠΌΠ΅ΡΠ° Π·Π°ΡΠΈΡΠ½ΠΈΠΊΠΎΠ².Π§Π΅ΡΠ²Π΅ΡΡΠ°Ρ ΡΡΡΠΎΠΊΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ f ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π», ΠΊΠ°ΠΆΠ΄ΠΎΠ΅ Π² ΠΏΡΠ΅Π΄Π΅Π»Π°Ρ
ΠΎΡ 1 Π΄ΠΎ 100 000 β Π½ΠΎΠΌΠ΅ΡΠ° Π½Π°ΠΏΠ°Π΄Π°ΡΡΠΈΡ
.ΠΠ°ΡΠ°Π½ΡΠΈΡΡΠ΅ΡΡΡ, ΡΡΠΎ ΠΎΠ±ΡΠ΅Π΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΈΠ³ΡΠΎΠΊΠΎΠ² Π½Π΅ ΠΏΡΠ΅Π²ΠΎΡΡ
ΠΎΠ΄ΠΈΡ 1 000, Ρ. Π΅. g + d + f β€ 1 000. ΠΡΠ΅ g + d + f Π½ΠΎΠΌΠ΅ΡΠΎΠ² ΠΈΠ³ΡΠΎΠΊΠΎΠ² ΡΠ°Π·Π»ΠΈΡΠ½Ρ.
|
ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΡΡ
ΡΡΠ°ΡΡΠΎΠ²ΡΡ
ΡΠΎΡΡΠ°Π²ΠΎΠ².
|
Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ Π²ΡΠ΅Π³ΠΎ ΠΎΠ΄ΠΈΠ½ Π²Π°ΡΠΈΠ°Π½Ρ Π΄Π»Ρ Π²ΡΠ±ΠΎΡΠ° ΡΠΎΡΡΠ°Π²Π°, ΠΊΠΎΡΠΎΡΡΠΉ ΡΠ΄ΠΎΠ²Π»Π΅ΡΠ²ΠΎΡΡΠ΅Ρ ΠΎΠΏΠΈΡΠ°Π½Π½ΡΠΌ ΡΡΠ»ΠΎΠ²ΠΈΡΠΌ, ΠΏΠΎΡΡΠΎΠΌΡ ΠΎΡΠ²Π΅Ρ 1.ΠΠΎ Π²ΡΠΎΡΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΈΠ³ΡΠΎΠ²ΡΠ΅ ΡΠΎΡΠ΅ΡΠ°Π½ΠΈΡ (Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ Π²ΡΠ°ΡΠ°ΡΡ-Π·Π°ΡΠΈΡΠ½ΠΈΠΊ-Π·Π°ΡΠΈΡΠ½ΠΈΠΊ-Π½Π°ΠΏΠ°Π΄Π°ΡΡΠΈΠΉ-Π½Π°ΠΏΠ°Π΄Π°ΡΡΠΈΠΉ-Π½Π°ΠΏΠ°Π΄Π°ΡΡΠΈΠΉ): 16 20 12 13 21 11 16 20 12 13 11 10 16 20 19 13 21 11 16 20 19 13 11 10 16 12 19 13 21 11 16 12 19 13 11 10 Π’Π°ΠΊΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ, ΠΎΡΠ²Π΅Ρ Π½Π° ΡΡΠΎΡ ΠΏΡΠΈΠΌΠ΅Ρ β 6.
|
Input: 1 2 31510 1920 11 13 | Output: 1
|
Medium
| 3 | 953 | 562 | 68 | 9 |
750 |
B
|
750B
|
B. New Year and North Pole
| 1,300 |
geometry; implementation
|
In this problem we assume the Earth to be a completely round ball and its surface a perfect sphere. The length of the equator and any meridian is considered to be exactly 40 000 kilometers. Thus, travelling from North Pole to South Pole or vice versa takes exactly 20 000 kilometers.Limak, a polar bear, lives on the North Pole. Close to the New Year, he helps somebody with delivering packages all around the world. Instead of coordinates of places to visit, Limak got a description how he should move, assuming that he starts from the North Pole. The description consists of n parts. In the i-th part of his journey, Limak should move ti kilometers in the direction represented by a string diri that is one of: ""North"", ""South"", ""West"", ""East"".Limak isnβt sure whether the description is valid. You must help him to check the following conditions: If at any moment of time (before any of the instructions or while performing one of them) Limak is on the North Pole, he can move only to the South. If at any moment of time (before any of the instructions or while performing one of them) Limak is on the South Pole, he can move only to the North. The journey must end on the North Pole. Check if the above conditions are satisfied and print ""YES"" or ""NO"" on a single line.
|
The first line of the input contains a single integer n (1 β€ n β€ 50).The i-th of next n lines contains an integer ti and a string diri (1 β€ ti β€ 106, ) β the length and the direction of the i-th part of the journey, according to the description Limak got.
|
Print ""YES"" if the description satisfies the three conditions, otherwise print ""NO"", both without the quotes.
|
Drawings below show how Limak's journey would look like in first two samples. In the second sample the answer is ""NO"" because he doesn't end on the North Pole.
|
Input: 57500 South10000 East3500 North4444 West4000 North | Output: YES
|
Easy
| 2 | 1,285 | 255 | 113 | 7 |
611 |
E
|
611E
|
E. New Year and Three Musketeers
| 2,400 |
data structures; greedy; sortings
|
Do you know the story about the three musketeers? Anyway, you must help them now.Richelimakieu is a cardinal in the city of Bearis. He found three brave warriors and called them the three musketeers. Athos has strength a, Borthos strength b, and Caramis has strength c.The year 2015 is almost over and there are still n criminals to be defeated. The i-th criminal has strength ti. It's hard to defeat strong criminals β maybe musketeers will have to fight together to achieve it.Richelimakieu will coordinate musketeers' actions. In each hour each musketeer can either do nothing or be assigned to one criminal. Two or three musketeers can be assigned to the same criminal and then their strengths are summed up. A criminal can be defeated in exactly one hour (also if two or three musketeers fight him). Richelimakieu can't allow the situation where a criminal has strength bigger than the sum of strengths of musketeers fighting him β a criminal would win then!In other words, there are three ways to defeat a criminal. A musketeer of the strength x in one hour can defeat a criminal of the strength not greater than x. So, for example Athos in one hour can defeat criminal i only if ti β€ a. Two musketeers can fight together and in one hour defeat a criminal of the strength not greater than the sum of strengths of these two musketeers. So, for example Athos and Caramis in one hour can defeat criminal i only if ti β€ a + c. Note that the third remaining musketeer can either do nothing or fight some other criminal. Similarly, all three musketeers can fight together and in one hour defeat a criminal of the strength not greater than the sum of musketeers' strengths, i.e. ti β€ a + b + c. Richelimakieu doesn't want musketeers to fight during the New Year's Eve. Thus, he must coordinate their actions in order to minimize the number of hours till all criminals will be defeated.Find the minimum number of hours to defeat all criminals. If musketeers can't defeat them all then print ""-1"" (without the quotes) instead.
|
The first line of the input contains a single integer n (1 β€ n β€ 200 000) β the number of criminals.The second line contains three integers a, b and c (1 β€ a, b, c β€ 108) β strengths of musketeers.The third line contains n integers t1, t2, ..., tn (1 β€ ti β€ 108) β strengths of criminals.
|
Print one line with the answer.If it's impossible to defeat all criminals, print ""-1"" (without the quotes). Otherwise, print the minimum number of hours the three musketeers will spend on defeating all criminals.
|
In the first sample Athos has strength 10, Borthos 20, and Caramis 30. They can defeat all criminals in two hours: Borthos and Caramis should together fight a criminal with strength 50. In the same hour Athos can fight one of four criminals with strength 1. There are three criminals left, each with strength 1. Each musketeer can fight one criminal in the second hour. In the second sample all three musketeers must together fight a criminal with strength 51. It takes one hour. In the second hour they can fight separately, each with one criminal. In the third hour one criminal is left and any of musketeers can fight him.
|
Input: 510 20 301 1 1 1 50 | Output: 2
|
Expert
| 3 | 2,025 | 288 | 214 | 6 |
1,270 |
A
|
1270A
|
A. Card Game
| 800 |
games; greedy; math
|
Two players decided to play one interesting card game.There is a deck of \(n\) cards, with values from \(1\) to \(n\). The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that \(n = 5\), the first player has cards with values \(2\) and \(3\), and the second player has cards with values \(1\), \(4\), \(5\). Then one possible flow of the game is:The first player chooses the card \(3\). The second player chooses the card \(1\). As \(3>1\), the first player gets both cards. Now the first player has cards \(1\), \(2\), \(3\), the second player has cards \(4\), \(5\).The first player chooses the card \(3\). The second player chooses the card \(4\). As \(3<4\), the second player gets both cards. Now the first player has cards \(1\), \(2\). The second player has cards \(3\), \(4\), \(5\).The first player chooses the card \(1\). The second player chooses the card \(3\). As \(1<3\), the second player gets both cards. Now the first player has only the card \(2\). The second player has cards \(1\), \(3\), \(4\), \(5\).The first player chooses the card \(2\). The second player chooses the card \(4\). As \(2<4\), the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows.The first line of each test case contains three integers \(n\), \(k_1\), \(k_2\) (\(2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n\)) β the number of cards, number of cards owned by the first player and second player correspondingly.The second line of each test case contains \(k_1\) integers \(a_1, \dots, a_{k_1}\) (\(1 \le a_i \le n\)) β the values of cards of the first player.The third line of each test case contains \(k_2\) integers \(b_1, \dots, b_{k_2}\) (\(1 \le b_i \le n\)) β the values of cards of the second player.It is guaranteed that the values of all cards are different.
|
For each test case, output ""YES"" in a separate line, if the first player wins. Otherwise, output ""NO"" in a separate line. You can print each letter in any case (upper or lower).
|
In the first test case of the example, there is only one possible move for every player: the first player will put \(2\), the second player will put \(1\). \(2>1\), so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement.
|
Input: 2 2 1 1 2 1 5 2 3 2 3 1 4 5 | Output: YES NO
|
Beginner
| 3 | 2,045 | 775 | 181 | 12 |
1,287 |
B
|
1287B
|
B. Hyperset
| 1,500 |
brute force; data structures; implementation
|
Bees Alice and Alesya gave beekeeper Polina famous card game ""Set"" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature β color, number, shape, and shading β the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called ""Hyperset"". In her game, there are \(n\) cards with \(k\) features, each feature has three possible values: ""S"", ""E"", or ""T"". The original ""Set"" game can be viewed as ""Hyperset"" with \(k = 4\).Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
|
The first line of each test contains two integers \(n\) and \(k\) (\(1 \le n \le 1500\), \(1 \le k \le 30\)) β number of cards and number of features.Each of the following \(n\) lines contains a card description: a string consisting of \(k\) letters ""S"", ""E"", ""T"". The \(i\)-th character of this string decribes the \(i\)-th feature of that card. All cards are distinct.
|
Output a single integer β the number of ways to choose three cards that form a set.
|
In the third example test, these two triples of cards are sets: ""SETT"", ""TEST"", ""EEET"" ""TEST"", ""ESTE"", ""STES""
|
Input: 3 3 SET ETS TSE | Output: 1
|
Medium
| 3 | 1,122 | 376 | 83 | 12 |
1,286 |
D
|
1286D
|
D. LCC
| 3,100 |
data structures; math; matrices; probabilities
|
An infinitely long Line Chillland Collider (LCC) was built in Chillland. There are \(n\) pipes with coordinates \(x_i\) that are connected to LCC. When the experiment starts at time 0, \(i\)-th proton flies from the \(i\)-th pipe with speed \(v_i\). It flies to the right with probability \(p_i\) and flies to the left with probability \((1 - p_i)\). The duration of the experiment is determined as the time of the first collision of any two protons. In case there is no collision, the duration of the experiment is considered to be zero.Find the expected value of the duration of the experiment.Illustration for the first example
|
The first line of input contains one integer \(n\) β the number of pipes (\(1 \le n \le 10^5\)). Each of the following \(n\) lines contains three integers \(x_i\), \(v_i\), \(p_i\) β the coordinate of the \(i\)-th pipe, the speed of the \(i\)-th proton and the probability that the \(i\)-th proton flies to the right in percentage points (\(-10^9 \le x_i \le 10^9, 1 \le v \le 10^6, 0 \le p_i \le 100\)). It is guaranteed that all \(x_i\) are distinct and sorted in increasing order.
|
It's possible to prove that the answer can always be represented as a fraction \(P/Q\), where \(P\) is an integer and \(Q\) is a natural number not divisible by \(998\,244\,353\). In this case, print \(P \cdot Q^{-1}\) modulo \(998\,244\,353\).
|
Input: 2 1 1 100 3 1 0 | Output: 1
|
Master
| 4 | 630 | 483 | 244 | 12 |
|
840 |
D
|
840D
|
D. Destiny
| 2,500 |
data structures; probabilities
|
Once, Leha found in the left pocket an array consisting of n integers, and in the right pocket q queries of the form l r k. If there are queries, then they must be answered. Answer for the query is minimal x such that x occurs in the interval l r strictly more than times or - 1 if there is no such number. Help Leha with such a difficult task.
|
First line of input data contains two integers n and q (1 β€ n, q β€ 3Β·105) β number of elements in the array and number of queries respectively.Next line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β Leha's array.Each of next q lines contains three integers l, r and k (1 β€ l β€ r β€ n, 2 β€ k β€ 5) β description of the queries.
|
Output answer for each query in new line.
|
Input: 4 21 1 2 21 3 21 4 2 | Output: 1-1
|
Expert
| 2 | 344 | 329 | 41 | 8 |
|
1,672 |
E
|
1672E
|
E. notepad.exe
| 2,200 |
binary search; constructive algorithms; greedy; interactive
|
This is an interactive problem.There are \(n\) words in a text editor. The \(i\)-th word has length \(l_i\) (\(1 \leq l_i \leq 2000\)). The array \(l\) is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width \(w\). Let \(a\) be an array of length \(k+1\) where \(1=a_1 < a_2 < \ldots < a_{k+1}=n+1\). \(a\) is a valid array if for all \(1 \leq i \leq k\), \(l_{a_i}+1+l_{a_i+1}+1+\ldots+1+l_{a_{i+1}-1} \leq w\). Then the height of the text editor is the minimum \(k\) over all valid arrays.Note that if \(w < \max(l_i)\), the text editor cannot display all the words properly and will crash, and the height of the text editor will be \(0\) instead.You can ask \(n+30\) queries. In one query, you provide a width \(w\). Then, the grader will return the height \(h_w\) of the text editor when its width is \(w\).Find the minimum area of the text editor, which is the minimum value of \(w \cdot h_w\) over all \(w\) for which \(h_w \neq 0\).The lengths are fixed in advance. In other words, the interactor is not adaptive.
|
The first and only line of input contains a single integer \(n\) (\(1 \leq n \leq 2000\)) β the number of words on the text editor.It is guaranteed that the hidden lengths \(l_i\) satisfy \(1 \leq l_i \leq 2000\).
|
In the first test case, the words are \(\{\texttt{glory},\texttt{to},\texttt{ukraine},\texttt{and},\texttt{anton},\texttt{trygub}\}\), so \(l=\{5,2,7,3,5,6\}\). If \(w=1\), then the text editor is not able to display all words properly and will crash. The height of the text editor is \(h_1=0\), so the grader will return \(0\).If \(w=9\), then a possible way that the words will be displayed on the text editor is: \(\texttt{glory__to}\) \(\texttt{ukraine__}\) \(\texttt{and_anton}\) \(\texttt{__trygub_}\) The height of the text editor is \(h_{9}=4\), so the grader will return \(4\).If \(w=16\), then a possible way that the words will be displayed on the text editor is: \(\texttt{glory_to_ukraine}\) \(\texttt{and_anton_trygub}\) The height of the text editor is \(h_{16}=2\), so the grader will return \(2\).We have somehow figured out that the minimum area of the text editor is \(32\), so we answer it.
|
Input: 6 0 4 2 | Output: ? 1 ? 9 ? 16 ! 32
|
Hard
| 4 | 1,379 | 213 | 0 | 16 |
|
1,690 |
C
|
1690C
|
C. Restoring the Duration of Tasks
| 800 |
data structures; greedy; implementation
|
Recently, Polycarp completed \(n\) successive tasks.For each completed task, the time \(s_i\) is known when it was given, no two tasks were given at the same time. Also given is the time \(f_i\) when the task was completed. For each task, there is an unknown value \(d_i\) (\(d_i>0\)) β duration of task execution.It is known that the tasks were completed in the order in which they came. Polycarp performed the tasks as follows: As soon as the very first task came, Polycarp immediately began to carry it out. If a new task arrived before Polycarp finished the previous one, he put the new task at the end of the queue. When Polycarp finished executing the next task and the queue was not empty, he immediately took a new task from the head of the queue (if the queue is empty β he just waited for the next task). Find \(d_i\) (duration) of each task.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The descriptions of the input data sets follow.The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line of each test case contains exactly \(n\) integers \(s_1 < s_2 < \dots < s_n\) (\(0 \le s_i \le 10^9\)).The third line of each test case contains exactly \(n\) integers \(f_1 < f_2 < \dots < f_n\) (\(s_i < f_i \le 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each of \(t\) test cases print \(n\) positive integers \(d_1, d_2, \dots, d_n\) β the duration of each task.
|
First test case:The queue is empty at the beginning: \([ ]\). And that's where the first task comes in. At time \(2\), Polycarp finishes doing the first task, so the duration of the first task is \(2\). The queue is empty so Polycarp is just waiting.At time \(3\), the second task arrives. And at time \(7\), the third task arrives, and now the queue looks like this: \([7]\).At the time \(10\), Polycarp finishes doing the second task, as a result, the duration of the second task is \(7\).And at time \(10\), Polycarp immediately starts doing the third task and finishes at time \(11\). As a result, the duration of the third task is \(1\). An example of the first test case.
|
Input: 430 3 72 10 11210 1511 16912 16 90 195 1456 1569 3001 5237 1927513 199 200 260 9100 10000 10914 91066 5735533101000000000 | Output: 2 7 1 1 1 1 183 1 60 7644 900 914 80152 5644467 1000000000
|
Beginner
| 3 | 852 | 564 | 112 | 16 |
1,981 |
E
|
1981E
|
E. Turtle and Intersected Segments
| 2,600 |
data structures; dsu; graphs; greedy
|
Turtle just received \(n\) segments and a sequence \(a_1, a_2, \ldots, a_n\). The \(i\)-th segment is \([l_i, r_i]\).Turtle will create an undirected graph \(G\). If segment \(i\) and segment \(j\) intersect, then Turtle will add an undirected edge between \(i\) and \(j\) with a weight of \(|a_i - a_j|\), for every \(i \ne j\).Turtle wants you to calculate the sum of the weights of the edges of the minimum spanning tree of the graph \(G\), or report that the graph \(G\) has no spanning tree.We say two segments \([l_1, r_1]\) and \([l_2, r_2]\) intersect if and only if \(\max(l_1, l_2) \le \min(r_1, r_2)\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 5 \cdot 10^5\)) β the number of segments.The \(i\)-th of the following \(n\) lines contains three integers \(l_i, r_i, a_i\) (\(1 \le l_i \le r_i \le 10^9, 1 \le a_i \le 10^9\)) β the \(i\)-th segment and the \(i\)-th element of the sequence.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5 \cdot 10^5\).
|
For each test case, output a single integer β the sum of the weights of the edges of the minimum spanning tree of the graph \(G\). If the graph \(G\) has no spanning tree, output \(-1\).
|
In the first test case, the graph \(G\) is as follows: One of the minimum spanning trees of \(G\) is as follows: The sum of the weights of the edges of the minimum spanning tree is \(9\).In the second test case, the graph \(G\) is as follows: \(G\) is already a tree, and the sum of the weights of the tree is \(13\).In the third test case, the graph \(G\) is as follows: In the fourth test case, the graph \(G\) is as follows: It's easy to see that \(G\) is not connected, so \(G\) has no spanning tree.
|
Input: 451 7 32 4 63 5 56 7 93 4 452 7 31 3 64 5 56 7 91 1 441 4 31 2 13 4 51 4 431 3 12 3 34 5 8 | Output: 9 13 4 -1
|
Expert
| 4 | 613 | 575 | 186 | 19 |
1,242 |
D
|
1242D
|
D. Number Discovery
| 3,400 |
math
|
Ujan needs some rest from cleaning, so he started playing with infinite sequences. He has two integers \(n\) and \(k\). He creates an infinite sequence \(s\) by repeating the following steps. Find \(k\) smallest distinct positive integers that are not in \(s\). Let's call them \(u_{1}, u_{2}, \ldots, u_{k}\) from the smallest to the largest. Append \(u_{1}, u_{2}, \ldots, u_{k}\) and \(\sum_{i=1}^{k} u_{i}\) to \(s\) in this order. Go back to the first step. Ujan will stop procrastinating when he writes the number \(n\) in the sequence \(s\). Help him find the index of \(n\) in \(s\). In other words, find the integer \(x\) such that \(s_{x} = n\). It's possible to prove that all positive integers are included in \(s\) only once.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^{5}\)), the number of test cases.Each of the following \(t\) lines contains two integers \(n\) and \(k\) (\(1 \le n \le 10^{18}\), \(2 \le k \le 10^{6}\)), the number to be found in the sequence \(s\) and the parameter used to create the sequence \(s\).
|
In each of the \(t\) lines, output the answer for the corresponding test case.
|
In the first sample, \(s = (1, 2, 3, 4, 5, 9, 6, 7, 13, 8, 10, 18, \ldots)\). \(10\) is the \(11\)-th number here, so the answer is \(11\).In the second sample, \(s = (1, 2, 3, 4, 5, 15, 6, 7, 8, 9, 10, 40, \ldots)\).
|
Input: 2 10 2 40 5 | Output: 11 12
|
Master
| 1 | 738 | 318 | 78 | 12 |
2,130 |
A
|
2130A
|
A. Submission is All You Need
| 800 |
greedy; math
|
For a multiset \(T\) consisting of non-negative integers, we define: \(\text{sum}(T)\) is the sum of all elements in \(T\). For example, if \(T = \{0,1, 1, 3\}\), then \(\text{sum}(T)= 0+1+1+3=5\). \(\text{mex}(T)\) is the smallest non-negative integer not in \(T\). For example, if \(T = \{0,1, 1, 3\}\), then \(\text{mex}(T) = 2\) because \(2\) is the smallest non-negative integer not present in \(T\). You are given a multiset \(S\) of size \(n\) consisting of non-negative integers. At first, your score is \(0\). You can perform the following operations any number of times in any order (possibly zero): Select a subset \(S' \subseteq S\) (i.e., \(S'\) contains some of the elements currently in \(S\)), add \(\text{sum}(S')\) to your score, and then remove \(S'\) from \(S\). Select a subset \(S' \subseteq S\), add \(\text{mex}(S')\) to your score, and then remove \(S'\) from \(S\). Find the maximum possible score that can be obtained.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 50\)).The second line of each test case contains \(n\) integers \(S_1, S_2, \ldots, S_n\) (\(0 \le S_i \le 50\)).
|
For each test case, print a single integer β the maximum possible score that can be obtained.
|
In the first test case, a possible optimal strategy is as follows: Select \(S'=\{0,1\}\), add \(\text{mex}(S')=\text{mex}(\{0,1\})=2\) to your score, and then remove \(S'\) from \(S\). Currently, your score is \(2\) and \(S=\{1\}\). Select \(S'=\{1\}\), add \(\text{sum}(S')=\text{sum}(\{1\})=1\) to your score, and then remove \(S'\) from \(S\). Currently, your score is \(3\) and \(S=\varnothing\). After that, you cannot do any further operations. It can be proven that \(3\) is the maximum possible score that can be obtained.
|
Input: 230 1 131 2 3 | Output: 3 6
|
Beginner
| 2 | 945 | 355 | 93 | 21 |
1,215 |
E
|
1215E
|
E. Marbles
| 2,200 |
bitmasks; dp
|
Monocarp has arranged \(n\) colored marbles in a row. The color of the \(i\)-th marble is \(a_i\). Monocarp likes ordered things, so he wants to rearrange marbles in such a way that all marbles of the same color form a contiguos segment (and there is only one such segment for each color). In other words, Monocarp wants to rearrange marbles so that, for every color \(j\), if the leftmost marble of color \(j\) is \(l\)-th in the row, and the rightmost marble of this color has position \(r\) in the row, then every marble from \(l\) to \(r\) has color \(j\).To achieve his goal, Monocarp can do the following operation any number of times: choose two neighbouring marbles, and swap them.You have to calculate the minimum number of operations Monocarp has to perform to rearrange the marbles. Note that the order of segments of marbles having equal color does not matter, it is only required that, for every color, all the marbles of this color form exactly one contiguous segment.
|
The first line contains one integer \(n\) \((2 \le n \le 4 \cdot 10^5)\) β the number of marbles.The second line contains an integer sequence \(a_1, a_2, \dots, a_n\) \((1 \le a_i \le 20)\), where \(a_i\) is the color of the \(i\)-th marble.
|
Print the minimum number of operations Monocarp has to perform to achieve his goal.
|
In the first example three operations are enough. Firstly, Monocarp should swap the third and the fourth marbles, so the sequence of colors is \([3, 4, 3, 2, 4, 2, 2]\). Then Monocarp should swap the second and the third marbles, so the sequence is \([3, 3, 4, 2, 4, 2, 2]\). And finally, Monocarp should swap the fourth and the fifth marbles, so the sequence is \([3, 3, 4, 4, 2, 2, 2]\). In the second example there's no need to perform any operations.
|
Input: 7 3 4 2 3 4 2 2 | Output: 3
|
Hard
| 2 | 982 | 241 | 83 | 12 |
1,906 |
E
|
1906E
|
E. Merge Not Sort
| 1,900 |
constructive algorithms; dp
|
You are currently researching the Merge Sort algorithm. Merge Sort is a sorting algorithm that is based on the principle of Divide and Conquer. It works by dividing an array into two subarrays of equal length, sorting each subarrays, then merging the sorted subarrays back together to form the final sorted array.You are particularly interested in the merging routine. Common merge implementation will combine two subarrays by iteratively comparing their first elements, and move the smaller one to a new merged array. More precisely, the merge algorithm can be presented by the following pseudocode. Merge(A[1..N], B[1..N]): C = [] i = 1 j = 1 while i <= N AND j <= N: if A[i] < B[j]: append A[i] to C i = i + 1 else: append B[j] to C j = j + 1 while i <= N: append A[i] to C i = i + 1 while j <= N: append B[j] to C j = j + 1 return CDuring your research, you are keen to understand the behaviour of the merge algorithm when arrays \(A\) and \(B\) are not necessarily sorted. For example, if \(A = [3, 1, 6]\) and \(B = [4, 5, 2]\), then \(\text{Merge}(A, B) = [3, 1, 4, 5, 2, 6]\).To further increase the understanding of the merge algorithm, you decided to work on the following problem. You are given an array \(C\) of length \(2 \cdot N\) such that it is a permutation of \(1\) to \(2 \cdot N\). Construct any two arrays \(A\) and \(B\) of the same length \(N\), such that \(\text{Merge}(A, B) = C\), or determine if it is impossible to do so.
|
The first line consists of an integer \(N\) (\(1 \leq N \leq 1000\)).The following line consists of \(2 \cdot N\) integers \(C_i\). The array \(C\) is a permutation of \(1\) to \(2 \cdot N\).
|
If it is impossible to construct two arrays \(A\) and \(B\) of length \(N\) such that \(\text{Merge}(A, B) = C\), then output -1.Otherwise, output the arrays \(A\) and \(B\) in two lines. The first line consists of \(N\) integers \(A_i\). The second line consists of \(N\) integers \(B_i\). If there are several possible answers, output any of them.
|
Explanation for the sample input/output #1The solution \(A = [3, 1, 4]\) and \(B = [5, 2, 6]\) is also correct. Explanation for the sample input/output #2The solution \(A = [1, 2, 3, 4]\) and \(B = [5, 6, 7, 8]\) is also correct.
|
Input: 3 3 1 4 5 2 6 | Output: 3 1 6 4 5 2
|
Hard
| 2 | 1,449 | 191 | 349 | 19 |
1,672 |
I
|
1672I
|
I. PermutationForces
| 3,000 |
data structures; greedy
|
You have a permutation \(p\) of integers from \(1\) to \(n\).You have a strength of \(s\) and will perform the following operation some times: Choose an index \(i\) such that \(1 \leq i \leq |p|\) and \(|i-p_i| \leq s\). For all \(j\) such that \(1 \leq j \leq |p|\) and \(p_i<p_j\), update \(p_j\) to \(p_j-1\). Delete the \(i\)-th element from \(p\). Formally, update \(p\) to \([p_1,\ldots,p_{i-1},p_{i+1},\ldots,p_n]\). It can be shown that no matter what \(i\) you have chosen, \(p\) will be a permutation of integers from \(1\) to \(|p|\) after all operations.You want to be able to transform \(p\) into the empty permutation. Find the minimum strength \(s\) that will allow you to do so.
|
The first line of input contains a single integer \(n\) (\(1 \leq n \leq 5 \cdot 10^5\)) β the length of the permutation \(p\).The second line of input conatains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq n\)) β the elements of the permutation \(p\).It is guaranteed that all elements in \(p\) are distinct.
|
Print the minimum strength \(s\) required.
|
In the first test case, the minimum \(s\) required is \(1\).Here is how we can transform \(p\) into the empty permutation with \(s=1\): In the first move, you can only choose \(i=2\) as choosing any other value of \(i\) will result in \(|i-p_i| \leq s\) being false. With \(i=2\), \(p\) will be changed to \([2,1]\). In the second move, you choose \(i=1\), then \(p\) will be changed to \([1]\). In the third move, you choose \(i=1\), then \(p\) will be changed to \([~]\). It can be shown that with \(s=0\), it is impossible to transform \(p\) into the empty permutation.
|
Input: 3 3 2 1 | Output: 1
|
Master
| 2 | 694 | 324 | 42 | 16 |
1,907 |
E
|
1907E
|
E. Good Triples
| 1,600 |
brute force; combinatorics; number theory
|
Given a non-negative integer number \(n\) (\(n \ge 0\)). Let's say a triple of non-negative integers \((a, b, c)\) is good if \(a + b + c = n\), and \(digsum(a) + digsum(b) + digsum(c) = digsum(n)\), where \(digsum(x)\) is the sum of digits of number \(x\).For example, if \(n = 26\), then the pair \((4, 12, 10)\) is good, because \(4 + 12 + 10 = 26\), and \((4) + (1 + 2) + (1 + 0) = (2 + 6)\).Your task is to find the number of good triples for the given number \(n\). The order of the numbers in a triple matters. For example, the triples \((4, 12, 10)\) and \((10, 12, 4)\) are two different triples.
|
The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Descriptions of test cases follow.The first and only line of the test case contains one integer \(n\) (\(0 \le n \le 10^7\)).
|
For each test case output one integer, the number of good triples for the given integer \(n\). Order of integers in a triple matters.
|
In the first example, the good triples are \((0, 0, 11)\), \((0, 1, 10)\), \((0, 10, 1)\), \((0, 11, 0)\), \((1, 0, 10)\), \((1, 10, 0)\), \((10, 0, 1)\), \((10, 1, 0)\), \((11, 0, 0)\).In the second example, there is only one good triple \((0, 0, 0)\).
|
Input: 121101234531419992718999999910000000 | Output: 9 1 3 6 10 15 21 1350 166375 29160 1522435234375 3
|
Medium
| 3 | 605 | 232 | 133 | 19 |
268 |
B
|
268B
|
B. Buttons
| 1,000 |
implementation; math
|
Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you should press the buttons in a certain order to open the lock. When you push some button, it either stays pressed into the lock (that means that you've guessed correctly and pushed the button that goes next in the sequence), or all pressed buttons return to the initial position. When all buttons are pressed into the lock at once, the lock opens.Consider an example with three buttons. Let's say that the opening sequence is: {2, 3, 1}. If you first press buttons 1 or 3, the buttons unpress immediately. If you first press button 2, it stays pressed. If you press 1 after 2, all buttons unpress. If you press 3 after 2, buttons 3 and 2 stay pressed. As soon as you've got two pressed buttons, you only need to press button 1 to open the lock.Manao doesn't know the opening sequence. But he is really smart and he is going to act in the optimal way. Calculate the number of times he's got to push a button in order to open the lock in the worst-case scenario.
|
A single line contains integer n (1 β€ n β€ 2000) β the number of buttons the lock has.
|
In a single line print the number of times Manao has to push a button in the worst-case scenario.
|
Consider the first test sample. Manao can fail his first push and push the wrong button. In this case he will already be able to guess the right one with his second push. And his third push will push the second right button. Thus, in the worst-case scenario he will only need 3 pushes.
|
Input: 2 | Output: 3
|
Beginner
| 2 | 1,059 | 85 | 97 | 2 |
2,095 |
I
|
2095I
|
I. Mysterious Script
| 0 |
*special; expression parsing; number theory
|
An extraterrestrial species has visited the authors of April Fools Day Contest 2025 while they were preparing problems. They call themselves the ""Balikons"" and expressed interest in chatting with humans. Although they can make sounds that are similar to human speech, they seem to prefer communicating by writing using a variety of media. Figure 1. A Balikon entity communicates with a human linguist (2025, colorized) Fortunately, there is a linguistics expert in the author team. With some effort, they were eventually able to fully understand the Balikon language. The following is a table of some example phrases they translated. Balikon scriptLatin scriptTranslationbalikonBalikonbalikong shasthree Balikonsfibucho shezali kin chakog leshasbuy shezali\(^{\text{β}}\) for twelve chakos\(^{\text{β }}\)fibuchog zinfa bashin(I) bought the wrong penshons ze tes to les konbilo lonshonletashashaleteshatesfive plus seven minus one modulo 998244358 Table 1. A few pieces of text in the Balikon language After some conversations, the Balikons learned about a popular human activity called competitive programming and were excited to try. In fact, they just created their first problem. Please check it out!\(^{\text{β}}\)a dish in traditional Balikon cuisine, similar to meatballs\(^{\text{β }}\)currency unit
|
The input contains two integers \(a\) and \(b\) (\(1 \le a, b \le 10^9\)).
|
Print the sum \(a + b\).
|
Input: shons tes | Output: leshas
|
Beginner
| 3 | 1,307 | 74 | 24 | 20 |
|
786 |
E
|
786E
|
E. ALT
| 3,200 |
data structures; flows; graphs; trees
|
ALT is a planet in a galaxy called ""Encore"". Humans rule this planet but for some reason there's no dog in their planet, so the people there are sad and depressed. Rick and Morty are universal philanthropists and they want to make people in ALT happy. ALT has n cities numbered from 1 to n and n - 1 bidirectional roads numbered from 1 to n - 1. One can go from any city to any other city using these roads.There are two types of people in ALT: Guardians. A guardian lives in a house alongside a road and guards the road. Citizens. A citizen lives in a house inside a city and works in an office in another city. Every person on ALT is either a guardian or a citizen and there's exactly one guardian alongside each road. Rick and Morty talked to all the people in ALT, and here's what they got: There are m citizens living in ALT. Citizen number i lives in city number xi and works in city number yi. Every day each citizen will go through all roads along the shortest path from his home to his work. A citizen will be happy if and only if either he himself has a puppy himself or all of guardians along his path to his work has a puppy (he sees the guardian's puppy in each road and will be happy). A guardian is always happy. You need to tell Rick and Morty the minimum number of puppies they need in order to make all people in ALT happy, and also provide an optimal way to distribute these puppies.
|
The first line of input contains two integers n and m (2 β€ n β€ 2 Γ 104, 1 β€ m β€ 104) β number of cities and number of citizens respectively.The next n - 1 lines contain the roads, i-th line contains endpoint of i-th edge, v and u (1 β€ v, u β€ n, v β u).The next m lines contain the information about citizens. i-th line contains two integers xi and yi (1 β€ xi, yi β€ n, xi β yi).
|
In the first line of input print a single integer k, the total number of puppies they need (1 β€ k β€ n).In the second line print an integer q, the number of puppies to give to citizens, followed by q distinct integers a1, a2, ..., aq, index of citizens to give puppy to (0 β€ q β€ min(m, k), 1 β€ ai β€ m).In the third line print an integer e, the number of puppies to give to guardians, followed by e distinct integers b1, b2, ..., be, index of road of guardians to give puppy to (0 β€ e β€ min(n - 1, k), 1 β€ bi β€ n - 1).Sum of q and e should be equal to k.
|
Map of ALT in the first sample testcase (numbers written on a road is its index): Map of ALT in the second sample testcase (numbers written on a road is its index):
|
Input: 4 52 43 41 42 42 12 41 22 3 | Output: 31 5 2 3 1
|
Master
| 4 | 1,404 | 377 | 552 | 7 |
234 |
H
|
234H
|
H. Merging Two Decks
| 2,000 |
constructive algorithms; greedy
|
There are two decks of cards lying on the table in front of you, some cards in these decks lay face up, some of them lay face down. You want to merge them into one deck in which each card is face down. You're going to do it in two stages.The first stage is to merge the two decks in such a way that the relative order of the cards from the same deck doesn't change. That is, for any two different cards i and j in one deck, if card i lies above card j, then after the merge card i must also be above card j.The second stage is performed on the deck that resulted from the first stage. At this stage, the executed operation is the turning operation. In one turn you can take a few of the top cards, turn all of them, and put them back. Thus, each of the taken cards gets turned and the order of these cards is reversed. That is, the card that was on the bottom before the turn, will be on top after it.Your task is to make sure that all the cards are lying face down. Find such an order of merging cards in the first stage and the sequence of turning operations in the second stage, that make all the cards lie face down, and the number of turns is minimum.
|
The first input line contains a single integer n β the number of cards in the first deck (1 β€ n β€ 105).The second input line contains n integers, separated by single spaces a1, a2, ..., an (0 β€ ai β€ 1). Value ai equals 0, if the i-th card is lying face down, and 1, if the card is lying face up. The cards are given in the order from the topmost one to the bottommost one.The third input line contains integer m β the number of cards in the second deck (1 β€ m β€ 105).The fourth input line contains m integers, separated by single spaces b1, b2, ..., bm (0 β€ bi β€ 1). Value bi equals 0, if the i-th card is lying face down, and 1, if the card is lying face up. The cards are given in the order from the topmost to the bottommost.
|
In the first line print n + m space-separated integers β the numbers of the cards in the order, in which they will lie after the first stage. List the cards from top to bottom. The cards from the first deck should match their indexes from 1 to n in the order from top to bottom. The cards from the second deck should match their indexes, increased by n, that is, numbers from n + 1 to n + m in the order from top to bottom.In the second line print a single integer x β the minimum number of turn operations you need to make all cards in the deck lie face down. In the third line print x integers: c1, c2, ..., cx (1 β€ ci β€ n + m), each of them represents the number of cards to take from the top of the deck to perform a turn operation. Print the operations in the order, in which they should be performed. If there are multiple optimal solutions, print any of them. It is guaranteed that the minimum number of operations doesn't exceed 6Β·105.
|
Input: 31 0 141 1 1 1 | Output: 1 4 5 6 7 2 335 6 7
|
Hard
| 2 | 1,156 | 728 | 943 | 2 |
|
380 |
A
|
380A
|
A. Sereja and Prefixes
| 1,600 |
binary search; brute force
|
Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm.Sereja takes a blank piece of paper. Then he starts writing out the sequence in m stages. Each time he either adds a new number to the end of the sequence or takes l first elements of the current sequence and adds them c times to the end. More formally, if we represent the current sequence as a1, a2, ..., an, then after we apply the described operation, the sequence transforms into a1, a2, ..., an[, a1, a2, ..., al] (the block in the square brackets must be repeated c times). A day has passed and Sereja has completed the sequence. He wonders what are the values of some of its elements. Help Sereja.
|
The first line contains integer m (1 β€ m β€ 105) β the number of stages to build a sequence. Next m lines contain the description of the stages in the order they follow. The first number in the line is a type of stage (1 or 2). Type 1 means adding one number to the end of the sequence, in this case the line contains integer xi (1 β€ xi β€ 105) β the number to add. Type 2 means copying a prefix of length li to the end ci times, in this case the line further contains two integers li, ci (1 β€ li β€ 105, 1 β€ ci β€ 104), li is the length of the prefix, ci is the number of copyings. It is guaranteed that the length of prefix li is never larger than the current length of the sequence.The next line contains integer n (1 β€ n β€ 105) β the number of elements Sereja is interested in. The next line contains the numbers of elements of the final sequence Sereja is interested in. The numbers are given in the strictly increasing order. It is guaranteed that all numbers are strictly larger than zero and do not exceed the length of the resulting sequence. Consider the elements of the final sequence numbered starting from 1 from the beginning to the end of the sequence.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
Print the elements that Sereja is interested in, in the order in which their numbers occur in the input.
|
Input: 61 11 22 2 11 32 5 21 4161 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Output: 1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4
|
Medium
| 2 | 724 | 1,310 | 104 | 3 |
|
914 |
H
|
914H
|
H. Ember and Storm's Tree Game
| 3,400 |
combinatorics; dp; games; trees
|
Ember and Storm play a game. First, Ember picks a labelled tree T of n vertices, such that the degree of every vertex is at most d. Then, Storm picks two distinct vertices u and v in this tree and writes down the labels of the vertices in the path from u to v in a sequence a1, a2... ak. Finally, Ember picks any index i (1 β€ i < k) in the array. Now he performs one of the following two operations exactly once: flip the subrange [i + 1, k] and add ai to it. After this, the sequence becomes a1, ... ai, ak + ai, ak - 1 + ai, ... ai + 1 + ai negate the subrange [i + 1, k] and add ai to it. i.e., the array becomes a1, ... ai, - ai + 1 + ai, - ai + 2 + ai, ... - ak + ai Ember wins if the array is monotonically increasing or decreasing after this. Otherwise Storm wins.The game can be described by the tuple (T, u, v, i, op) where op is Β«flipΒ» or Β«negateΒ» depending on the action Ember chose in the last turn. Find the number of tuples that can occur if Ember and Storm play optimally. When they play optimally, if there are multiple moves by which they are guaranteed to win, then they may play any of the winning moves. Otherwise, if someone loses no matter what they play, then they may play any of the possible moves.Report the answer modulo m.
|
The input consists of a single line containing three integers n, d and m (2 β€ n β€ 200, 1 β€ d < n, 1 β€ m β€ 2Β·109).
|
Print a single number β the number of possible tuples if Ember and Storm play as described, modulo m.
|
In the first sample case, there is only one possible tree. There are two possible paths, 1 to 2 and 2 to 1. For both paths, i can only be 1, and op can take both possibilities. Therefore, the answer is 4.In the second sample, there are no possible trees.In the third sample, there are three possible trees.
|
Input: 2 1 1000000007 | Output: 4
|
Master
| 4 | 1,250 | 113 | 101 | 9 |
1,331 |
E
|
1331E
|
E. Jordan Smiley
| 0 |
*special; dfs and similar; geometry; implementation
|
The input contains two integers \(row\), \(col\) (\(0 \le row, col \le 63\)), separated by a single space.
|
Output ""IN"" or ""OUT"".
|
Input: 0 0 | Output: OUT
|
Beginner
| 4 | 0 | 106 | 25 | 13 |
||
1,023 |
C
|
1023C
|
C. Bracket Subsequence
| 1,200 |
greedy
|
A bracket sequence is a string containing only characters ""("" and "")"". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters ""1"" and ""+"" between the original characters of the sequence. For example, bracket sequences ""()()"" and ""(())"" are regular (the resulting expressions are: ""(1)+(1)"" and ""((1+1)+1)""), and "")("", ""("" and "")"" are not.Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You are given a regular bracket sequence \(s\) and an integer number \(k\). Your task is to find a regular bracket sequence of length exactly \(k\) such that it is also a subsequence of \(s\).It is guaranteed that such sequence always exists.
|
The first line contains two integers \(n\) and \(k\) (\(2 \le k \le n \le 2 \cdot 10^5\), both \(n\) and \(k\) are even) β the length of \(s\) and the length of the sequence you are asked to find.The second line is a string \(s\) β regular bracket sequence of length \(n\).
|
Print a single string β a regular bracket sequence of length exactly \(k\) such that it is also a subsequence of \(s\).It is guaranteed that such sequence always exists.
|
Input: 6 4()(()) | Output: ()()
|
Easy
| 1 | 828 | 273 | 169 | 10 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.