contest_id
int32 1
2.13k
| index
stringclasses 62
values | problem_id
stringlengths 2
6
| title
stringlengths 0
67
| rating
int32 0
3.5k
| tags
stringlengths 0
139
| statement
stringlengths 0
6.96k
| input_spec
stringlengths 0
2.32k
| output_spec
stringlengths 0
1.52k
| note
stringlengths 0
5.06k
| sample_tests
stringlengths 0
1.02k
| difficulty_category
stringclasses 6
values | tag_count
int8 0
11
| statement_length
int32 0
6.96k
| input_spec_length
int16 0
2.32k
| output_spec_length
int16 0
1.52k
| contest_year
int16 0
21
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,732 |
C1
|
1732C1
|
C1. Sheikh (Easy version)
| 1,800 |
binary search; bitmasks; greedy; two pointers
|
This is the easy version of the problem. The only difference is that in this version \(q = 1\).You are given an array of integers \(a_1, a_2, \ldots, a_n\).The cost of a subsegment of the array \([l, r]\), \(1 \leq l \leq r \leq n\), is the value \(f(l, r) = \operatorname{sum}(l, r) - \operatorname{xor}(l, r)\), where \(\operatorname{sum}(l, r) = a_l + a_{l+1} + \ldots + a_r\), and \(\operatorname{xor}(l, r) = a_l \oplus a_{l+1} \oplus \ldots \oplus a_r\) (\(\oplus\) stands for bitwise XOR).You will have \(q = 1\) query. Each query is given by a pair of numbers \(L_i\), \(R_i\), where \(1 \leq L_i \leq R_i \leq n\). You need to find the subsegment \([l, r]\), \(L_i \leq l \leq r \leq R_i\), with maximum value \(f(l, r)\). If there are several answers, then among them you need to find a subsegment with the minimum length, that is, the minimum value of \(r - l + 1\).
|
Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \leq n \leq 10^5\), \(q = 1\)) β the length of the array and the number of queries.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 10^9\)) β array elements.\(i\)-th of the next \(q\) lines of each test case contains two integers \(L_i\) and \(R_i\) (\(1 \leq L_i \leq R_i \leq n\)) β the boundaries in which we need to find the segment.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).It is guaranteed that \(L_1 = 1\) and \(R_1 = n\).
|
For each test case print \(q\) pairs of numbers \(L_i \leq l \leq r \leq R_i\) such that the value \(f(l, r)\) is maximum and among such the length \(r - l + 1\) is minimum. If there are several correct answers, print any of them.
|
In the first test case, \(f(1, 1) = 0 - 0 = 0\).In the second test case, \(f(1, 1) = 5 - 5 = 0\), \(f(2, 2) = 10 - 10 = 0\). Note that \(f(1, 2) = (10 + 5) - (10 \oplus 5) = 0\), but we need to find a subsegment with the minimum length among the maximum values of \(f(l, r)\). So, only segments \([1, 1]\) and \([2, 2]\) are the correct answers.In the fourth test case, \(f(2, 3) = (12 + 8) - (12 \oplus 8) = 16\). There are two correct answers in the fifth test case, since \(f(2, 3) = f(3, 4)\) and their lengths are equal.
|
Input: 61 101 12 15 101 23 10 2 41 34 10 12 8 31 45 121 32 32 32 101 57 10 1 0 1 0 1 01 7 | Output: 1 1 1 1 1 1 2 3 2 3 2 4
|
Medium
| 4 | 877 | 784 | 230 | 17 |
241 |
B
|
241B
|
B. Friends
| 2,700 |
binary search; bitmasks; data structures; math
|
You have n friends and you want to take m pictures of them. Exactly two of your friends should appear in each picture and no two pictures should contain the same pair of your friends. So if you have n = 3 friends you can take 3 different pictures, each containing a pair of your friends.Each of your friends has an attractiveness level which is specified by the integer number ai for the i-th friend. You know that the attractiveness of a picture containing the i-th and the j-th friends is equal to the exclusive-or (xor operation) of integers ai and aj.You want to take pictures in a way that the total sum of attractiveness of your pictures is maximized. You have to calculate this value. Since the result may not fit in a 32-bit integer number, print it modulo 1000000007 (109 + 7).
|
The first line of input contains two integers n and m β the number of friends and the number of pictures that you want to take. Next line contains n space-separated integers a1, a2, ..., an (0 β€ ai β€ 109) β the values of attractiveness of the friends.
|
The only line of output should contain an integer β the optimal total sum of attractiveness of your pictures.
|
Input: 3 11 2 3 | Output: 3
|
Master
| 4 | 786 | 251 | 109 | 2 |
|
802 |
K
|
802K
|
K. Send the Fool Further! (medium)
| 2,100 |
dp; trees
|
Thank you for helping Heidi! It is now the second of April, but she has been summoned by Jenny again. The pranks do not seem to end...In the meantime, Heidi has decided that she does not trust her friends anymore. Not too much, anyway. Her relative lack of trust is manifested as follows: whereas previously she would not be made to visit the same person twice, now she can only be sure that she will not be made to visit the same person more than k times. (In the case of Jenny, this includes her first visit in the beginning. The situation from the easy version corresponds to setting k = 1.)This is not as bad as it looks, since a single ticket for a route between two friends allows Heidi to travel between this pair of friends the whole day (in both directions). In other words, once she pays for travel between a pair of friends, all further travels between that pair are free.How much money will Heidi waste now, in a worst-case scenario?
|
The first line contains two space-separated integers β the number of friends n () and the parameter k (1 β€ k β€ 105). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104) meaning that u and v are friends and the cost for traveling between u and v is c.It is again guaranteed that the social network of the input forms a tree.
|
Again, output a single integer β the maximum sum of costs of tickets.
|
In the first example, the worst-case scenario for Heidi is to visit the friends in the following order: 0, 1, 5, 1, 3, 1, 0, 2, 6, 2, 7, 2, 8. Observe that no friend is visited more than 3 times.
|
Input: 9 30 1 10 2 11 3 21 4 21 5 22 6 32 7 32 8 3 | Output: 15
|
Hard
| 2 | 945 | 379 | 69 | 8 |
1,244 |
G
|
1244G
|
G. Running in Pairs
| 2,400 |
constructive algorithms; greedy; math
|
Demonstrative competitions will be held in the run-up to the \(20NN\) Berlatov Olympic Games. Today is the day for the running competition!Berlatov team consists of \(2n\) runners which are placed on two running tracks; \(n\) runners are placed on each track. The runners are numbered from \(1\) to \(n\) on each track. The runner with number \(i\) runs through the entire track in \(i\) seconds.The competition is held as follows: first runners on both tracks start running at the same time; when the slower of them arrives at the end of the track, second runners on both tracks start running, and everyone waits until the slower of them finishes running, and so on, until all \(n\) pairs run through the track.The organizers want the run to be as long as possible, but if it lasts for more than \(k\) seconds, the crowd will get bored. As the coach of the team, you may choose any order in which the runners are arranged on each track (but you can't change the number of runners on each track or swap runners between different tracks).You have to choose the order of runners on each track so that the duration of the competition is as long as possible, but does not exceed \(k\) seconds.Formally, you want to find two permutations \(p\) and \(q\) (both consisting of \(n\) elements) such that \(sum = \sum\limits_{i=1}^{n} max(p_i, q_i)\) is maximum possible, but does not exceed \(k\). If there is no such pair, report about it.
|
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 10^6, 1 \le k \le n^2\)) β the number of runners on each track and the maximum possible duration of the competition, respectively.
|
If it is impossible to reorder the runners so that the duration of the competition does not exceed \(k\) seconds, print \(-1\). Otherwise, print three lines. The first line should contain one integer \(sum\) β the maximum possible duration of the competition not exceeding \(k\). The second line should contain a permutation of \(n\) integers \(p_1, p_2, \dots, p_n\) (\(1 \le p_i \le n\), all \(p_i\) should be pairwise distinct) β the numbers of runners on the first track in the order they participate in the competition. The third line should contain a permutation of \(n\) integers \(q_1, q_2, \dots, q_n\) (\(1 \le q_i \le n\), all \(q_i\) should be pairwise distinct) β the numbers of runners on the second track in the order they participate in the competition. The value of \(sum = \sum\limits_{i=1}^{n} max(p_i, q_i)\) should be maximum possible, but should not exceed \(k\). If there are multiple answers, print any of them.
|
In the first example the order of runners on the first track should be \([5, 3, 2, 1, 4]\), and the order of runners on the second track should be \([1, 4, 2, 5, 3]\). Then the duration of the competition is \(max(5, 1) + max(3, 4) + max(2, 2) + max(1, 5) + max(4, 3) = 5 + 4 + 2 + 5 + 4 = 20\), so it is equal to the maximum allowed duration.In the first example the order of runners on the first track should be \([2, 3, 1]\), and the order of runners on the second track should be \([2, 1, 3]\). Then the duration of the competition is \(8\), and it is the maximum possible duration for \(n = 3\).
|
Input: 5 20 | Output: 20 1 2 3 4 5 5 2 4 3 1
|
Expert
| 3 | 1,431 | 198 | 935 | 12 |
28 |
E
|
28E
|
E. DravDe saves the world
| 2,800 |
geometry; math
|
How horrible! The empire of galactic chickens tries to conquer a beautiful city ""Z"", they have built a huge incubator that produces millions of chicken soldiers a day, and fenced it around. The huge incubator looks like a polygon on the plane Oxy with n vertices. Naturally, DravDe can't keep still, he wants to destroy the chicken empire. For sure, he will start with the incubator.DravDe is strictly outside the incubator's territory in point A(xa, ya), and wants to get inside and kill all the chickens working there. But it takes a lot of doing! The problem is that recently DravDe went roller skating and has broken both his legs. He will get to the incubator's territory in his jet airplane LEVAP-41.LEVAP-41 flies at speed V(xv, yv, zv). DravDe can get on the plane in point A, fly for some time, and then air drop himself. DravDe is very heavy, that's why he falls vertically at speed Fdown, but in each point of his free fall DravDe can open his parachute, and from that moment he starts to fall at the wind speed U(xu, yu, zu) until he lands. Unfortunately, DravDe isn't good at mathematics. Would you help poor world's saviour find such an air dropping plan, that allows him to land on the incubator's territory? If the answer is not unique, DravDe wants to find the plan with the minimum time of his flight on the plane. If the answers are still multiple, he wants to find the one with the minimum time of his free fall before opening his parachute
|
The first line contains the number n (3 β€ n β€ 104) β the amount of vertices of the fence. Then there follow n lines containing the coordinates of these vertices (two integer numbers xi, yi) in clockwise or counter-clockwise order. It's guaranteed, that the fence does not contain self-intersections.The following four lines contain coordinates of point A(xa, ya), speeds V(xv, yv, zv), Fdown and speed U(xu, yu, zu). All the input numbers are integer. All the coordinates don't exceed 104 in absolute value. It's guaranteed, that zv > 0 and Fdown, zu < 0, and point A is strictly outside the incubator's territory.
|
In the first line output two numbers t1, t2 such, that if DravDe air drops at time t1 (counting from the beginning of the flight), he lands on the incubator's territory (landing on the border is regarder as landing on the territory). If DravDe doesn't open his parachute, the second number should be equal to the duration of DravDe's falling down. If it's impossible for DravDe to get to the incubator's territory, output -1 -1. If the answer is not unique, output the answer with the minimum t1. If the answers are still multiple, output the answer with the minimum t2. Your answer must have an absolute or relative error less than 10 - 6.
|
Input: 40 01 01 10 10 -11 0 1-10 1 -1 | Output: 1.00000000 0.00000000
|
Master
| 2 | 1,462 | 614 | 640 | 0 |
|
991 |
C
|
991C
|
C. Candies
| 1,500 |
binary search; implementation
|
After passing a test, Vasya got himself a box of \(n\) candies. He decided to eat an equal amount of candies each morning until there are no more candies. However, Petya also noticed the box and decided to get some candies for himself.This means the process of eating candies is the following: in the beginning Vasya chooses a single integer \(k\), same for all days. After that, in the morning he eats \(k\) candies from the box (if there are less than \(k\) candies in the box, he eats them all), then in the evening Petya eats \(10\%\) of the candies remaining in the box. If there are still candies left in the box, the process repeats β next day Vasya eats \(k\) candies again, and Petya β \(10\%\) of the candies left in a box, and so on.If the amount of candies in the box is not divisible by \(10\), Petya rounds the amount he takes from the box down. For example, if there were \(97\) candies in the box, Petya would eat only \(9\) of them. In particular, if there are less than \(10\) candies in a box, Petya won't eat any at all.Your task is to find out the minimal amount of \(k\) that can be chosen by Vasya so that he would eat at least half of the \(n\) candies he initially got. Note that the number \(k\) must be integer.
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^{18}\)) β the initial amount of candies in the box.
|
Output a single integer β the minimal amount of \(k\) that would allow Vasya to eat at least half of candies he got.
|
In the sample, the amount of candies, with \(k=3\), would change in the following way (Vasya eats first):\(68 \to 65 \to 59 \to 56 \to 51 \to 48 \to 44 \to 41 \\ \to 37 \to 34 \to 31 \to 28 \to 26 \to 23 \to 21 \to 18 \to 17 \to 14 \\ \to 13 \to 10 \to 9 \to 6 \to 6 \to 3 \to 3 \to 0\).In total, Vasya would eat \(39\) candies, while Petya β \(29\).
|
Input: 68 | Output: 3
|
Medium
| 2 | 1,238 | 118 | 116 | 9 |
178 |
D3
|
178D3
|
D3. Magic Squares
| 2,100 |
The Smart Beaver from ABBYY loves puzzles. One of his favorite puzzles is the magic square. He has recently had an idea to automate the solution of this puzzle. The Beaver decided to offer this challenge to the ABBYY Cup contestants.The magic square is a matrix of size n Γ n. The elements of this matrix are integers. The sum of numbers in each row of the matrix is equal to some number s. The sum of numbers in each column of the matrix is also equal to s. In addition, the sum of the elements on the main diagonal is equal to s and the sum of elements on the secondary diagonal is equal to s. Examples of magic squares are given in the following figure: Magic squares You are given a set of n2 integers ai. It is required to place these numbers into a square matrix of size n Γ n so that they form a magic square. Note that each number must occur in the matrix exactly the same number of times as it occurs in the original set.It is guaranteed that a solution exists!
|
The first input line contains a single integer n. The next line contains n2 integers ai ( - 108 β€ ai β€ 108), separated by single spaces.The input limitations for getting 20 points are: 1 β€ n β€ 3 The input limitations for getting 50 points are: 1 β€ n β€ 4 It is guaranteed that there are no more than 9 distinct numbers among ai. The input limitations for getting 100 points are: 1 β€ n β€ 4
|
The first line of the output should contain a single integer s. In each of the following n lines print n integers, separated by spaces and describing the resulting magic square. In the resulting magic square the sums in the rows, columns and diagonals must be equal to s. If there are multiple solutions, you are allowed to print any of them.
|
Input: 31 2 3 4 5 6 7 8 9 | Output: 152 7 69 5 14 3 8
|
Hard
| 0 | 970 | 387 | 342 | 1 |
||
703 |
D
|
703D
|
D. Mishka and Interesting sum
| 2,100 |
data structures
|
Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, a2, ..., an of n elements!Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to process m queries.Each query is processed in the following way: Two integers l and r (1 β€ l β€ r β€ n) are specified β bounds of query segment. Integers, presented in array segment [l, r] (in sequence of integers al, al + 1, ..., ar) even number of times, are written down. XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are x1, x2, ..., xk, then Mishka wants to know the value , where β operator of exclusive bitwise OR. Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented.
|
The first line of the input contains single integer n (1 β€ n β€ 1 000 000) β the number of elements in the array.The second line of the input contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β array elements.The third line of the input contains single integer m (1 β€ m β€ 1 000 000) β the number of queries.Each of the next m lines describes corresponding query by a pair of integers l and r (1 β€ l β€ r β€ n) β the bounds of query segment.
|
Print m non-negative integers β the answers for the queries in the order they appear in the input.
|
In the second sample:There is no integers in the segment of the first query, presented even number of times in the segment β the answer is 0.In the second query there is only integer 3 is presented even number of times β the answer is 3.In the third query only integer 1 is written down β the answer is 1.In the fourth query all array elements are considered. Only 1 and 2 are presented there even number of times. The answer is .In the fifth query 1 and 3 are written down. The answer is .
|
Input: 33 7 811 3 | Output: 0
|
Hard
| 1 | 1,004 | 438 | 98 | 7 |
1,167 |
B
|
1167B
|
B. Lost Numbers
| 1,400 |
brute force; divide and conquer; interactive; math
|
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.The jury guessed some array \(a\) consisting of \(6\) integers. There are \(6\) special numbers β \(4\), \(8\), \(15\), \(16\), \(23\), \(42\) β and each of these numbers occurs in \(a\) exactly once (so, \(a\) is some permutation of these numbers).You don't know anything about their order, but you are allowed to ask up to \(4\) queries. In each query, you may choose two indices \(i\) and \(j\) (\(1 \le i, j \le 6\), \(i\) and \(j\) are not necessarily distinct), and you will get the value of \(a_i \cdot a_j\) in return.Can you guess the array \(a\)?The array \(a\) is fixed beforehand in each test, the interaction program doesn't try to adapt to your queries.
|
If you want to submit a hack for this problem, your test should contain exactly six space-separated integers \(a_1\), \(a_2\), ..., \(a_6\). Each of \(6\) special numbers should occur exactly once in the test. The test should be ended with a line break character.
|
Input: 16 64 345 672 | Output: ? 1 1 ? 2 2 ? 3 5 ? 4 6 ! 4 8 15 16 23 42
|
Easy
| 4 | 1,079 | 0 | 0 | 11 |
||
616 |
B
|
616B
|
B. Dinner with Emma
| 1,000 |
games; greedy
|
Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places.Munhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to n and the avenues are numbered with integers from 1 to m. The cost of dinner in the restaurant at the intersection of the i-th street and the j-th avenue is cij.Jack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.
|
The first line contains two integers n, m (1 β€ n, m β€ 100) β the number of streets and avenues in Munhattan.Each of the next n lines contains m integers cij (1 β€ cij β€ 109) β the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue.
|
Print the only integer a β the cost of the dinner for Jack and Emma.
|
In the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2.In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1.
|
Input: 3 44 1 3 52 2 2 25 4 5 1 | Output: 2
|
Beginner
| 2 | 904 | 275 | 68 | 6 |
1,902 |
B
|
1902B
|
B. Getting Points
| 1,100 |
binary search; brute force; greedy
|
Monocarp is a student at Berland State University. Due to recent changes in the Berland education system, Monocarp has to study only one subject β programming.The academic term consists of \(n\) days, and in order not to get expelled, Monocarp has to earn at least \(P\) points during those \(n\) days. There are two ways to earn points β completing practical tasks and attending lessons. For each practical task Monocarp fulfills, he earns \(t\) points, and for each lesson he attends, he earns \(l\) points.Practical tasks are unlocked ""each week"" as the term goes on: the first task is unlocked on day \(1\) (and can be completed on any day from \(1\) to \(n\)), the second task is unlocked on day \(8\) (and can be completed on any day from \(8\) to \(n\)), the third task is unlocked on day \(15\), and so on.Every day from \(1\) to \(n\), there is a lesson which can be attended by Monocarp. And every day, Monocarp chooses whether to study or to rest the whole day. When Monocarp decides to study, he attends a lesson and can complete no more than \(2\) tasks, which are already unlocked and not completed yet. If Monocarp rests the whole day, he skips a lesson and ignores tasks.Monocarp wants to have as many days off as possible, i. e. he wants to maximize the number of days he rests. Help him calculate the maximum number of days he can rest!
|
The first line contains a single integer \(tc\) (\(1 \le tc \le 10^4\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains four integers \(n\), \(P\), \(l\) and \(t\) (\(1 \le n, l, t \le 10^9\); \(1 \le P \le 10^{18}\)) β the number of days, the minimum total points Monocarp has to earn, the points for attending one lesson and points for completing one task.It's guaranteed for each test case that it's possible not to be expelled if Monocarp will attend all lessons and will complete all tasks.
|
For each test, print one integer β the maximum number of days Monocarp can rest without being expelled from University.
|
In the first test case, the term lasts for \(1\) day, so Monocarp should attend at day \(1\). Since attending one lesson already gives \(5\) points (\(5 \ge P\)), so it doesn't matter, will Monocarp complete the task or not.In the second test case, Monocarp can, for example, study at days \(8\) and \(9\): at day \(8\) he will attend a lesson for \(10^9\) points and complete two tasks for another \(5 \cdot 10^8 + 5 \cdot 10^8\) points. And at day \(9\) he only attends a lesson for another \(10^9\) points.In the third test case, Monocarp can, for example, study at day \(42\): attending a lesson gives him \(1\) point and solving \(2\) out of \(6\) available tasks gives him another \(2 \cdot 10\) points.In the fourth test case, Monocarp has to attend all lessons and complete all tasks to get \(8 \cdot 10 + 2 \cdot 20 = 120\) points.In the fifth test case, Monocarp can, for example, study at days: \(8\) β one lesson and first and second tasks; \(15\) β one lesson and the third task; \(22\) β one lesson and the fourth task; \(29\) β one lesson and the fifth task; \(36\) β one lesson and the sixth task.
|
Input: 51 5 5 214 3000000000 1000000000 500000000100 20 1 108 120 10 2042 280 13 37 | Output: 0 12 99 0 37
|
Easy
| 3 | 1,356 | 558 | 119 | 19 |
1,916 |
H1
|
1916H1
|
H1. Matrix Rank (Easy Version)
| 2,700 |
brute force; combinatorics; dp; math; matrices
|
This is the easy version of the problem. The only differences between the two versions of this problem are the constraints on \(k\). You can make hacks only if all versions of the problem are solved.You are given integers \(n\), \(p\) and \(k\). \(p\) is guaranteed to be a prime number. For each \(r\) from \(0\) to \(k\), find the number of \(n \times n\) matrices \(A\) of the field\(^\dagger\) of integers modulo \(p\) such that the rank\(^\ddagger\) of \(A\) is exactly \(r\). Since these values are big, you are only required to output them modulo \(998\,244\,353\).\(^\dagger\) https://en.wikipedia.org/wiki/Field_(mathematics)\(^\ddagger\) https://en.wikipedia.org/wiki/Rank_(linear_algebra)
|
The first line of input contains three integers \(n\), \(p\) and \(k\) (\(1 \leq n \leq 10^{18}\), \(2 \leq p < 998\,244\,353\), \(0 \leq k \leq 5000\)).It is guaranteed that \(p\) is a prime number.
|
Output \(k+1\) integers, the answers for each \(r\) from \(0\) to \(k\).
|
Input: 3 2 3 | Output: 1 49 294 168
|
Master
| 5 | 699 | 199 | 72 | 19 |
|
805 |
A
|
805A
|
A. Fake NP
| 1,000 |
greedy; math
|
Tavak and Seyyed are good friends. Seyyed is very funny and he told Tavak to solve the following problem instead of longest-path.You are given l and r. For all integers from l to r, inclusive, we wrote down all of their integer divisors except 1. Find the integer that we wrote down the maximum number of times.Solve the problem to show that it's not a NP problem.
|
The first line contains two integers l and r (2 β€ l β€ r β€ 109).
|
Print single integer, the integer that appears maximum number of times in the divisors. If there are multiple answers, print any of them.
|
Definition of a divisor: https://www.mathsisfun.com/definitions/divisor-of-an-integer-.htmlThe first example: from 19 to 29 these numbers are divisible by 2: {20, 22, 24, 26, 28}.The second example: from 3 to 6 these numbers are divisible by 3: {3, 6}.
|
Input: 19 29 | Output: 2
|
Beginner
| 2 | 364 | 63 | 137 | 8 |
1,316 |
A
|
1316A
|
A. Grade Allocation
| 800 |
implementation
|
\(n\) students are taking an exam. The highest possible score at this exam is \(m\). Let \(a_{i}\) be the score of the \(i\)-th student. You have access to the school database which stores the results of all students.You can change each student's score as long as the following conditions are satisfied: All scores are integers \(0 \leq a_{i} \leq m\) The average score of the class doesn't change. You are student \(1\) and you would like to maximize your own score.Find the highest possible score you can assign to yourself such that all conditions are satisfied.
|
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\) and \(m\) (\(1 \leq n \leq 10^{3}\), \(1 \leq m \leq 10^{5}\)) β the number of students and the highest possible score respectively.The second line of each testcase contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\( 0 \leq a_{i} \leq m\)) β scores of the students.
|
For each testcase, output one integer β the highest possible score you can assign to yourself such that both conditions are satisfied._
|
In the first case, \(a = [1,2,3,4] \), with average of \(2.5\). You can change array \(a\) to \([10,0,0,0]\). Average remains \(2.5\), and all conditions are satisfied.In the second case, \(0 \leq a_{i} \leq 5\). You can change \(a\) to \([5,1,1,3]\). You cannot increase \(a_{1}\) further as it will violate condition \(0\le a_i\le m\).
|
Input: 2 4 10 1 2 3 4 4 5 1 2 3 4 | Output: 10 5
|
Beginner
| 1 | 565 | 487 | 135 | 13 |
1,516 |
D
|
1516D
|
D. Cut
| 2,100 |
binary search; data structures; dp; graphs; number theory; two pointers
|
This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array \(a\) of length \(n\) written on it, and then he does the following: he picks a range \((l, r)\) and cuts the subsegment \(a_l, a_{l + 1}, \ldots, a_r\) out, removing the rest of the array. he then cuts this range into multiple subranges. to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their least common multiple (LCM). Formally, he partitions the elements of \(a_l, a_{l + 1}, \ldots, a_r\) into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for \(q\) independent ranges \((l, r)\), tell Baby Ehab the minimum number of subarrays he needs.
|
The first line contains \(2\) integers \(n\) and \(q\) (\(1 \le n,q \le 10^5\)) β the length of the array \(a\) and the number of queries.The next line contains \(n\) integers \(a_1\), \(a_2\), \(\ldots\), \(a_n\) (\(1 \le a_i \le 10^5\)) β the elements of the array \(a\).Each of the next \(q\) lines contains \(2\) integers \(l\) and \(r\) (\(1 \le l \le r \le n\)) β the endpoints of this query's interval.
|
For each query, print its answer on a new line.
|
The first query asks about the whole array. You can partition it into \([2]\), \([3,10,7]\), and \([5,14]\). The first subrange has product and LCM equal to \(2\). The second has product and LCM equal to \(210\). And the third has product and LCM equal to \(70\). Another possible partitioning is \([2,3]\), \([10,7]\), and \([5,14]\).The second query asks about the range \((2,4)\). Its product is equal to its LCM, so you don't need to partition it further.The last query asks about the range \((3,5)\). You can partition it into \([10,7]\) and \([5]\).
|
Input: 6 3 2 3 10 7 5 14 1 6 2 4 3 5 | Output: 3 1 2
|
Hard
| 6 | 747 | 409 | 47 | 15 |
1,353 |
D
|
1353D
|
D. Constructing the Array
| 1,600 |
constructive algorithms; data structures; sortings
|
You are given an array \(a\) of length \(n\) consisting of zeros. You perform \(n\) actions with this array: during the \(i\)-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be \([l; r]\). If \(r-l+1\) is odd (not divisible by \(2\)) then assign (set) \(a[\frac{l+r}{2}] := i\) (where \(i\) is the number of the current action), otherwise (if \(r-l+1\) is even) assign (set) \(a[\frac{l+r-1}{2}] := i\). Consider the array \(a\) of length \(5\) (initially \(a=[0, 0, 0, 0, 0]\)). Then it changes as follows: Firstly, we choose the segment \([1; 5]\) and assign \(a[3] := 1\), so \(a\) becomes \([0, 0, 1, 0, 0]\); then we choose the segment \([1; 2]\) and assign \(a[1] := 2\), so \(a\) becomes \([2, 0, 1, 0, 0]\); then we choose the segment \([4; 5]\) and assign \(a[4] := 3\), so \(a\) becomes \([2, 0, 1, 3, 0]\); then we choose the segment \([2; 2]\) and assign \(a[2] := 4\), so \(a\) becomes \([2, 4, 1, 3, 0]\); and at last we choose the segment \([5; 5]\) and assign \(a[5] := 5\), so \(a\) becomes \([2, 4, 1, 3, 5]\). Your task is to find the array \(a\) of length \(n\) after performing all \(n\) actions. Note that the answer exists and unique.You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.The only line of the test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\) (\(\sum n \le 2 \cdot 10^5\)).
|
For each test case, print the answer β the array \(a\) of length \(n\) after performing \(n\) actions described in the problem statement. Note that the answer exists and unique.
|
Input: 6 1 2 3 4 5 6 | Output: 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
|
Medium
| 3 | 1,368 | 368 | 177 | 13 |
|
1,906 |
A
|
1906A
|
A. Easy As ABC
| 1,000 |
brute force
|
You are playing a word puzzle. The puzzle starts with a \(3\) by \(3\) grid, where each cell contains either the letter A, B, or C.The goal of this puzzle is to find the lexicographically smallest possible word of length \(3\). The word can be formed by choosing three different cells where the cell containing the first letter is adjacent to the cell containing the second letter, and the cell containing the second letter is adjacent to the cell containing the third letter.Two cells are adjacent to each other if they share a border or a corner, as shown in the following illustration. Formally, if \((r, c)\) denotes the cell in the \(r\)-th row and \(c\)-th column, then cell \((r, c)\) is adjacent to cell \((r, c + 1)\), \((r - 1, c + 1)\), \((r - 1, c)\), \((r - 1, c - 1)\), \((r, c - 1)\), \((r + 1, c - 1)\), \((r + 1, c)\), and \((r + 1, c + 1)\). Determine the lexicographically smallest possible word of length \(3\) that you can find within the grid.A string \(s\) of length \(n\) is lexicographically smaller than string \(t\) of the same length if there exists an integer \(1 \leq i \leq n\) such that \(s_j = t_j\) for all \(1 \leq j < i\), and \(s_i < t_i\) in alphabetical order. The following illustration shows some examples on some grids and their the lexicographically smallest possible word of length \(3\) that you can find within the grids.
|
Input consists of three lines, each containing three letters, representing the puzzle grid. Each letter in the grid can only be either A, B, or C.
|
Output the lexicographically smallest possible word of length \(3\) that you can find within the grid.
|
Input: BCB CAC BCB | Output: ABC
|
Beginner
| 1 | 1,367 | 146 | 102 | 19 |
|
2,048 |
G
|
2048G
|
G. Kevin and Matrices
| 2,800 |
brute force; combinatorics; dp; math
|
Kevin has been transported to Sacred Heart Hospital, which contains all the \( n \times m \) matrices with integer values in the range \( [1,v] \).Now, Kevin wants to befriend some matrices, but he is willing to befriend a matrix \( a \) if and only if the following condition is satisfied:$$$\( \min_{1\le i\le n}\left(\max_{1\le j\le m}a_{i,j}\right)\le\max_{1\le j\le m}\left(\min_{1\le i\le n}a_{i,j}\right). \)\(Please count how many matrices in Sacred Heart Hospital can be friends with Kevin.Since Kevin is very friendly, there could be many matrices that meet this condition. Therefore, you only need to output the result modulo \)998\,244\,353$$$.
|
Each test contains multiple test cases. The first line contains the number of test cases \( t \) (\( 1 \le t \le 8\cdot 10^3 \)).The only line of each test case contains three integers \(n\), \(m\), \(v\) (\( 1 \le n, v, n \cdot v \leq 10^6\), \(1 \le m \le 10^9 \)).It is guaranteed that the sum of \( n \cdot v \) over all test cases doesn't exceed \( 10^6 \).
|
For each test case, output one integer β the number of matrices that can be friends with Kevin modulo \(998\,244\,353\).
|
In the first test case, besides the matrices \( a=\begin{bmatrix}1&2\\2&1\end{bmatrix} \) and \( a=\begin{bmatrix}2&1\\1&2\end{bmatrix} \), which do not satisfy the condition, the remaining \( 2^{2 \cdot 2} - 2 = 14 \) matrices can all be friends with Kevin.
|
Input: 32 2 22 3 411 45 14 | Output: 14 2824 883799966
|
Master
| 4 | 656 | 362 | 120 | 20 |
2,057 |
C
|
2057C
|
C. Trip to the Olympiad
| 1,500 |
bitmasks; constructive algorithms; greedy; math
|
In the upcoming year, there will be many team olympiads, so the teachers of ""T-generation"" need to assemble a team of three pupils to participate in them. Any three pupils will show a worthy result in any team olympiad. But winning the olympiad is only half the battle; first, you need to get there...Each pupil has an independence level, expressed as an integer. In ""T-generation"", there is exactly one student with each independence levels from \(l\) to \(r\), inclusive. For a team of three pupils with independence levels \(a\), \(b\), and \(c\), the value of their team independence is equal to \((a \oplus b) + (b \oplus c) + (a \oplus c)\), where \(\oplus\) denotes the bitwise XOR operation. Your task is to choose any trio of students with the maximum possible team independence.
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case set contains two integers \(l\) and \(r\) (\(0 \le l, r < 2^{30}\), \(r - l > 1\)) β the minimum and maximum independence levels of the students.
|
For each test case set, output three pairwise distinct integers \(a, b\), and \(c\), such that \(l \le a, b, c \le r\) and the value of the expression \((a \oplus b) + (b \oplus c) + (a \oplus c)\) is maximized. If there are multiple triples with the maximum value, any of them can be output.
|
In the first test case, the only suitable triplet of numbers (\(a, b, c\)) (up to permutation) is (\(0, 1, 2\)).In the second test case, one of the suitable triplets is (\(8, 7, 1\)), where \((8 \oplus 7) + (7 \oplus 1) + (8 \oplus 1) = 15 + 6 + 9 = 30\). It can be shown that \(30\) is the maximum possible value of \((a \oplus b) + (b \oplus c) + (a \oplus c)\) for \(0 \le a, b, c \le 8\).
|
Input: 80 20 81 36 22128 13769 98115 1270 1073741823 | Output: 1 2 0 8 7 1 2 1 3 7 16 11 134 132 137 98 85 76 123 121 118 965321865 375544086 12551794
|
Medium
| 4 | 792 | 358 | 292 | 20 |
1,002 |
E2
|
1002E2
|
E2. Another array reconstruction algorithm
| 1,900 |
*special
|
You are given a quantum oracle - an operation on N + 1 qubits which implements a function . You are guaranteed that the function f implemented by the oracle can be represented in the following form (oracle from problem D2):Here (a vector of N integers, each of which can be 0 or 1), and is a vector of N 1s.Your task is to reconstruct the array which could produce the given oracle. Your code is allowed to call the given oracle only once.You have to implement an operation which takes the following inputs: an integer N - the number of qubits in the oracle input (1 β€ N β€ 8), an oracle Uf, implemented as an operation with signature ((Qubit[], Qubit) => ()), i.e., an operation which takes as input an array of qubits and an output qubit and has no output.The return of your operation is an array of integers of length N, each of them 0 or 1.Note that in this problem we're comparing the oracle generated by your return to the oracle Uf, instead of comparing your return to the (hidden) value of used to generate Uf. This means that any kind of incorrect return results in ""Runtime Error"" verdict, as well as actual runtime errors like releasing qubits in non-zero state.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (N : Int, Uf : ((Qubit[], Qubit) => ())) : Int[] { body { // your code here } }}
|
Hard
| 1 | 1,401 | 0 | 0 | 10 |
||||
134 |
A
|
134A
|
A. Average Numbers
| 1,200 |
brute force; implementation
|
You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).
|
The first line contains the integer n (2 β€ n β€ 2Β·105). The second line contains elements of the sequence a1, a2, ..., an (1 β€ ai β€ 1000). All the elements are positive integers.
|
Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n.If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.
|
Input: 51 2 3 4 5 | Output: 13
|
Easy
| 2 | 202 | 177 | 330 | 1 |
|
609 |
D
|
609D
|
D. Gadgets for dollars and pounds
| 2,000 |
binary search; greedy; two pointers
|
Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing.Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds.Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days.Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input.
|
First line contains four integers n, m, k, s (1 β€ n β€ 2Β·105, 1 β€ k β€ m β€ 2Β·105, 1 β€ s β€ 109) β number of days, total number and required number of gadgets, number of burles Nura has.Second line contains n integers ai (1 β€ ai β€ 106) β the cost of one dollar in burles on i-th day.Third line contains n integers bi (1 β€ bi β€ 106) β the cost of one pound in burles on i-th day.Each of the next m lines contains two integers ti, ci (1 β€ ti β€ 2, 1 β€ ci β€ 106) β type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds.
|
If Nura can't buy k gadgets print the only line with the number -1.Otherwise the first line should contain integer d β the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n.In case there are multiple possible solutions, print any of them.
|
Input: 5 4 2 21 2 3 2 13 2 1 2 31 12 11 22 2 | Output: 31 12 3
|
Hard
| 3 | 901 | 618 | 498 | 6 |
|
1,539 |
D
|
1539D
|
D. PriceFixed
| 1,600 |
binary search; greedy; implementation; sortings; two pointers
|
Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store β ""PriceFixed"". Here are some rules of that store: The store has an infinite number of items of every product. All products have the same price: \(2\) rubles per item. For every product \(i\) there is a discount for experienced buyers: if you buy \(b_i\) items of products (of any type, not necessarily type \(i\)), then for all future purchases of the \(i\)-th product there is a \(50\%\) discount (so you can buy an item of the \(i\)-th product for \(1\) ruble!). Lena needs to buy \(n\) products: she must purchase at least \(a_i\) items of the \(i\)-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 100\,000\)) β the number of products.Each of next \(n\) lines contains a product description. Each description consists of two integers \(a_i\) and \(b_i\) (\(1 \leq a_i \leq 10^{14}\), \(1 \leq b_i \leq 10^{14}\)) β the required number of the \(i\)-th product and how many products you need to buy to get the discount on the \(i\)-th product. The sum of all \(a_i\) does not exceed \(10^{14}\).
|
Output the minimum sum that Lena needs to make all purchases.
|
In the first example, Lena can purchase the products in the following way: one item of product \(3\) for \(2\) rubles, one item of product \(1\) for \(2\) rubles, one item of product \(1\) for \(2\) rubles, one item of product \(2\) for \(1\) ruble (she can use the discount because \(3\) items are already purchased), one item of product \(1\) for \(1\) ruble (she can use the discount because \(4\) items are already purchased). In total, she spends \(8\) rubles. It can be proved that it is impossible to spend less.In the second example Lena can purchase the products in the following way: one item of product \(1\) for \(2\) rubles, two items of product \(2\) for \(2\) rubles for each, one item of product \(5\) for \(2\) rubles, one item of product \(3\) for \(1\) ruble, two items of product \(4\) for \(1\) ruble for each, one item of product \(1\) for \(1\) ruble. In total, she spends \(12\) rubles.
|
Input: 3 3 4 1 3 1 5 | Output: 8
|
Medium
| 5 | 904 | 459 | 61 | 15 |
2,103 |
D
|
2103D
|
D. Local Construction
| 2,000 |
constructive algorithms; dfs and similar; implementation; two pointers
|
An element \(b_i\) (\(1\le i\le m\)) in an array \(b_1, b_2, \ldots, b_m\) is a local minimum if at least one of the following holds: \(2\le i\le m - 1\) and \(b_i < b_{i - 1}\) and \(b_i < b_{i + 1}\), or \(i = 1\) and \(b_1 < b_2\), or \(i = m\) and \(b_m < b_{m - 1}\). Similarly, an element \(b_i\) (\(1\le i\le m\)) in an array \(b_1, b_2, \ldots, b_m\) is a local maximum if at least one of the following holds: \(2\le i\le m - 1\) and \(b_i > b_{i - 1}\) and \(b_i > b_{i + 1}\), or \(i = 1\) and \(b_1 > b_2\), or \(i = m\) and \(b_m > b_{m - 1}\). Note that local minima and maxima are not defined for arrays with only one element.There is a hidden permutation\(^{\text{β}}\) \(p\) of length \(n\). The following two operations are applied to permutation \(p\) alternately, starting from operation 1, until there is only one element left in \(p\): Operation 1 β remove all elements of \(p\) which are not local minima. Operation 2 β remove all elements of \(p\) which are not local maxima. More specifically, operation 1 is applied during every odd iteration, and operation 2 is applied during every even iteration, until there is only one element left in \(p\).For each index \(i\) (\(1\le i\le n\)), let \(a_i\) be the iteration number that element \(p_i\) is removed, or \(-1\) if it was never removed.It can be proven that there will be only one element left in \(p\) after at most \(\lceil \log_2 n\rceil\) iterations (in other words, \(a_i \le \lceil \log_2 n\rceil\)).You are given the array \(a_1, a_2, \ldots, a_n\). Your task is to construct any permutation \(p\) of \(n\) elements that satisfies array \(a\).\(^{\text{β}}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of elements in permutation \(p\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le \lceil\log_2 n\rceil\) or \(a_i = -1\)) β the iteration number that element \(p_i\) is removed.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\). It is guaranteed that there exists at least one permutation \(p\) that satisfies array \(a\).
|
For each test case, output \(n\) integers representing the elements of the permutation satisfying array \(a\).If there are multiple solutions, you may output any of them.
|
In the first test case, operations will be applied to permutation \([3, 2, 1]\) as follows: The only local minimum in \([3, 2, 1]\) is \(1\). Hence, elements \(3\) and \(2\) are removed. There is only one remaining element; hence the process terminates. This satisfies array \(a = [1, 1, -1]\) as both \(p_1\) and \(p_2\) were removed on iteration number \(1\), while \(p_3\) was not removed.In the second test case, operations will be applied to permutation \(p = [4, 3, 5, 1, 2]\) as follows: The local minima in \([4, 3, 5, 1, 2]\) are \(3\) and \(1\). Hence, elements \(4\), \(5\), and \(2\) are removed. The only local maximum in \([3, 1]\) is \(3\). Hence, element \(1\) is removed. There is only one remaining element; hence the process terminates. This satisfies array \(a = [1, -1, 1, 2, 1]\) as elements \(p_1 = 4\), \(p_3 = 5\), and \(p_5 = 2\) were removed on iteration \(1\), element \(p_4 = 1\) was removed on iteration \(2\), and element \(p_2 = 3\) was not removed.In the third test case, operations will be applied on permutation \([6, 7, 2, 4, 3, 8, 5, 1]\) as follows: The local minima in \([6, 7, 2, 4, 3, 8, 5, 1]\) are \(6\), \(2\), \(3\), and \(1\). Hence, elements \(7\), \(4\), \(8\), and \(5\) are removed. The local maxima in \([6, 2, 3, 1]\) are \(6\) and \(3\). Hence, elements \(2\) and \(1\) are removed. The only local minimum in \([6, 3]\) is \(3\). Hence, element \(6\) is removed. There is only one remaining element; hence the process terminates. In the fourth test case, one permutation satisfying the constraints is [\(6\), \(5\), \(2\), \(1\), \(3\), \(4\), \(7\)]. \(1\) is the only local minimum, so only it will stay after the first iteration. Note that there are other valid permutations; for example, [\(6\), \(4\), \(3\), \(1\), \(2\), \(5\), \(7\)] would also be considered correct.
|
Input: 731 1 -151 -1 1 2 183 1 2 1 -1 1 1 271 1 1 -1 1 1 151 1 1 1 -15-1 1 1 1 15-1 1 2 1 2 | Output: 3 2 1 4 3 5 1 2 6 7 2 4 3 8 5 1 6 5 2 1 3 4 7 5 4 3 2 1 1 2 3 4 5 4 5 2 3 1
|
Hard
| 4 | 1,967 | 686 | 170 | 21 |
777 |
D
|
777D
|
D. Cloud of Hashtags
| 1,800 |
binary search; greedy; implementation; strings
|
Vasya is an administrator of a public page of organization ""Mouse and keyboard"" and his everyday duty is to publish news from the world of competitive programming. For each news he also creates a list of hashtags to make searching for a particular topic more comfortable. For the purpose of this problem we define hashtag as a string consisting of lowercase English letters and exactly one symbol '#' located at the beginning of the string. The length of the hashtag is defined as the number of symbols in it without the symbol '#'.The head administrator of the page told Vasya that hashtags should go in lexicographical order (take a look at the notes section for the definition).Vasya is lazy so he doesn't want to actually change the order of hashtags in already published news. Instead, he decided to delete some suffixes (consecutive characters at the end of the string) of some of the hashtags. He is allowed to delete any number of characters, even the whole string except for the symbol '#'. Vasya wants to pick such a way to delete suffixes that the total number of deleted symbols is minimum possible. If there are several optimal solutions, he is fine with any of them.
|
The first line of the input contains a single integer n (1 β€ n β€ 500 000) β the number of hashtags being edited now.Each of the next n lines contains exactly one hashtag of positive length.It is guaranteed that the total length of all hashtags (i.e. the total length of the string except for characters '#') won't exceed 500 000.
|
Print the resulting hashtags in any of the optimal solutions.
|
Word a1, a2, ..., am of length m is lexicographically not greater than word b1, b2, ..., bk of length k, if one of two conditions hold: at first position i, such that ai β bi, the character ai goes earlier in the alphabet than character bi, i.e. a has smaller character than b in the first position where they differ; if there is no such position i and m β€ k, i.e. the first word is a prefix of the second or two words are equal. The sequence of words is said to be sorted in lexicographical order if each word (except the last one) is lexicographically not greater than the next word.For the words consisting of lowercase English letters the lexicographical order coincides with the alphabet word order in the dictionary.According to the above definition, if a hashtag consisting of one character '#' it is lexicographically not greater than any other valid hashtag. That's why in the third sample we can't keep first two hashtags unchanged and shorten the other two.
|
Input: 3#book#bigtown#big | Output: #b#big#big
|
Medium
| 4 | 1,182 | 329 | 61 | 7 |
115 |
D
|
115D
|
D. Unambiguous Arithmetic Expression
| 2,600 |
dp; expression parsing
|
Let's define an unambiguous arithmetic expression (UAE) as follows. All non-negative integers are UAE's. Integers may have leading zeroes (for example, 0000 and 0010 are considered valid integers). If X and Y are two UAE's, then ""(X) + (Y)"", ""(X) - (Y)"", ""(X) * (Y)"", and ""(X) / (Y)"" (all without the double quotes) are UAE's. If X is an UAE, then "" - (X)"" and "" + (X)"" (both without the double quotes) are UAE's.You are given a string consisting only of digits (""0"" - ""9"") and characters ""-"", ""+"", ""*"", and ""/"". Your task is to compute the number of different possible unambiguous arithmetic expressions such that if all brackets (characters ""("" and "")"") of that unambiguous arithmetic expression are removed, it becomes the input string. Since the answer may be very large, print it modulo 1000003 (106 + 3).
|
The first line is a non-empty string consisting of digits ('0'-'9') and characters '-', '+', '*', and/or '/'. Its length will not exceed 2000. The line doesn't contain any spaces.
|
Print a single integer representing the number of different unambiguous arithmetic expressions modulo 1000003 (106 + 3) such that if all its brackets are removed, it becomes equal to the input string (character-by-character).
|
For the first example, the two possible unambiguous arithmetic expressions are:((1) + (2)) * (3)(1) + ((2) * (3))For the second example, the three possible unambiguous arithmetic expressions are:(03) + (( - (30)) + (40))(03) + ( - ((30) + (40)))((03) + ( - (30))) + (40)
|
Input: 1+2*3 | Output: 2
|
Expert
| 2 | 838 | 179 | 225 | 1 |
130 |
B
|
130B
|
B. Gnikool Ssalg
| 1,400 |
*special; implementation; strings
|
You are given a string. Reverse its characters.
|
The only line of input contains a string between 1 and 100 characters long. Each character of the string has ASCII-code between 33 (exclamation mark) and 126 (tilde), inclusive.
|
Output the characters of this string in reverse order.
|
Input: secrofedoc | Output: codeforces
|
Easy
| 3 | 47 | 177 | 54 | 1 |
|
1,873 |
F
|
1873F
|
F. Money Trees
| 1,300 |
binary search; greedy; math; two pointers
|
Luca is in front of a row of \(n\) trees. The \(i\)-th tree has \(a_i\) fruit and height \(h_i\).He wants to choose a contiguous subarray of the array \([h_l, h_{l+1}, \dots, h_r]\) such that for each \(i\) (\(l \leq i < r\)), \(h_i\) is divisible\(^{\dagger}\) by \(h_{i+1}\). He will collect all the fruit from each of the trees in the subarray (that is, he will collect \(a_l + a_{l+1} + \dots + a_r\) fruits). However, if he collects more than \(k\) fruits in total, he will get caught. What is the maximum length of a subarray Luca can choose so he doesn't get caught?\(^{\dagger}\) \(x\) is divisible by \(y\) if the ratio \(\frac{x}{y}\) is an integer.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first of each test case line contains two space-separated integers \(n\) and \(k\) (\(1 \leq n \leq 2 \cdot 10^5\); \(1 \leq k \leq 10^9\)) β the number of trees and the maximum amount of fruits Luca can collect without getting caught.The second line of each test case contains \(n\) space-separated integers \(a_i\) (\(1 \leq a_i \leq 10^4\)) β the number of fruits in the \(i\)-th tree.The third line of each test case contains \(n\) space-separated integers \(h_i\) (\(1 \leq h_i \leq 10^9\)) β the height of the \(i\)-th tree.The sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case output a single integer, the length of the maximum length contiguous subarray satisfying the conditions, or \(0\) if there is no such subarray.
|
In the first test case, Luca can select the subarray with \(l=1\) and \(r=3\).In the second test case, Luca can select the subarray with \(l=3\) and \(r=4\).In the third test case, Luca can select the subarray with \(l=2\) and \(r=2\).
|
Input: 55 123 2 4 1 84 4 2 4 14 85 4 1 26 2 3 13 127 9 102 2 41 101117 102 6 3 1 5 10 672 24 24 12 4 4 2 | Output: 3 2 1 0 3
|
Easy
| 4 | 659 | 703 | 162 | 18 |
719 |
B
|
719B
|
B. Anatoly and Cockroaches
| 1,400 |
greedy
|
Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are n cockroaches living in Anatoly's room.Anatoly just made all his cockroaches to form a single line. As he is a perfectionist, he would like the colors of cockroaches in the line to alternate. He has a can of black paint and a can of red paint. In one turn he can either swap any two cockroaches, or take any single cockroach and change it's color.Help Anatoly find out the minimum number of turns he needs to make the colors of cockroaches in the line alternate.
|
The first line of the input contains a single integer n (1 β€ n β€ 100 000) β the number of cockroaches.The second line contains a string of length n, consisting of characters 'b' and 'r' that denote black cockroach and red cockroach respectively.
|
Print one integer β the minimum number of moves Anatoly has to perform in order to make the colors of cockroaches in the line to alternate.
|
In the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1 turn to do this.In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires 2 turns.In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is 0.
|
Input: 5rbbrr | Output: 1
|
Easy
| 1 | 658 | 245 | 139 | 7 |
1,548 |
C
|
1548C
|
C. The Three Little Pigs
| 2,500 |
combinatorics; dp; fft; math
|
Three little pigs from all over the world are meeting for a convention! Every minute, a triple of 3 new pigs arrives on the convention floor. After the \(n\)-th minute, the convention ends.The big bad wolf has learned about this convention, and he has an attack plan. At some minute in the convention, he will arrive and eat exactly \(x\) pigs. Then he will get away.The wolf wants Gregor to help him figure out the number of possible attack plans that involve eating exactly \(x\) pigs for various values of \(x\) (\(1 \le x \le 3n\)). Two attack plans are considered different, if they occur at different times or if the sets of little pigs to eat are different.Note that all queries are independent, that is, the wolf does not eat the little pigs, he only makes plans!
|
The first line of input contains two integers \(n\) and \(q\) (\(1 \le n \le 10^6\), \(1 \le q \le 2\cdot 10^5\)), the number of minutes the convention lasts and the number of queries the wolf asks.Each of the next \(q\) lines contains a single integer \(x_i\) (\(1 \le x_i \le 3n\)), the number of pigs the wolf will eat in the \(i\)-th query.
|
You should print \(q\) lines, with line \(i\) representing the number of attack plans if the wolf wants to eat \(x_i\) pigs. Since each query answer can be large, output each answer modulo \(10^9+7\).
|
In the example test, \(n=2\). Thus, there are \(3\) pigs at minute \(1\), and \(6\) pigs at minute \(2\). There are three queries: \(x=1\), \(x=5\), and \(x=6\).If the wolf wants to eat \(1\) pig, he can do so in \(3+6=9\) possible attack plans, depending on whether he arrives at minute \(1\) or \(2\).If the wolf wants to eat \(5\) pigs, the wolf cannot arrive at minute \(1\), since there aren't enough pigs at that time. Therefore, the wolf has to arrive at minute \(2\), and there are \(6\) possible attack plans.If the wolf wants to eat \(6\) pigs, his only plan is to arrive at the end of the convention and devour everybody.Remember to output your answers modulo \(10^9+7\)!
|
Input: 2 3 1 5 6 | Output: 9 6 1
|
Expert
| 4 | 771 | 344 | 200 | 15 |
1,276 |
B
|
1276B
|
B. Two Fairs
| 1,900 |
combinatorics; dfs and similar; dsu; graphs
|
There are \(n\) cities in Berland and some pairs of them are connected by two-way roads. It is guaranteed that you can pass from any city to any other, moving along the roads. Cities are numerated from \(1\) to \(n\).Two fairs are currently taking place in Berland β they are held in two different cities \(a\) and \(b\) (\(1 \le a, b \le n\); \(a \ne b\)).Find the number of pairs of cities \(x\) and \(y\) (\(x \ne a, x \ne b, y \ne a, y \ne b\)) such that if you go from \(x\) to \(y\) you will have to go through both fairs (the order of visits doesn't matter). Formally, you need to find the number of pairs of cities \(x,y\) such that any path from \(x\) to \(y\) goes through \(a\) and \(b\) (in any order).Print the required number of pairs. The order of two cities in a pair does not matter, that is, the pairs \((x,y)\) and \((y,x)\) must be taken into account only once.
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 4\cdot10^4\)) β the number of test cases in the input. Next, \(t\) test cases are specified.The first line of each test case contains four integers \(n\), \(m\), \(a\) and \(b\) (\(4 \le n \le 2\cdot10^5\), \(n - 1 \le m \le 5\cdot10^5\), \(1 \le a,b \le n\), \(a \ne b\)) β numbers of cities and roads in Berland and numbers of two cities where fairs are held, respectively.The following \(m\) lines contain descriptions of roads between cities. Each of road description contains a pair of integers \(u_i, v_i\) (\(1 \le u_i, v_i \le n\), \(u_i \ne v_i\)) β numbers of cities connected by the road.Each road is bi-directional and connects two different cities. It is guaranteed that from any city you can pass to any other by roads. There can be more than one road between a pair of cities.The sum of the values of \(n\) for all sets of input data in the test does not exceed \(2\cdot10^5\). The sum of the values of \(m\) for all sets of input data in the test does not exceed \(5\cdot10^5\).
|
Print \(t\) integers β the answers to the given test cases in the order they are written in the input.
|
Input: 3 7 7 3 5 1 2 2 3 3 4 4 5 5 6 6 7 7 5 4 5 2 3 1 2 2 3 3 4 4 1 4 2 4 3 2 1 1 2 2 3 4 1 | Output: 4 0 1
|
Hard
| 4 | 881 | 1,063 | 102 | 12 |
|
489 |
B
|
489B
|
B. BerSU Ball
| 1,200 |
dfs and similar; dp; graph matchings; greedy; sortings; two pointers
|
The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and m girls are already busy rehearsing waltz, minuet, polonaise and quadrille moves.We know that several boy&girl pairs are going to be invited to the ball. However, the partners' dancing skill in each pair must differ by at most one.For each boy, we know his dancing skills. Similarly, for each girl we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed from n boys and m girls.
|
The first line contains an integer n (1 β€ n β€ 100) β the number of boys. The second line contains sequence a1, a2, ..., an (1 β€ ai β€ 100), where ai is the i-th boy's dancing skill.Similarly, the third line contains an integer m (1 β€ m β€ 100) β the number of girls. The fourth line contains sequence b1, b2, ..., bm (1 β€ bj β€ 100), where bj is the j-th girl's dancing skill.
|
Print a single number β the required maximum possible number of pairs.
|
Input: 41 4 6 255 1 5 7 9 | Output: 3
|
Easy
| 6 | 551 | 373 | 70 | 4 |
|
486 |
A
|
486A
|
A. Calculating Function
| 800 |
implementation; math
|
For a positive integer n let's define a function f:f(n) = - 1 + 2 - 3 + .. + ( - 1)nn Your task is to calculate f(n) for a given integer n.
|
The single line contains the positive integer n (1 β€ n β€ 1015).
|
Print f(n) in a single line.
|
f(4) = - 1 + 2 - 3 + 4 = 2f(5) = - 1 + 2 - 3 + 4 - 5 = - 3
|
Input: 4 | Output: 2
|
Beginner
| 2 | 139 | 63 | 28 | 4 |
1,611 |
E2
|
1611E2
|
E2. Escape The Maze (hard version)
| 1,900 |
dfs and similar; dp; greedy; shortest paths; trees
|
The only difference with E1 is the question of the problem.Vlad built a maze out of \(n\) rooms and \(n-1\) bidirectional corridors. From any room \(u\) any other room \(v\) can be reached through a sequence of corridors. Thus, the room system forms an undirected tree.Vlad invited \(k\) friends to play a game with them.Vlad starts the game in the room \(1\) and wins if he reaches a room other than \(1\), into which exactly one corridor leads. Friends are placed in the maze: the friend with number \(i\) is in the room \(x_i\), and no two friends are in the same room (that is, \(x_i \neq x_j\) for all \(i \neq j\)). Friends win if one of them meets Vlad in any room or corridor before he wins.For one unit of time, each participant of the game can go through one corridor. All participants move at the same time. Participants may not move. Each room can fit all participants at the same time.Friends know the plan of a maze and intend to win. They don't want to waste too much energy. They ask you to determine if they can win and if they can, what minimum number of friends must remain in the maze so that they can always catch Vlad.In other words, you need to determine the size of the minimum (by the number of elements) subset of friends who can catch Vlad or say that such a subset does not exist.
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the input. The input contains an empty string before each test case.The first line of the test case contains two numbers \(n\) and \(k\) (\(1 \le k < n \le 2\cdot 10^5\)) β the number of rooms and friends, respectively.The next line of the test case contains \(k\) integers \(x_1, x_2, \dots, x_k\) (\(2 \le x_i \le n\)) β numbers of rooms with friends. All \(x_i\) are different.The next \(n-1\) lines contain descriptions of the corridors, two numbers per line \(v_j\) and \(u_j\) (\(1 \le u_j, v_j \le n\)) β numbers of rooms that connect the \(j\) corridor. All corridors are bidirectional. From any room, you can go to any other by moving along the corridors.It is guaranteed that the sum of the values \(n\) over all test cases in the test is not greater than \(2\cdot10^5\).
|
Print \(t\) lines, each line containing the answer to the corresponding test case. The answer to a test case should be \(-1\) if Vlad wins anyway and a minimal number of friends otherwise.
|
In the first set of inputs, even if all the friends stay in the maze, Vlad can still win. Therefore, the answer is ""-1"".In the second set of inputs it is enough to leave friends from rooms \(6\) and \(7\). Then Vlad will not be able to win. The answer is ""2"".In the third and fourth sets of inputs Vlad cannot win only if all his friends stay in the maze. Therefore the answers are ""1"" and ""2"".
|
Input: 4 8 2 5 3 4 7 2 5 1 6 3 6 7 2 1 7 6 8 8 4 6 5 7 3 4 7 2 5 1 6 3 6 7 2 1 7 6 8 3 1 2 1 2 2 3 3 2 2 3 3 1 1 2 | Output: -1 2 1 2
|
Hard
| 5 | 1,308 | 888 | 188 | 16 |
920 |
E
|
920E
|
E. Connected Components?
| 2,100 |
data structures; dfs and similar; dsu; graphs
|
You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices is not listed in the input, then there is an edge between these vertices.You have to find the number of connected components in the graph and the size of each component. A connected component is a set of vertices X such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to X violates this rule.
|
The first line contains two integers n and m (1 β€ n β€ 200000, ).Then m lines follow, each containing a pair of integers x and y (1 β€ x, y β€ n, x β y) denoting that there is no edge between x and y. Each pair is listed at most once; (x, y) and (y, x) are considered the same (so they are never listed in the same test). If some pair of vertices is not listed in the input, then there exists an edge between those vertices.
|
Firstly print k β the number of connected components in this graph.Then print k integers β the sizes of components. You should output these integers in non-descending order.
|
Input: 5 51 23 43 24 22 5 | Output: 21 4
|
Hard
| 4 | 622 | 421 | 173 | 9 |
|
553 |
B
|
553B
|
B. Kyoya and Permutation
| 1,900 |
binary search; combinatorics; constructive algorithms; greedy; implementation; math
|
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place. Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
|
The first line will contain two integers n, k (1 β€ n β€ 50, 1 β€ k β€ min{1018, l} where l is the length of the Kyoya's list).
|
Print n space-separated integers, representing the permutation that is the answer for the question.
|
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
|
Input: 4 3 | Output: 1 3 2 4
|
Hard
| 6 | 1,778 | 123 | 99 | 5 |
1,548 |
B
|
1548B
|
B. Integers Have Friends
| 1,800 |
binary search; data structures; divide and conquer; math; number theory; two pointers
|
British mathematician John Littlewood once said about Indian mathematician Srinivasa Ramanujan that ""every positive integer was one of his personal friends.""It turns out that positive integers can also be friends with each other! You are given an array \(a\) of distinct positive integers. Define a subarray \(a_i, a_{i+1}, \ldots, a_j\) to be a friend group if and only if there exists an integer \(m \ge 2\) such that \(a_i \bmod m = a_{i+1} \bmod m = \ldots = a_j \bmod m\), where \(x \bmod y\) denotes the remainder when \(x\) is divided by \(y\).Your friend Gregor wants to know the size of the largest friend group in \(a\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot 10^4\)). Each test case begins with a line containing the integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)), the size of the array \(a\).The next line contains \(n\) positive integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le {10}^{18}\)), representing the contents of the array \(a\). It is guaranteed that all the numbers in \(a\) are distinct.It is guaranteed that the sum of \(n\) over all test cases is less than \(2\cdot 10^5\).
|
Your output should consist of \(t\) lines. Each line should consist of a single integer, the size of the largest friend group in \(a\).
|
In the first test case, the array is \([1,5,2,4,6]\). The largest friend group is \([2,4,6]\), since all those numbers are congruent to \(0\) modulo \(2\), so \(m=2\).In the second test case, the array is \([8,2,5,10]\). The largest friend group is \([8,2,5]\), since all those numbers are congruent to \(2\) modulo \(3\), so \(m=3\).In the third case, the largest friend group is \([1000,2000]\). There are clearly many possible values of \(m\) that work.
|
Input: 4 5 1 5 2 4 6 4 8 2 5 10 2 1000 2000 8 465 55 3 54 234 12 45 78 | Output: 3 3 2 6
|
Medium
| 6 | 632 | 547 | 135 | 15 |
863 |
C
|
863C
|
C. 1-2-3
| 1,800 |
graphs; implementation
|
Ilya is working for the company that constructs robots. Ilya writes programs for entertainment robots, and his current project is ""Bob"", a new-generation game robot. Ilya's boss wants to know his progress so far. Especially he is interested if Bob is better at playing different games than the previous model, ""Alice"". So now Ilya wants to compare his robots' performance in a simple game called ""1-2-3"". This game is similar to the ""Rock-Paper-Scissors"" game: both robots secretly choose a number from the set {1, 2, 3} and say it at the same moment. If both robots choose the same number, then it's a draw and noone gets any points. But if chosen numbers are different, then one of the robots gets a point: 3 beats 2, 2 beats 1 and 1 beats 3. Both robots' programs make them choose their numbers in such a way that their choice in (i + 1)-th game depends only on the numbers chosen by them in i-th game. Ilya knows that the robots will play k games, Alice will choose number a in the first game, and Bob will choose b in the first game. He also knows both robots' programs and can tell what each robot will choose depending on their choices in previous game. Ilya doesn't want to wait until robots play all k games, so he asks you to predict the number of points they will have after the final game.
|
The first line contains three numbers k, a, b (1 β€ k β€ 1018, 1 β€ a, b β€ 3). Then 3 lines follow, i-th of them containing 3 numbers Ai, 1, Ai, 2, Ai, 3, where Ai, j represents Alice's choice in the game if Alice chose i in previous game and Bob chose j (1 β€ Ai, j β€ 3). Then 3 lines follow, i-th of them containing 3 numbers Bi, 1, Bi, 2, Bi, 3, where Bi, j represents Bob's choice in the game if Alice chose i in previous game and Bob chose j (1 β€ Bi, j β€ 3).
|
Print two numbers. First of them has to be equal to the number of points Alice will have, and second of them must be Bob's score after k games.
|
In the second example game goes like this:The fourth and the seventh game are won by Bob, the first game is draw and the rest are won by Alice.
|
Input: 10 2 11 1 11 1 11 1 12 2 22 2 22 2 2 | Output: 1 9
|
Medium
| 2 | 1,309 | 459 | 143 | 8 |
1,905 |
D
|
1905D
|
D. Cyclic MEX
| 2,000 |
data structures; implementation; math; two pointers
|
For an array \(a\), define its cost as \(\sum_{i=1}^{n} \operatorname{mex} ^\dagger ([a_1,a_2,\ldots,a_i])\).You are given a permutation\(^\ddagger\) \(p\) of the set \(\{0,1,2,\ldots,n-1\}\). Find the maximum cost across all cyclic shifts of \(p\).\(^\dagger\operatorname{mex}([b_1,b_2,\ldots,b_m])\) is the smallest non-negative integer \(x\) such that \(x\) does not occur among \(b_1,b_2,\ldots,b_m\).\(^\ddagger\)A permutation of the set \(\{0,1,2,...,n-1\}\) is an array consisting of \(n\) distinct integers from \(0\) to \(n-1\) in arbitrary order. For example, \([1,2,0,4,3]\) is a permutation, but \([0,1,1]\) is not a permutation (\(1\) appears twice in the array), and \([0,2,3]\) is also not a permutation (\(n=3\) but there is \(3\) in the array).
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)) β the length of the permutation \(p\).The second line of each test case contain \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) (\(0 \le p_i < n\)) β the elements of the permutation \(p\).It is guaranteed that sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case, output a single integer β the maximum cost across all cyclic shifts of \(p\).
|
In the first test case, the cyclic shift that yields the maximum cost is \([2,1,0,5,4,3]\) with cost \(0+0+3+3+3+6=15\).In the second test case, the cyclic shift that yields the maximum cost is \([0,2,1]\) with cost \(1+1+3=5\).
|
Input: 465 4 3 2 1 032 1 082 3 6 7 0 1 4 510 | Output: 15 5 31 1
|
Hard
| 4 | 761 | 541 | 97 | 19 |
1,445 |
B
|
1445B
|
B. Elimination
| 900 |
greedy; math
|
There is a famous olympiad, which has more than a hundred participants. The Olympiad consists of two stages: the elimination stage, and the final stage. At least a hundred participants will advance to the final stage. The elimination stage in turn consists of two contests.A result of the elimination stage is the total score in two contests, but, unfortunately, the jury lost the final standings and has only standings for the first and for the second contest separately. In each contest, the participants are ranked by their point score in non-increasing order. When two participants have a tie (earned the same score), they are ranked by their passport number (in accordance with local regulations, all passport numbers are distinct). In the first contest, the participant on the 100-th place scored \(a\) points. Also, the jury checked all participants from the 1-st to the 100-th place (inclusive) in the first contest and found out that all of them have at least \(b\) points in the second contest.Similarly, for the second contest, the participant on the 100-th place has \(c\) points. And the jury checked that all the participants from the 1-st to the 100-th place (inclusive) have at least \(d\) points in the first contest.After two contests, all participants are ranked by their total score in two contests in non-increasing order. When participants have the same total score, tie-breaking with passport numbers is used. The cutoff score to qualify to the final stage is the total score of the participant on the 100-th place.Given integers \(a\), \(b\), \(c\), \(d\), please help the jury determine the smallest possible value of the cutoff score.
|
You need to process \(t\) test cases.The first line contains an integer \(t\) (\(1 \leq t \leq 3025\)) β the number of test cases. Then descriptions of \(t\) test cases follow.The first line of each test case contains four integers \(a\), \(b\), \(c\), \(d\) (\(0 \le a,\,b,\,c,\,d \le 9\); \(d \leq a\); \(b \leq c\)). One can show that for any test case satisfying the constraints above, there is at least one olympiad scenario possible.
|
For each test case print a single integer β the smallest possible cutoff score in some olympiad scenario satisfying the given information.
|
For the first test case, consider the following olympiad scenario: there are \(101\) participants in the elimination stage, each having \(1\) point for the first contest and \(2\) points for the second contest. Hence the total score of the participant on the 100-th place is \(3\).For the second test case, consider the following olympiad scenario: there are \(50\) participants with points \(5\) and \(9\) for the first and second contest respectively; \(50\) participants with points \(4\) and \(8\) for the first and second contest respectively; and \(50\) participants with points \(2\) and \(9\) for the first and second contest respectively. Hence the total point score of the participant on the 100-th place is \(12\).
|
Input: 2 1 2 2 1 4 8 9 2 | Output: 3 12
|
Beginner
| 2 | 1,660 | 439 | 138 | 14 |
570 |
A
|
570A
|
A. Elections
| 1,100 |
implementation
|
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
|
The first line of the input contains two integers n, m (1 β€ n, m β€ 100) β the number of candidates and of cities, respectively.Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 β€ j β€ n, 1 β€ i β€ m, 0 β€ aij β€ 109) denotes the number of votes for candidate j in city i.It is guaranteed that the total number of people in all the cities does not exceed 109.
|
Print a single number β the index of the candidate who won the elections. The candidates are indexed starting from one.
|
Note to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
|
Input: 3 31 2 32 3 11 2 1 | Output: 2
|
Easy
| 1 | 815 | 402 | 119 | 5 |
462 |
A
|
462A
|
A. Appleman and Easy Task
| 1,000 |
brute force; implementation
|
Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you help him?Given a n Γ n checkerboard. Each cell of the board has either character 'x', or character 'o'. Is it true that each cell of the board has even number of adjacent cells with 'o'? Two cells of the board are adjacent if they share a side.
|
The first line contains an integer n (1 β€ n β€ 100). Then n lines follow containing the description of the checkerboard. Each of them contains n characters (either 'x' or 'o') without spaces.
|
Print ""YES"" or ""NO"" (without the quotes) depending on the answer to the problem.
|
Input: 3xxoxoxoxx | Output: YES
|
Beginner
| 2 | 360 | 190 | 84 | 4 |
|
1,718 |
F
|
1718F
|
F. Burenka, an Array and Queries
| 3,300 |
data structures; math; number theory
|
Eugene got Burenka an array \(a\) of length \(n\) of integers from \(1\) to \(m\) for her birthday. Burenka knows that Eugene really likes coprime integers (integers \(x\) and \(y\) such that they have only one common factor (equal to \(1\))) so she wants to to ask Eugene \(q\) questions about the present. Each time Burenka will choose a subsegment \(a_l, a_{l + 1}, \ldots, a_r\) of array \(a\), and compute the product of these numbers \(p = a_l \cdot a_{l + 1} \cdot \ldots \cdot a_r\). Then she will ask Eugene to count the number of integers between \(1\) and \(C\) inclusive which are coprime with \(p\). Help Eugene answer all the questions!
|
In the first line of input there are four integers \(n\), \(m\), \(C\), \(q\) (\(1 \leq n, q \leq 10^5\), \(1 \leq m \leq 2\cdot 10^4\), \(1 \leq C \leq 10^5\)) β the length of the array \(a\), the maximum possible value of \(a_{i}\), the value \(C\), and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_{i} \leq m\)) β the array \(a\) .In the next \(q\) lines the queries are given. Each query consists of two integers \(l\) and \(r\) (\(1 \leq l \leq r \leq n\)).
|
Print \(q\) integers β the answers to Burenka's queries.
|
Here's an explanation for the example: in the first query, the product is equal to \(1\), which is coprime with \(1,2,3,4,5\). in the second query, the product is equal to \(12\), which is coprime with \(1\) and \(5\). in the third query, the product is equal to \(10\), which is coprime with \(1\) and \(3\).
|
Input: 5 5 5 3 1 2 3 2 5 1 1 2 4 4 5 | Output: 5 2 2
|
Master
| 3 | 650 | 517 | 56 | 17 |
986 |
D
|
986D
|
D. Perfect Encoding
| 3,100 |
fft; math
|
You are working as an analyst in a company working on a new system for big data storage. This system will store \(n\) different objects. Each object should have a unique ID.To create the system, you choose the parameters of the system β integers \(m \ge 1\) and \(b_{1}, b_{2}, \ldots, b_{m}\). With these parameters an ID of some object in the system is an array of integers \([a_{1}, a_{2}, \ldots, a_{m}]\) where \(1 \le a_{i} \le b_{i}\) holds for every \(1 \le i \le m\).Developers say that production costs are proportional to \(\sum_{i=1}^{m} b_{i}\). You are asked to choose parameters \(m\) and \(b_{i}\) so that the system will be able to assign unique IDs to \(n\) different objects and production costs are minimized. Note that you don't have to use all available IDs.
|
In the only line of input there is one positive integer \(n\). The length of the decimal representation of \(n\) is no greater than \(1.5 \cdot 10^{6}\). The integer does not contain leading zeros.
|
Print one number β minimal value of \(\sum_{i=1}^{m} b_{i}\).
|
Input: 36 | Output: 10
|
Master
| 2 | 780 | 197 | 61 | 9 |
|
1,846 |
F
|
1846F
|
F. Rudolph and Mimic
| 1,800 |
constructive algorithms; implementation; interactive
|
This is an interactive task.Rudolph is a scientist who studies alien life forms. There is a room in front of Rudolph with \(n\) different objects scattered around. Among the objects there is exactly one amazing creature β a mimic that can turn into any object. He has already disguised himself in this room and Rudolph needs to find him by experiment.The experiment takes place in several stages. At each stage, the following happens: Rudolf looks at all the objects in the room and writes down their types. The type of each object is indicated by a number; there can be several objects of the same type. After inspecting, Rudolph can point to an object that he thinks is a mimic. After that, the experiment ends. Rudolph only has one try, so if he is unsure of the mimic's position, he does the next step instead. Rudolf can remove any number of objects from the room (possibly zero). Then Rudolf leaves the room and at this time all objects, including the mimic, are mixed with each other, their order is changed, and the mimic can transform into any other object (even one that is not in the room). After this, Rudolf returns to the room and repeats the stage. The mimic may not change appearance, but it can not remain a same object for more than two stages in a row.Rudolf's task is to detect mimic in no more than five stages.
|
The first line contains one integer \(t\) \((1 \le t \le 1000)\) β the number of test cases.The first line of each test case contains one integer \(n\) \((2 \le n \le 200)\) β the number of objects in the room.The second line of each test case contains \(n\) integers \(a_1\),\(a_2\),...,\(a_n\) \((1 \le a_i \le 9)\) β object types.
|
Explanation for the first test: initial array is \(x_1\), \(x_2\), \(x_3\), \(x_4\), \(x_5\). Mimic is in first position. Delete the fifth object. After that, the positions are shuffled, and the mimic chose not to change his appearance. Object positions become \(x_4\), \(x_1\), \(x_2\), \(x_3\). Delete the third objects. The mimic is forced to turn into another object, because it has already been in the form \(1\) for two stages. The mimic chose to transform into \(2\), the objects are shuffled and become \(x_3\), \(x_4\), \(x_1\). Delete the first and second objects. The objects positions become \(x_1\). Only the mimic remains, and it remains an object \(2\). Point to the first element.
|
Input: 3 5 1 1 2 2 3 2 1 1 2 2 2 2 2 8 1 2 3 4 3 4 2 1 4 3 4 3 2 2 1 3 2 3 3 2 5 3 2 2 5 15 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 7 9 5 4 3 2 1 | Output: - 1 5 - 1 3 - 2 1 2 ! 1 - 0 - 4 1 3 7 8 - 1 4 - 1 2 ! 2 - 0 ! 10
|
Medium
| 3 | 1,332 | 333 | 0 | 18 |
|
1,638 |
B
|
1638B
|
B. Odd Swap Sort
| 1,100 |
data structures; math; sortings
|
You are given an array \(a_1, a_2, \dots, a_n\). You can perform operations on the array. In each operation you can choose an integer \(i\) (\(1 \le i < n\)), and swap elements \(a_i\) and \(a_{i+1}\) of the array, if \(a_i + a_{i+1}\) is odd.Determine whether it can be sorted in non-decreasing order using this operation any number of times.
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the length of the array.The second line of each test case contains \(n\) integers \(a_1,a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the elements of the array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print ""Yes"" or ""No"" depending on whether you can or can not sort the given array.You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as positive answer).
|
In the first test case, we can simply swap \(31\) and \(14\) (\(31 + 14 = 45\) which is odd) and obtain the non-decreasing array \([1,6,14,31]\).In the second test case, the only way we could sort the array is by swapping \(4\) and \(2\), but this is impossible, since their sum \(4 + 2 = 6\) is even.In the third test case, there is no way to make the array non-decreasing.In the fourth test case, the array is already non-decreasing.
|
Input: 441 6 31 1424 252 9 6 7 1036 6 6 | Output: Yes No No Yes
|
Easy
| 3 | 343 | 517 | 235 | 16 |
1,607 |
C
|
1607C
|
C. Minimum Extraction
| 1,000 |
brute force; sortings
|
Yelisey has an array \(a\) of \(n\) integers.If \(a\) has length strictly greater than \(1\), then Yelisei can apply an operation called minimum extraction to it: First, Yelisei finds the minimal number \(m\) in the array. If there are several identical minima, Yelisey can choose any of them. Then the selected minimal element is removed from the array. After that, \(m\) is subtracted from each remaining element. Thus, after each operation, the length of the array is reduced by \(1\).For example, if \(a = [1, 6, -4, -2, -4]\), then the minimum element in it is \(a_3 = -4\), which means that after this operation the array will be equal to \(a=[1 {- (-4)}, 6 {- (-4)}, -2 {- (-4)}, -4 {- (-4)}] = [5, 10, 2, 0]\).Since Yelisey likes big numbers, he wants the numbers in the array \(a\) to be as big as possible.Formally speaking, he wants to make the minimum of the numbers in array \(a\) to be maximal possible (i.e. he want to maximize a minimum). To do this, Yelisey can apply the minimum extraction operation to the array as many times as he wants (possibly, zero). Note that the operation cannot be applied to an array of length \(1\).Help him find what maximal value can the minimal element of the array have after applying several (possibly, zero) minimum extraction operations to the array.
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The next \(2t\) lines contain descriptions of the test cases.In the description of each test case, the first line contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the original length of the array \(a\). The second line of the description lists \(n\) space-separated integers \(a_i\) (\(-10^9 \leq a_i \leq 10^9\)) β elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
Print \(t\) lines, each of them containing the answer to the corresponding test case. The answer to the test case is a single integer β the maximal possible minimum in \(a\), which can be obtained by several applications of the described operation to it.
|
In the first example test case, the original length of the array \(n = 1\). Therefore minimum extraction cannot be applied to it. Thus, the array remains unchanged and the answer is \(a_1 = 10\).In the second set of input data, the array will always consist only of zeros.In the third set, the array will be changing as follows: \([\color{blue}{-1}, 2, 0] \to [3, \color{blue}{1}] \to [\color{blue}{2}]\). The minimum elements are highlighted with \(\color{blue}{\text{blue}}\). The maximal one is \(2\).In the fourth set, the array will be modified as \([2, 10, \color{blue}{1}, 7] \to [\color{blue}{1}, 9, 6] \to [8, \color{blue}{5}] \to [\color{blue}{3}]\). Similarly, the maximum of the minimum elements is \(5\).
|
Input: 8 1 10 2 0 0 3 -1 2 0 4 2 10 1 7 2 2 3 5 3 2 -4 -2 0 2 -1 1 1 -2 | Output: 10 0 2 5 2 2 2 -2
|
Beginner
| 2 | 1,303 | 541 | 254 | 16 |
747 |
C
|
747C
|
C. Servers
| 1,300 |
implementation
|
There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id β integer from 1 to n.It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti β the moment in seconds in which the task will come, ki β the number of servers needed to perform it, and di β the time needed to perform this task in seconds. All ti are distinct.To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored.Write the program that determines which tasks will be performed and which will be ignored.
|
The first line contains two positive integers n and q (1 β€ n β€ 100, 1 β€ q β€ 105) β the number of servers and the number of tasks. Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 β€ ti β€ 106, 1 β€ ki β€ n, 1 β€ di β€ 1000) β the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds.
|
Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1.
|
In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10).In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task.
|
Input: 4 31 3 22 2 13 4 3 | Output: 6-110
|
Easy
| 1 | 931 | 500 | 171 | 7 |
1,267 |
B
|
1267B
|
B. Balls of Buma
| 900 |
Balph is learning to play a game called Buma. In this game, he is given a row of colored balls. He has to choose the color of one new ball and the place to insert it (between two balls, or to the left of all the balls, or to the right of all the balls).When the ball is inserted the following happens repeatedly: if some segment of balls of the same color became longer as a result of a previous action and its length became at least \(3\), then all the balls of this segment are eliminated. Consider, for example, a row of balls 'AAABBBWWBB'. Suppose Balph chooses a ball of color 'W' and the place to insert it after the sixth ball, i. e. to the left of the two 'W's. After Balph inserts this ball, the balls of color 'W' are eliminated, since this segment was made longer and has length \(3\) now, so the row becomes 'AAABBBBB'. The balls of color 'B' are eliminated now, because the segment of balls of color 'B' became longer and has length \(5\) now. Thus, the row becomes 'AAA'. However, none of the balls are eliminated now, because there is no elongated segment.Help Balph count the number of possible ways to choose a color of a new ball and a place to insert it that leads to the elimination of all the balls.
|
The only line contains a non-empty string of uppercase English letters of length at most \(3 \cdot 10^5\). Each letter represents a ball with the corresponding color.
|
Output the number of ways to choose a color and a position of a new ball in order to eliminate all the balls.
|
Input: BBWWBB | Output: 3
|
Beginner
| 0 | 1,220 | 166 | 109 | 12 |
||
1,038 |
A
|
1038A
|
A. Equality
| 800 |
implementation; strings
|
You are given a string \(s\) of length \(n\), which consists only of the first \(k\) letters of the Latin alphabet. All letters in string \(s\) are uppercase.A subsequence of string \(s\) is a string that can be derived from \(s\) by deleting some of its symbols without changing the order of the remaining symbols. For example, ""ADE"" and ""BD"" are subsequences of ""ABCDE"", but ""DEA"" is not.A subsequence of \(s\) called good if the number of occurences of each of the first \(k\) letters of the alphabet is the same.Find the length of the longest good subsequence of \(s\).
|
The first line of the input contains integers \(n\) (\(1\le n \le 10^5\)) and \(k\) (\(1 \le k \le 26\)).The second line of the input contains the string \(s\) of length \(n\). String \(s\) only contains uppercase letters from 'A' to the \(k\)-th letter of Latin alphabet.
|
Print the only integer β the length of the longest good subsequence of string \(s\).
|
In the first example, ""ACBCAB"" (""ACAABCCAB"") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence ""CAB"" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is \(0\).
|
Input: 9 3ACAABCCAB | Output: 6
|
Beginner
| 2 | 581 | 272 | 84 | 10 |
1,338 |
C
|
1338C
|
C. Perfect Triples
| 2,200 |
bitmasks; brute force; constructive algorithms; divide and conquer; math
|
Consider the infinite sequence \(s\) of positive integers, created by repeating the following steps: Find the lexicographically smallest triple of positive integers \((a, b, c)\) such that \(a \oplus b \oplus c = 0\), where \(\oplus\) denotes the bitwise XOR operation. \(a\), \(b\), \(c\) are not in \(s\). Here triple of integers \((a_1, b_1, c_1)\) is considered to be lexicographically smaller than triple \((a_2, b_2, c_2)\) if sequence \([a_1, b_1, c_1]\) is lexicographically smaller than sequence \([a_2, b_2, c_2]\). Append \(a\), \(b\), \(c\) to \(s\) in this order. Go back to the first step. You have integer \(n\). Find the \(n\)-th element of \(s\).You have to answer \(t\) independent test cases.A sequence \(a\) is lexicographically smaller than a sequence \(b\) if in the first position where \(a\) and \(b\) differ, the sequence \(a\) has a smaller element than the corresponding element in \(b\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases.Each of the next \(t\) lines contains a single integer \(n\) (\(1\le n \le 10^{16}\)) β the position of the element you want to know.
|
In each of the \(t\) lines, output the answer to the corresponding test case.
|
The first elements of \(s\) are \(1, 2, 3, 4, 8, 12, 5, 10, 15, \dots \)
|
Input: 9 1 2 3 4 5 6 7 8 9 | Output: 1 2 3 4 8 12 5 10 15
|
Hard
| 5 | 915 | 230 | 77 | 13 |
1,439 |
C
|
1439C
|
C. Greedy Shopping
| 2,600 |
binary search; data structures; divide and conquer; greedy; implementation
|
You are given an array \(a_1, a_2, \ldots, a_n\) of integers. This array is non-increasing.Let's consider a line with \(n\) shops. The shops are numbered with integers from \(1\) to \(n\) from left to right. The cost of a meal in the \(i\)-th shop is equal to \(a_i\).You should process \(q\) queries of two types: 1 x y: for each shop \(1 \leq i \leq x\) set \(a_{i} = max(a_{i}, y)\). 2 x y: let's consider a hungry man with \(y\) money. He visits the shops from \(x\)-th shop to \(n\)-th and if he can buy a meal in the current shop he buys one item of it. Find how many meals he will purchase. The man can buy a meal in the shop \(i\) if he has at least \(a_i\) money, and after it his money decreases by \(a_i\).
|
The first line contains two integers \(n\), \(q\) (\(1 \leq n, q \leq 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_{1},a_{2}, \ldots, a_{n}\) \((1 \leq a_{i} \leq 10^9)\) β the costs of the meals. It is guaranteed, that \(a_1 \geq a_2 \geq \ldots \geq a_n\).Each of the next \(q\) lines contains three integers \(t\), \(x\), \(y\) (\(1 \leq t \leq 2\), \(1\leq x \leq n\), \(1 \leq y \leq 10^9\)), each describing the next query.It is guaranteed that there exists at least one query of type \(2\).
|
For each query of type \(2\) output the answer on the new line.
|
In the first query a hungry man will buy meals in all shops from \(3\) to \(10\).In the second query a hungry man will buy meals in shops \(4\), \(9\), and \(10\).After the third query the array \(a_1, a_2, \ldots, a_n\) of costs won't change and will be \(\{10, 10, 10, 6, 6, 5, 5, 5, 3, 1\}\).In the fourth query a hungry man will buy meals in shops \(2\), \(3\), \(4\), \(5\), \(9\), and \(10\).After the fifth query the array \(a\) of costs will be \(\{10, 10, 10, 7, 6, 5, 5, 5, 3, 1\}\).In the sixth query a hungry man will buy meals in shops \(2\) and \(4\).
|
Input: 10 6 10 10 10 6 6 5 5 5 3 1 2 3 50 2 4 10 1 3 10 2 2 36 1 4 7 2 2 17 | Output: 8 3 6 2
|
Expert
| 5 | 717 | 512 | 63 | 14 |
2,068 |
E
|
2068E
|
E. Porto Vs. Benfica
| 2,800 |
data structures; dfs and similar; dsu; graphs; shortest paths
|
FC Porto and SL Benfica are the two largest football teams in Portugal. Naturally, when the two play each other, a lot of people travel from all over the country to watch the game. This includes the Benfica supporters' club, which is going to travel from Lisbon to Porto to watch the upcoming game. To avoid tensions between them and the Porto supporters' club, the national police want to delay their arrival to Porto as much as they can.The road network in Portugal can be modelled as a simple, undirected, unweighted, connected graph with \(n\) vertices and \(m\) edges, where vertices represent towns and edges represent roads. Vertex \(1\) corresponds to Lisbon, i.e., the starting vertex of the supporters' club, and vertex \(n\) is Porto, i.e., the destination vertex of the supporters' club. The supporters' club wants to minimize the number of roads they take to reach Porto.The police are following the supporters' club carefully, and so they always know where they are. To delay their arrival, at any point the police can pick exactly one road and block it, as long as the supporters' club isn't currently traversing it. They can do this exactly once, and once they do that, the road is blocked forever. Once the police block a road, the supporters' club immediately learns that that road is blocked, and they can change their route however they prefer. Furthermore, the supporters' club knows that the police are planning on blocking some road and can plan their route accordingly.Assuming that both the supporters' club and the police always make optimal choices, determine the minimum number of roads the supporters' club needs to traverse to go from Lisbon to Porto. If the police can block the supporters' club from ever reaching Porto, then output \(-1\).
|
The first line contains two integers \(n\) and \(m\) (\(2 \leq n \leq 200\,000\), \(n - 1 \leq m \leq \min\{n(n - 1)/2, 200\,000\}\)) β the number of towns and the number of roads in the road network of Portugal.Each of the next \(m\) lines contains two integers \(s_i\) and \(t_i\) (\(1 \leq s_i, t_i \leq n\)) β the two towns connected by the \(i\)-th road.It is guaranteed that the road network is connected, each road connects two distinct towns, and that there are no repeated roads.
|
Print the minimum number of roads the supporters' club needs to traverse to travel from Lisbon to Porto.
|
In the first sample, the road network is represented by the following picture: Note that the optimal strategy for the police is to wait until the supporters' club is on a vertex adjacent to the destination (i.e., vertex \(5\)) and then block the edge that connects that vertex to the destination. The optimal strategy for the supporters' club is to follow the upper path (by going from \(1\) to \(2\)) and then after seeing the edge between \(2\) and \(5\) get blocked, they will go around by following \(2\) back to \(1\) and then following the lower path (\(1\) to \(3\) to \(4\) to \(5\)). The number of traversed roads in this case is \(5\).In the second sample, the road network is represented by the following picture: There are multiple strategies here, but one can verify that the optimal one for both the police and supporters' club is to follow the upper path (\(1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5\)), then the police block the edge \(5\) to \(11\), and so the supporters' club go around by following \(5 \rightarrow 4 \rightarrow 3 \rightarrow 6 \rightarrow 7 \rightarrow 11\). The total number of traversed roads of this strategy is \(9\).In the third sample, the road network is represented by the following picture: The optimal strategy for the police is to block edge \(2\) to \(3\) if the supporters' club reaches vertex \(2\), or block edge \(5\) to \(6\) if the supporters' club reaches vertex \(5\). The optimal path in this case is \(1 \rightarrow 2 \rightarrow 1 \rightarrow 5 \rightarrow 6 \rightarrow 8\). The total number of traversed roads in this case is \(5\).
|
Input: 5 51 21 32 53 44 5 | Output: 5
|
Master
| 5 | 1,772 | 488 | 104 | 20 |
1,978 |
F
|
1978F
|
F. Large Graph
| 2,400 |
data structures; dfs and similar; dsu; graphs; number theory; two pointers
|
Given an array \(a\) of length \(n\). Let's construct a square matrix \(b\) of size \(n \times n\), in which the \(i\)-th row contains the array \(a\) cyclically shifted to the right by \((i - 1)\). For example, for the array \(a = [3, 4, 5]\), the obtained matrix is$$$\(b = \begin{bmatrix} 3 & 4 & 5 \\ 5 & 3 & 4 \\ 4 & 5 & 3 \end{bmatrix}\)\(Let's construct the following graph: The graph contains \)n^2\( vertices, each of which corresponds to one of the elements of the matrix. Let's denote the vertex corresponding to the element \)b_{i, j}\( as \)(i, j)\(. We will draw an edge between vertices \)(i_1, j_1)\( and \)(i_2, j_2)\( if \)|i_1 - i_2| + |j_1 - j_2| \le k\( and \)\gcd(b_{i_1, j_1}, b_{i_2, j_2}) > 1\(, where \)\gcd(x, y)\( denotes the greatest common divisor of integers \)x\( and \)y\(. Your task is to calculate the number of connected components\)^{\dagger}\( in the obtained graph.\)^{\dagger}$$$A connected component of a graph is a set of vertices in which any vertex is reachable from any other via edges, and adding any other vertex to the set violates this rule.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(2 \le n \le 10^6\), \(2 \le k \le 2 \cdot 10^6\)) β the length of the array and the parameter \(k\). The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^6\)) β the elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case, output a single integer β the number of connected components in the obtained graph.
|
In the first test case, the matrix \(b\) is given in the statement. The first connected component contains the vertices \((1, 1)\), \((2, 2)\), and \((3, 3)\). The second connected component contains the vertices \((1, 2)\), \((2, 3)\), and \((3, 1)\). The third connected component contains the vertices \((1, 3)\), \((2, 1)\), and \((3, 2)\). Thus, the graph has \(3\) connected components.In the second test case, the following matrix is obtained:$$$\(b = \begin{bmatrix} 3 & 4 & 9 \\ 9 & 3 & 4 \\ 4 & 9 & 3 \end{bmatrix}\)\(The first connected component contains all vertices corresponding to elements with values \)3\( and \)9\(. The second connected component contains all vertices corresponding to elements with the value \)4$$$.In the fourth test case, all vertices are in one connected component.
|
Input: 63 33 4 53 33 4 93 23 4 92 22 85 38 27 5 4 34 102 2 2 2 | Output: 3 2 3 1 4 1
|
Expert
| 6 | 1,090 | 587 | 103 | 19 |
753 |
C
|
753C
|
C. Interactive Bulls and Cows (Hard)
| 2,500 |
brute force; constructive algorithms; interactive
|
The only difference from the previous problem is the constraint on the number of requests. In this problem your program should guess the answer doing at most 7 requests.This problem is a little bit unusual. Here you are to implement an interaction with a testing system. That means that you can make queries and get responses in the online mode. Please be sure to use the stream flushing operation after each query's output in order not to leave part of your output in some buffer. For example, in C++ you've got to use the fflush(stdout) function, in Java β call System.out.flush(), and in Pascal β flush(output).Bulls and Cows (also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots) is an old code-breaking paper and pencil game for two players, predating the similar commercially marketed board game Mastermind.On a sheet of paper, the first player thinks a secret string. This string consists only of digits and has the length 4. The digits in the string must be all different, no two or more equal digits are allowed.Then the second player tries to guess his opponent's string. For every guess the first player gives the number of matches. If the matching digits are on their right positions, they are ""bulls"", if on different positions, they are ""cows"". Thus a response is a pair of numbers β the number of ""bulls"" and the number of ""cows"". A try can contain equal digits.More formally, let's the secret string is s and the second player are trying to guess it with a string x. The number of ""bulls"" is a number of such positions i (1 β€ i β€ 4) where s[i] = x[i]. The number of ""cows"" is a number of such digits c that s contains c in the position i (i.e. s[i] = c), x contains c, but x[i] β c.For example, the secret string is ""0427"", the opponent's try is ""0724"", then the answer is 2 bulls and 2 cows (the bulls are ""0"" and ""2"", the cows are ""4"" and ""7""). If the secret string is ""0123"", the opponent's try is ""0330"", then the answer is 1 bull and 1 cow.In this problem you are to guess the string s that the system has chosen. You only know that the chosen string consists of 4 distinct digits.You can make queries to the testing system, each query is the output of a single 4-digit string. The answer to the query is the number of bulls and number of cows. If the system's response equals ""4 0"", that means the interaction with your problem is over and the program must terminate. That is possible for two reasons β the program either guessed the number x or made an invalid action (for example, printed letters instead of digits).Your program is allowed to do at most 7 queries.You can hack solutions of other participants providing a 4-digit string containing distinct digits β the secret string.
|
To read answers to the queries, the program must use the standard input.The program will receive pairs of non-negative integers in the input, one pair per line. The first number in a pair is a number of bulls and the second one is a number of cows of the string s and the string xi printed by your program. If the system response equals ""4 0"", then your solution should terminate.The testing system will let your program read the i-th pair of integers from the input only after your program displays the corresponding system query in the output: prints value xi in a single line and executes operation flush.
|
The program must use the standard output to print queries.Your program must output requests β 4-digit strings x1, x2, ..., one per line. After the output of each line the program must execute flush operation. The program should read the answer to the query from the standard input.Your program is allowed to do at most 7 queries.
|
The secret string s in the example is ""0123"".
|
Input: 0 12 01 10 42 14 0 | Output: 800001793159321001120123
|
Expert
| 3 | 2,751 | 610 | 329 | 7 |
1,413 |
B
|
1413B
|
B. A New Technique
| 1,100 |
implementation
|
All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of \(n\cdot m\) different seals, denoted by distinct numbers. All of them were written in an \(n\times m\) table.The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique.
|
The first line of the input contains the only integer \(t\) (\(1\leq t\leq 100\,000\)) denoting the number of test cases. Their descriptions follow.The first line of each test case description consists of two space-separated integers \(n\) and \(m\) (\(1 \leq n, m \leq 500\)) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from \(1\) to \(n\cdot m\).The following \(n\) lines contain \(m\) space separated integers each, denoting elements of an arbitrary row in the table left to right.The following \(m\) lines contain \(n\) space separated integers each, denoting elements of an arbitrary column in the table top to bottom.Sum of \(nm\) over all test cases does not exceed \(250\,000\). It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from \(1\) to \(nm\) occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists.
|
For each test case, output \(n\) lines with \(m\) space-separated integers each, denoting the restored table. One can show that the answer is always unique.
|
Consider the first test case. The matrix is \(2 \times 3\). You are given the rows and columns in arbitrary order.One of the rows is \([6, 5, 4]\). One of the rows is \([1, 2, 3]\).One of the columns is \([1, 6]\). One of the columns is \([2, 5]\). One of the columns is \([3, 4]\).You are to reconstruct the matrix. The answer is given in the output.
|
Input: 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 | Output: 1 2 3 6 5 4 3 1 2
|
Easy
| 1 | 535 | 1,048 | 156 | 14 |
1,994 |
H
|
1994H
|
H. Fortnite
| 3,500 |
combinatorics; constructive algorithms; games; greedy; hashing; interactive; math; number theory; strings
|
This is an interactive problem!Timofey is writing a competition called Capture the Flag (or CTF for short). He has one task left, which involves hacking a security system. The entire system is based on polynomial hashes\(^{\text{β}}\).Timofey can input a string consisting of lowercase Latin letters into the system, and the system will return its polynomial hash. To hack the system, Timofey needs to find the polynomial hash parameters (\(p\) and \(m\)) that the system uses.Timofey doesn't have much time left, so he will only be able to make \(3\) queries. Help him solve the task.\(^{\text{β}}\) The polynomial hash of a string \(s\), consisting of lowercase Latin letters of length \(n\), based on \(p\) and modulo \(m\) is \((\mathrm{ord}(s_1) \cdot p ^ 0 + \mathrm{ord}(s_2) \cdot p ^ 1 + \mathrm{ord}(s_3) \cdot p ^ 2 + \ldots + \mathrm{ord}(s_n) \cdot p ^ {n - 1}) \bmod m\). Where \(s_i\) denotes the \(i\)-th character of the string \(s\), \(\mathrm{ord}(\mathrm{chr})\) denotes the ordinal number of the character \(\mathrm{chr}\) in the English alphabet, and \(x \bmod m\) is the remainder of \(x\) when divided by \(m\).
|
Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^3\)) β the number of test cases.It is guaranteed that the \(p\) and \(m\) used by the system satisfy the conditions: \(26 < p \leq 50\) and \(p + 1 < m \leq 2 \cdot 10^9\).
|
Answer for the first query is \((ord(a) \cdot 31^0 + ord(a) \cdot 31^1) \mod 59 = (1 + 1 \cdot 31) \mod 59 = 32\).Answer for the second query is \((ord(y) \cdot 31^0 + ord(b) \cdot 31^1) \mod 59 = (25 + 2 \cdot 31) \mod 59 = 28\).
|
Input: 1 32 28 | Output: ? aa ? yb ! 31 59
|
Master
| 9 | 1,135 | 276 | 0 | 19 |
|
490 |
D
|
490D
|
D. Chocolate
| 1,900 |
brute force; dfs and similar; math; meet-in-the-middle; number theory
|
Polycarpus likes giving presents to Paraskevi. He has bought two chocolate bars, each of them has the shape of a segmented rectangle. The first bar is a1 Γ b1 segments large and the second one is a2 Γ b2 segments large.Polycarpus wants to give Paraskevi one of the bars at the lunch break and eat the other one himself. Besides, he wants to show that Polycarpus's mind and Paraskevi's beauty are equally matched, so the two bars must have the same number of squares.To make the bars have the same number of squares, Polycarpus eats a little piece of chocolate each minute. Each minute he does the following: he either breaks one bar exactly in half (vertically or horizontally) and eats exactly a half of the bar, or he chips of exactly one third of a bar (vertically or horizontally) and eats exactly a third of the bar. In the first case he is left with a half, of the bar and in the second case he is left with two thirds of the bar.Both variants aren't always possible, and sometimes Polycarpus cannot chip off a half nor a third. For example, if the bar is 16 Γ 23, then Polycarpus can chip off a half, but not a third. If the bar is 20 Γ 18, then Polycarpus can chip off both a half and a third. If the bar is 5 Γ 7, then Polycarpus cannot chip off a half nor a third.What is the minimum number of minutes Polycarpus needs to make two bars consist of the same number of squares? Find not only the required minimum number of minutes, but also the possible sizes of the bars after the process.
|
The first line of the input contains integers a1, b1 (1 β€ a1, b1 β€ 109) β the initial sizes of the first chocolate bar. The second line of the input contains integers a2, b2 (1 β€ a2, b2 β€ 109) β the initial sizes of the second bar.You can use the data of type int64 (in Pascal), long long (in Π‘++), long (in Java) to process large integers (exceeding 231 - 1).
|
In the first line print m β the sought minimum number of minutes. In the second and third line print the possible sizes of the bars after they are leveled in m minutes. Print the sizes using the format identical to the input format. Print the sizes (the numbers in the printed pairs) in any order. The second line must correspond to the first bar and the third line must correspond to the second bar. If there are multiple solutions, print any of them.If there is no solution, print a single line with integer -1.
|
Input: 2 62 3 | Output: 11 62 3
|
Hard
| 5 | 1,497 | 360 | 513 | 4 |
|
1,681 |
B
|
1681B
|
B. Card Trick
| 800 |
implementation; math
|
Monocarp has just learned a new card trick, and can't wait to present it to you. He shows you the entire deck of \(n\) cards. You see that the values of cards from the topmost to the bottommost are integers \(a_1, a_2, \dots, a_n\), and all values are different.Then he asks you to shuffle the deck \(m\) times. With the \(j\)-th shuffle, you should take \(b_j\) topmost cards and move them under the remaining \((n - b_j)\) cards without changing the order.And then, using some magic, Monocarp tells you the topmost card of the deck. However, you are not really buying that magic. You tell him that you know the topmost card yourself. Can you surprise Monocarp and tell him the topmost card before he shows it?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of cards in the deck.The second line contains \(n\) pairwise distinct integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) β the values of the cards.The third line contains a single integer \(m\) (\(1 \le m \le 2 \cdot 10^5\)) β the number of shuffles.The fourth line contains \(m\) integers \(b_1, b_2, \dots, b_m\) (\(1 \le b_j \le n - 1\)) β the amount of cards that are moved on the \(j\)-th shuffle.The sum of \(n\) over all testcases doesn't exceed \(2 \cdot 10^5\). The sum of \(m\) over all testcases doesn't exceed \(2 \cdot 10^5\).
|
For each testcase, print a single integer β the value of the card on the top of the deck after the deck is shuffled \(m\) times.
|
In the first testcase, each shuffle effectively swaps two cards. After three swaps, the deck will be \([2, 1]\).In the second testcase, the second shuffle cancels what the first shuffle did. First, three topmost cards went underneath the last card, then that card went back below the remaining three cards. So the deck remained unchanged from the initial one β the topmost card has value \(3\).
|
Input: 321 231 1 143 1 4 223 152 1 5 4 353 2 1 2 1 | Output: 2 3 3
|
Beginner
| 2 | 711 | 748 | 128 | 16 |
1,083 |
E
|
1083E
|
E. The Fair Nut and Rectangles
| 2,400 |
data structures; dp; geometry
|
The Fair Nut got stacked in planar world. He should solve this task to get out.You are given \(n\) rectangles with vertexes in \((0, 0)\), \((x_i, 0)\), \((x_i, y_i)\), \((0, y_i)\). For each rectangle, you are also given a number \(a_i\). Choose some of them that the area of union minus sum of \(a_i\) of the chosen ones is maximum.It is guaranteed that there are no nested rectangles.Nut has no idea how to find the answer, so he asked for your help.
|
The first line contains one integer \(n\) (\(1 \leq n \leq 10^6\)) β the number of rectangles.Each of the next \(n\) lines contains three integers \(x_i\), \(y_i\) and \(a_i\) (\(1 \leq x_i, y_i \leq 10^9\), \(0 \leq a_i \leq x_i \cdot y_i\)).It is guaranteed that there are no nested rectangles.
|
In a single line print the answer to the problem β the maximum value which you can achieve.
|
In the first example, the right answer can be achieved by choosing the first and the second rectangles.In the second example, the right answer can also be achieved by choosing the first and the second rectangles.
|
Input: 3 4 4 8 1 5 0 5 2 10 | Output: 9
|
Expert
| 3 | 453 | 296 | 91 | 10 |
1,627 |
C
|
1627C
|
C. Not Assigning
| 1,400 |
constructive algorithms; dfs and similar; number theory; trees
|
You are given a tree of \(n\) vertices numbered from \(1\) to \(n\), with edges numbered from \(1\) to \(n-1\). A tree is a connected undirected graph without cycles. You have to assign integer weights to each edge of the tree, such that the resultant graph is a prime tree.A prime tree is a tree where the weight of every path consisting of one or two edges is prime. A path should not visit any vertex twice. The weight of a path is the sum of edge weights on that path.Consider the graph below. It is a prime tree as the weight of every path of two or less edges is prime. For example, the following path of two edges: \(2 \to 1 \to 3\) has a weight of \(11 + 2 = 13\), which is prime. Similarly, the path of one edge: \(4 \to 3\) has a weight of \(5\), which is also prime. Print any valid assignment of weights such that the resultant tree is a prime tree. If there is no such assignment, then print \(-1\). It can be proven that if a valid assignment exists, one exists with weights between \(1\) and \(10^5\) as well.
|
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains one integer \(n\) (\(2 \leq n \leq 10^5\)) β the number of vertices in the tree.Then, \(n-1\) lines follow. The \(i\)-th line contains two integers \(u\) and \(v\) (\(1 \leq u, v \leq n\)) denoting that edge number \(i\) is between vertices \(u\) and \(v\). It is guaranteed that the edges form a tree.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, if a valid assignment exists, then print a single line containing \(n-1\) integers \(a_1, a_2, \dots, a_{n-1}\) (\(1 \leq a_i \le 10^5\)), where \(a_i\) denotes the weight assigned to the edge numbered \(i\). Otherwise, print \(-1\).If there are multiple solutions, you may print any.
|
For the first test case, there are only two paths having one edge each: \(1 \to 2\) and \(2 \to 1\), both having a weight of \(17\), which is prime. The second test case is described in the statement.It can be proven that no such assignment exists for the third test case.
|
Input: 321 241 34 32 171 21 33 43 56 27 2 | Output: 17 2 5 11 -1
|
Easy
| 4 | 1,024 | 607 | 304 | 16 |
515 |
D
|
515D
|
D. Drazil and Tiles
| 2,000 |
constructive algorithms; greedy
|
Drazil created a following problem about putting 1 Γ 2 tiles into an n Γ m grid:""There is a grid with some cells that are empty and some cells that are occupied. You should use 1 Γ 2 tiles to cover all empty cells and no two tiles should cover each other. And you should print a solution about how to do it.""But Drazil doesn't like to write special checking program for this task. His friend, Varda advised him: ""how about asking contestant only to print the solution when it exists and it is unique? Otherwise contestant may print 'Not unique' "".Drazil found that the constraints for this task may be much larger than for the original task!Can you solve this new problem?Note that you should print 'Not unique' either when there exists no solution or when there exists several different solutions for the original task.
|
The first line contains two integers n and m (1 β€ n, m β€ 2000).The following n lines describe the grid rows. Character '.' denotes an empty cell, and the character '*' denotes a cell that is occupied.
|
If there is no solution or the solution is not unique, you should print the string ""Not unique"".Otherwise you should print how to cover all empty cells with 1 Γ 2 tiles. Use characters ""<>"" to denote horizontal tiles and characters ""^v"" to denote vertical tiles. Refer to the sample test for the output format example.
|
In the first case, there are indeed two solutions:<>^^*vv<>and^<>v*^<>vso the answer is ""Not unique"".
|
Input: 3 3....*.... | Output: Not unique
|
Hard
| 2 | 824 | 200 | 324 | 5 |
744 |
C
|
744C
|
C. Hongcow Buys a Deck of Cards
| 2,400 |
bitmasks; brute force; dp
|
One day, Hongcow goes to the store and sees a brand new deck of n special cards. Each individual card is either red or blue. He decides he wants to buy them immediately. To do this, he needs to play a game with the owner of the store.This game takes some number of turns to complete. On a turn, Hongcow may do one of two things: Collect tokens. Hongcow collects 1 red token and 1 blue token by choosing this option (thus, 2 tokens in total per one operation). Buy a card. Hongcow chooses some card and spends tokens to purchase it as specified below. The i-th card requires ri red resources and bi blue resources. Suppose Hongcow currently has A red cards and B blue cards. Then, the i-th card will require Hongcow to spend max(ri - A, 0) red tokens, and max(bi - B, 0) blue tokens. Note, only tokens disappear, but the cards stay with Hongcow forever. Each card can be bought only once.Given a description of the cards and their costs determine the minimum number of turns Hongcow needs to purchase all cards.
|
The first line of input will contain a single integer n (1 β€ n β€ 16).The next n lines of input will contain three tokens ci, ri and bi. ci will be 'R' or 'B', denoting the color of the card as red or blue. ri will be an integer denoting the amount of red resources required to obtain the card, and bi will be an integer denoting the amount of blue resources required to obtain the card (0 β€ ri, bi β€ 107).
|
Output a single integer, denoting the minimum number of turns needed to acquire all the cards.
|
For the first sample, Hongcow's four moves are as follows: Collect tokens Buy card 1 Buy card 2 Buy card 3 Note, at the fourth step, Hongcow is able to buy card 3 because Hongcow already has one red and one blue card, so we don't need to collect tokens.For the second sample, one optimal strategy is as follows: Collect tokens Collect tokens Buy card 2 Collect tokens Buy card 3 Buy card 1 At the fifth step, even though Hongcow has a red token, Hongcow doesn't actually need to spend it, since Hongcow has a red card already.
|
Input: 3R 0 1B 1 0R 1 1 | Output: 4
|
Expert
| 3 | 1,010 | 405 | 94 | 7 |
2,124 |
G
|
2124G
|
G. Maximise Sum
| 3,200 |
binary search; data structures
|
This problem differs from problem B. In this problem, you must output the maximum sum of prefix minimums over all operations that cost at least \(x\) for each integer \(x\) from \(0\) to \(n-1\).You are given an array \(a\) of length \(n\), with elements satisfying \(\boldsymbol{0 \le a_i \le n}\). You can perform the following operation at most once: Choose two indices \(i\) and \(j\) such that \(i < j\). Set \(a_i := a_i + a_j\). Then, set \(a_j = 0\). Let the cost of one operation be the value of \(j-i\). The cost of not performing an operation is \(0\).For each integer \(x\) from \(0\) to \(n-1\) inclusive, output the maximum possible value of \(\min(a_1) + \min(a_1,a_2) + \ldots + \min(a_1, a_2, \ldots, a_n)\) over all possible operations that have a cost of at least \(x\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 10^6\)) β the length of \(a\).The following line contains \(n\) space-separated integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le n\)) β denoting the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case, output \(n\) integers on a new line: the \(i\)-th integer denoting the maximum answer over all operations that have a cost of at least \(i-1\).
|
Let's analyze the fifth test case: \(x=0,1,2\): the optimal operation is \(i=2\) and \(j=4\), which has a cost of \(2\). The array \(a\) becomes \([4,4,3,0,1]\), which has a score of \(11\). \(x=3\): the optimal operation is \(i=2\) and \(j=5\), which has a cost of \(3\). The array \(a\) becomes \([4,2,3,3,0]\), which has a score of \(10\). \(x=4\): the optimal (and only) operation is \(i=1\) and \(j=5\), which has a cost of \(4\). The array \(a\) becomes \([5,1,3,3,0]\), which has a score of \(8\).
|
Input: 621 242 3 1 421 055 5 0 5 554 1 3 3 1107 4 7 0 8 7 5 0 2 1 | Output: 3 3 10 10 10 10 1 1 20 20 20 15 15 11 11 11 10 8 27 27 27 27 23 23 20 19 17 16
|
Master
| 2 | 789 | 486 | 163 | 21 |
1,452 |
D
|
1452D
|
D. Radio Towers
| 1,600 |
combinatorics; dp; math
|
There are \(n + 2\) towns located on a coordinate line, numbered from \(0\) to \(n + 1\). The \(i\)-th town is located at the point \(i\).You build a radio tower in each of the towns \(1, 2, \dots, n\) with probability \(\frac{1}{2}\) (these events are independent). After that, you want to set the signal power on each tower to some integer from \(1\) to \(n\) (signal powers are not necessarily the same, but also not necessarily different). The signal from a tower located in a town \(i\) with signal power \(p\) reaches every city \(c\) such that \(|c - i| < p\).After building the towers, you want to choose signal powers in such a way that: towns \(0\) and \(n + 1\) don't get any signal from the radio towers; towns \(1, 2, \dots, n\) get signal from exactly one radio tower each. For example, if \(n = 5\), and you have built the towers in towns \(2\), \(4\) and \(5\), you may set the signal power of the tower in town \(2\) to \(2\), and the signal power of the towers in towns \(4\) and \(5\) to \(1\). That way, towns \(0\) and \(n + 1\) don't get the signal from any tower, towns \(1\), \(2\) and \(3\) get the signal from the tower in town \(2\), town \(4\) gets the signal from the tower in town \(4\), and town \(5\) gets the signal from the tower in town \(5\).Calculate the probability that, after building the towers, you will have a way to set signal powers to meet all constraints.
|
The first (and only) line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).
|
Print one integer β the probability that there will be a way to set signal powers so that all constraints are met, taken modulo \(998244353\).Formally, the probability can be expressed as an irreducible fraction \(\frac{x}{y}\). You have to print the value of \(x \cdot y^{-1} \bmod 998244353\), where \(y^{-1}\) is an integer such that \(y \cdot y^{-1} \bmod 998244353 = 1\).
|
The real answer for the first example is \(\frac{1}{4}\): with probability \(\frac{1}{4}\), the towers are built in both towns \(1\) and \(2\), so we can set their signal powers to \(1\). The real answer for the second example is \(\frac{1}{4}\): with probability \(\frac{1}{8}\), the towers are built in towns \(1\), \(2\) and \(3\), so we can set their signal powers to \(1\); with probability \(\frac{1}{8}\), only one tower in town \(2\) is built, and we can set its signal power to \(2\). The real answer for the third example is \(\frac{5}{32}\). Note that even though the previous explanations used equal signal powers for all towers, it is not necessarily so. For example, if \(n = 5\) and the towers are built in towns \(2\), \(4\) and \(5\), you may set the signal power of the tower in town \(2\) to \(2\), and the signal power of the towers in towns \(4\) and \(5\) to \(1\).
|
Input: 2 | Output: 748683265
|
Medium
| 3 | 1,402 | 97 | 376 | 14 |
429 |
A
|
429A
|
A. Xor-tree
| 1,300 |
dfs and similar; trees
|
Iahub is very proud of his recent discovery, propagating trees. Right now, he invented a new tree, called xor-tree. After this new revolutionary discovery, he invented a game for kids which uses xor-trees.The game is played on a tree having n nodes, numbered from 1 to n. Each node i has an initial value initi, which is either 0 or 1. The root of the tree is node 1.One can perform several (possibly, zero) operations on the tree during the game. The only available type of operation is to pick a node x. Right after someone has picked node x, the value of node x flips, the values of sons of x remain the same, the values of sons of sons of x flips, the values of sons of sons of sons of x remain the same and so on.The goal of the game is to get each node i to have value goali, which can also be only 0 or 1. You need to reach the goal of the game by using minimum number of operations.
|
The first line contains an integer n (1 β€ n β€ 105). Each of the next n - 1 lines contains two integers ui and vi (1 β€ ui, vi β€ n; ui β vi) meaning there is an edge between nodes ui and vi. The next line contains n integer numbers, the i-th of them corresponds to initi (initi is either 0 or 1). The following line also contains n integer numbers, the i-th number corresponds to goali (goali is either 0 or 1).
|
In the first line output an integer number cnt, representing the minimal number of operations you perform. Each of the next cnt lines should contain an integer xi, representing that you pick a node xi.
|
Input: 102 13 14 25 16 27 58 69 810 51 0 1 1 0 1 0 1 0 11 0 1 0 0 1 1 1 0 1 | Output: 247
|
Easy
| 2 | 890 | 409 | 201 | 4 |
|
1,951 |
F
|
1951F
|
F. Inversion Composition
| 2,500 |
constructive algorithms; data structures; greedy
|
My Chemical Romance - DisenchantedΰΆYou are given a permutation \(p\) of size \(n\), as well as a non-negative integer \(k\). You need to construct a permutation \(q\) of size \(n\) such that \(\operatorname{inv}(q) + \operatorname{inv}(q \cdot p) = k {}^\dagger {}^\ddagger\), or determine if it is impossible to do so.\({}^\dagger\) For two permutations \(p\) and \(q\) of the same size \(n\), the permutation \(w = q \cdot p\) is such that \(w_i = q_{p_i}\) for all \(1 \le i \le n\).\({}^\ddagger\) For a permutation \(p\) of size \(n\), the function \(\operatorname{inv}(p)\) returns the number of inversions of \(p\), i.e. the number of pairs of indices \(1 \le i < j \le n\) such that \(p_i > p_j\).
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 3 \cdot 10^5, 0 \le k \le n(n - 1)\)) β the size of \(p\) and the target number of inversions.The second line of each test case contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\), \(p_i\)'s are pairwise distinct) β the given permutation \(p\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, print on one line ""YES"" if there exists a permutation \(q\) that satisfies the given condition, or ""NO"" if there is no such permutation.If the answer is ""YES"", on the second line, print \(n\) integers \(q_1, q_2, \ldots, q_n\) that represent such a satisfactory permutation \(q\). If there are multiple such \(q\)'s, print any of them.
|
In the first test case, we have \(q \cdot p = [2, 1, 3]\), \(\operatorname{inv}(q) = 3\), and \(\operatorname{inv}(q \cdot p) = 1\).In the fourth test case, we have \(q \cdot p = [9, 1, 8, 5, 7, 6, 4, 3, 2]\), \(\operatorname{inv}(q) = 24\), and \(\operatorname{inv}(q \cdot p) = 27\).
|
Input: 53 42 3 15 52 3 5 1 46 115 1 2 3 4 69 513 1 4 2 5 6 7 8 91 01 | Output: YES 3 2 1 NO NO YES 1 5 9 8 7 6 4 3 2 YES 1
|
Expert
| 3 | 705 | 615 | 361 | 19 |
919 |
A
|
919A
|
A. Supermarket
| 800 |
brute force; greedy; implementation
|
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that \(a\) yuan for \(b\) kilos (You don't need to care about what ""yuan"" is), the same as \(a/b\) yuan for a kilo.Now imagine you'd like to buy \(m\) kilos of apples. You've asked \(n\) supermarkets and got the prices. Find the minimum cost for those apples.You can assume that there are enough apples in all supermarkets.
|
The first line contains two positive integers \(n\) and \(m\) (\(1 \leq n \leq 5\,000\), \(1 \leq m \leq 100\)), denoting that there are \(n\) supermarkets and you want to buy \(m\) kilos of apples.The following \(n\) lines describe the information of the supermarkets. Each line contains two positive integers \(a, b\) (\(1 \leq a, b \leq 100\)), denoting that in this supermarket, you are supposed to pay \(a\) yuan for \(b\) kilos of apples.
|
The only line, denoting the minimum cost for \(m\) kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed \(10^{-6}\).Formally, let your answer be \(x\), and the jury's answer be \(y\). Your answer is considered correct if \(\frac{|x - y|}{\max{(1, |y|)}} \le 10^{-6}\).
|
In the first sample, you are supposed to buy \(5\) kilos of apples in supermarket \(3\). The cost is \(5/3\) yuan.In the second sample, you are supposed to buy \(1\) kilo of apples in supermarket \(2\). The cost is \(98/99\) yuan.
|
Input: 3 51 23 41 3 | Output: 1.66666667
|
Beginner
| 3 | 517 | 444 | 341 | 9 |
1,504 |
B
|
1504B
|
B. Flip the Bits
| 1,200 |
constructive algorithms; greedy; implementation; math
|
There is a binary string \(a\) of length \(n\). In one operation, you can select any prefix of \(a\) with an equal number of \(0\) and \(1\) symbols. Then all symbols in the prefix are inverted: each \(0\) becomes \(1\) and each \(1\) becomes \(0\).For example, suppose \(a=0111010000\). In the first operation, we can select the prefix of length \(8\) since it has four \(0\)'s and four \(1\)'s: \([01110100]00\to [10001011]00\). In the second operation, we can select the prefix of length \(2\) since it has one \(0\) and one \(1\): \([10]00101100\to [01]00101100\). It is illegal to select the prefix of length \(4\) for the third operation, because it has three \(0\)'s and one \(1\). Can you transform the string \(a\) into the string \(b\) using some finite number of operations (possibly, none)?
|
The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1\le n\le 3\cdot 10^5\)) β the length of the strings \(a\) and \(b\).The following two lines contain strings \(a\) and \(b\) of length \(n\), consisting of symbols \(0\) and \(1\).The sum of \(n\) across all test cases does not exceed \(3\cdot 10^5\).
|
For each test case, output ""YES"" if it is possible to transform \(a\) into \(b\), or ""NO"" if it is impossible. You can print each letter in any case (upper or lower).
|
The first test case is shown in the statement.In the second test case, we transform \(a\) into \(b\) by using zero operations.In the third test case, there is no legal operation, so it is impossible to transform \(a\) into \(b\).In the fourth test case, here is one such transformation: Select the length \(2\) prefix to get \(100101010101\). Select the length \(12\) prefix to get \(011010101010\). Select the length \(8\) prefix to get \(100101011010\). Select the length \(4\) prefix to get \(011001011010\). Select the length \(6\) prefix to get \(100110011010\). In the fifth test case, the only legal operation is to transform \(a\) into \(111000\). From there, the only legal operation is to return to the string we started with, so we cannot transform \(a\) into \(b\).
|
Input: 5 10 0111010000 0100101100 4 0000 0000 3 001 000 12 010101010101 100110011010 6 000111 110100 | Output: YES YES NO YES NO
|
Easy
| 4 | 802 | 414 | 170 | 15 |
1,635 |
F
|
1635F
|
F. Closest Pair
| 2,800 |
data structures; greedy
|
There are \(n\) weighted points on the \(OX\)-axis. The coordinate and the weight of the \(i\)-th point is \(x_i\) and \(w_i\), respectively. All points have distinct coordinates and positive weights. Also, \(x_i < x_{i + 1}\) holds for any \(1 \leq i < n\). The weighted distance between \(i\)-th point and \(j\)-th point is defined as \(|x_i - x_j| \cdot (w_i + w_j)\), where \(|val|\) denotes the absolute value of \(val\).You should answer \(q\) queries, where the \(i\)-th query asks the following: Find the minimum weighted distance among all pairs of distinct points among the points in subarray \([l_i,r_i]\).
|
The first line contains 2 integers \(n\) and \(q\) \((2 \leq n \leq 3 \cdot 10^5; 1 \leq q \leq 3 \cdot 10^5)\) β the number of points and the number of queries.Then, \(n\) lines follows, the \(i\)-th of them contains two integers \(x_i\) and \(w_i\) \((-10^9 \leq x_i \leq 10^9; 1 \leq w_i \leq 10^9)\) β the coordinate and the weight of the \(i\)-th point.It is guaranteed that the points are given in the increasing order of \(x\).Then, \(q\) lines follows, the \(i\)-th of them contains two integers \(l_i\) and \(r_i\) \((1 \leq l_i < r_i \leq n)\) β the given subarray of the \(i\)-th query.
|
For each query output one integer, the minimum weighted distance among all pair of distinct points in the given subarray.
|
For the first query, the minimum weighted distance is between points \(1\) and \(3\), which is equal to \(|x_1 - x_3| \cdot (w_1 + w_3) = |-2 - 1| \cdot (2 + 1) = 9\).For the second query, the minimum weighted distance is between points \(2\) and \(3\), which is equal to \(|x_2 - x_3| \cdot (w_2 + w_3) = |0 - 1| \cdot (10 + 1) = 11\).For the fourth query, the minimum weighted distance is between points \(3\) and \(4\), which is equal to \(|x_3 - x_4| \cdot (w_3 + w_4) = |1 - 9| \cdot (1 + 2) = 24\).
|
Input: 5 5 -2 2 0 10 1 1 9 2 12 7 1 3 2 3 1 5 3 5 2 4 | Output: 9 11 9 24 11
|
Master
| 2 | 617 | 597 | 121 | 16 |
1,218 |
E
|
1218E
|
E. Product Tuples
| 2,500 |
divide and conquer; fft
|
While roaming the mystic areas of Stonefalls, in order to drop legendary loot, an adventurer was given a quest as follows. He was given an array \(A = {a_1,a_2,...,a_N }\) of length \(N\), and a number \(K\).Define array \(B\) as \(B(q, A) = \) { \(q-a_1, q-a_2, ..., q-a_N\) }. Define function \(F\) as \(F(B,K)\) being sum of products of all \(K\)-tuples of elements in array \(B\). For example, if the array \(B\) is \([2,3,4,5]\), and with \(K=3\), sum of products of all 3-tuples is $$$\(F(B, 3) = 2*3*4+2*3*5+3*4*5+2*4*5\)\(He was then given a number Q, number of queries of two types: Type 1: Given \)q\(, \)i\(, and \)d\( calculate \)F(B(q, A), K)\( where we make change to initial array as \)A[i] = d\(. Type 2: Given \)q\(, \)L\(, \)R\(, and \)d\( calculate \)F(B(q, A), K)\( where we make change to initial array as \)A[i] = A[i] + d\( for all \)i\( in range \)[L, R]$$$ inclusive. All changes are temporarily made to initial array, and don't propagate to following queries. Help the adventurer calculate the answer to a quest, and finally get that loot!
|
In the first two lines, numbers \(N\) (\(1 \leq N \leq 2*10^4\)) and \(K\) (\(1 \leq K \leq N\)), the length of initial array \(A\), and tuple size, followed by \(a_1,a_2,a_3,β¦,a_N\) (\(0 \leq a_i \leq 10^9\)) , elements of array \(A\), in the next line. Then follows number \(Q\) (\(Q \leq 10\)), number of queries. In the next \(Q\) lines come queries of the form: 1 q i d, for type 1, 2 q L R d, for type 2, as explained above (\(0 \leq q, d \leq 10^9, 1 \leq i,L,R \leq N\))
|
Print \(Q\) lines, the answers to queries, modulo \(998244353\).
|
In the first query array A = [1, 2, 3, 4, 5], B = [5, 4, 3, 2, 1], sum of products of 2-tuples = 85.In second query array A = [1, 2, 3, 4, 2], B = [5, 4, 3, 2, 4], sum of products of 2-tuples = 127In third query array A = [1, 3, 4, 4, 5], B = [5, 3, 2, 2, 1], sum of products of 2-tuples = 63
|
Input: 5 2 1 2 3 4 5 3 1 6 1 1 1 6 5 2 2 6 2 3 1 | Output: 85 127 63
|
Expert
| 2 | 1,065 | 478 | 64 | 12 |
1,063 |
F
|
1063F
|
F. String Journey
| 3,300 |
data structures; dp; string suffix structures
|
We call a sequence of strings t1, ..., tk a journey of length k, if for each i > 1 ti is a substring of ti - 1 and length of ti is strictly less than length of ti - 1. For example, {ab, b} is a journey, but {ab, c} and {a, a} are not.Define a journey on string s as journey t1, ..., tk, such that all its parts can be nested inside s in such a way that there exists a sequence of strings u1, ..., uk + 1 (each of these strings can be empty) and s = u1 t1 u2 t2... uk tk uk + 1. As an example, {ab, b} is a journey on string abb, but not on bab because the journey strings ti should appear from the left to the right.The length of a journey on a string is the number of strings in it. Determine the maximum possible length of a journey on the given string s.
|
The first line contains a single integer n (1 β€ n β€ 500 000) β the length of string s.The second line contains the string s itself, consisting of n lowercase Latin letters.
|
Print one number β the maximum possible length of string journey on s.
|
In the first sample, the string journey of maximum length is {abcd, bc, c}.In the second sample, one of the suitable journeys is {bb, b}.
|
Input: 7abcdbcc | Output: 3
|
Master
| 3 | 757 | 172 | 70 | 10 |
1,991 |
G
|
1991G
|
G. Grid Reset
| 2,700 |
constructive algorithms; greedy; implementation
|
You are given a grid consisting of \(n\) rows and \(m\) columns, where each cell is initially white. Additionally, you are given an integer \(k\), where \(1 \le k \le \min(n, m)\).You will process \(q\) operations of two types: \(\mathtt{H}\) (horizontal operation) β You choose a \(1 \times k\) rectangle completely within the grid, where all cells in this rectangle are white. Then, all cells in this rectangle are changed to black. \(\mathtt{V}\) (vertical operation) β You choose a \(k \times 1\) rectangle completely within the grid, where all cells in this rectangle are white. Then, all cells in this rectangle are changed to black. After each operation, if any rows or columns become completely black, all cells in these rows and columns are simultaneously reset to white. Specifically, if all cells in the row and column a cell is contained in become black, all cells in both the row and column will be reset to white.Choose the rectangles in a way that you can perform all given operations, or determine that it is impossible.
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains four integers \(n\), \(m\), \(k\), and \(q\) (\(1 \le n, m \le 100\), \(1 \le k \le \min(n, m)\), \(1 \le q \le 1000\)) β the number of rows and columns in the grid, the size of the operation rectangle, and the number of operations, respectively.The second line of each test case contains a string \(s\) of length \(q\), consisting only of characters \(\mathtt{H}\) and \(\mathtt{V}\) β the sequence of operation types.It is guaranteed that the sum of \(q\) over all test cases does not exceed \(1000\).
|
For each test case, output a single integer \(-1\) if it is impossible to perform all the operations.Otherwise, output \(q\) lines. Each line contains two integers \(i\), \(j\) (\(1 \le i \le n\), \(1 \le j \le m\)) β the coordinates of the top-left cell of the operation rectangle.If there are multiple solutions, output any of them.
|
Illustration of example. The first operation is horizontal. The operation rectangle starts at \((1,1)\) and is a \(1 \times 3\) rectangle. After the operation, cells \((1,1)\), \((1,2)\), and \((1,3)\) become black.The second operation is vertical. The operation rectangle starts at \((2,1)\) and is a \(3 \times 1\) rectangle. After the operation, cells \((2,1)\), \((3,1)\), and \((4,1)\) become black. At this point, the first column becomes completely black, so all cells in the first column are reset to white.The third operation is vertical. The operation rectangle starts at \((1,1)\) and is a \(3 \times 1\) rectangle. After the operation, cells \((1,1)\), \((2,1)\), and \((3,1)\) become black.The fourth operation is horizontal. The operation rectangle starts at \((2,3)\) and is a \(1 \times 3\) rectangle. After the operation, cells \((2,3)\), \((2,4)\), and \((2,5)\) become black.The fifth operation is horizontal. The operation rectangle starts at \((3,3)\) and is a \(1 \times 3\) rectangle. After the operation, cells \((3,3)\), \((3,4)\), and \((3,5)\) become black.The sixth operation is vertical. The operation rectangle starts at \((2,2)\) and is a \(3 \times 1\) rectangle. After the operation, cells \((2,2)\), \((3,2)\), and \((4,2)\) become black. At this point, two rows and one column become completely black, so all cells in these rows and the column are reset to white.
|
Input: 14 5 3 6HVVHHV | Output: 1 1 2 1 1 1 2 3 3 3 2 2
|
Master
| 3 | 1,036 | 721 | 334 | 19 |
1,181 |
A
|
1181A
|
A. Chunga-Changa
| 1,000 |
greedy; math
|
Soon after the Chunga-Changa island was discovered, it started to acquire some forms of civilization and even market economy. A new currency arose, colloquially called ""chizhik"". One has to pay in chizhiks to buy a coconut now.Sasha and Masha are about to buy some coconuts which are sold at price \(z\) chizhiks per coconut. Sasha has \(x\) chizhiks, Masha has \(y\) chizhiks. Each girl will buy as many coconuts as she can using only her money. This way each girl will buy an integer non-negative number of coconuts.The girls discussed their plans and found that the total number of coconuts they buy can increase (or decrease) if one of them gives several chizhiks to the other girl. The chizhiks can't be split in parts, so the girls can only exchange with integer number of chizhiks.Consider the following example. Suppose Sasha has \(5\) chizhiks, Masha has \(4\) chizhiks, and the price for one coconut be \(3\) chizhiks. If the girls don't exchange with chizhiks, they will buy \(1 + 1 = 2\) coconuts. However, if, for example, Masha gives Sasha one chizhik, then Sasha will have \(6\) chizhiks, Masha will have \(3\) chizhiks, and the girls will buy \(2 + 1 = 3\) coconuts. It is not that easy to live on the island now, so Sasha and Mash want to exchange with chizhiks in such a way that they will buy the maximum possible number of coconuts. Nobody wants to have a debt, so among all possible ways to buy the maximum possible number of coconuts find such a way that minimizes the number of chizhiks one girl gives to the other (it is not important who will be the person giving the chizhiks).
|
The first line contains three integers \(x\), \(y\) and \(z\) (\(0 \le x, y \le 10^{18}\), \(1 \le z \le 10^{18}\)) β the number of chizhics Sasha has, the number of chizhics Masha has and the price of a coconut.
|
Print two integers: the maximum possible number of coconuts the girls can buy and the minimum number of chizhiks one girl has to give to the other.
|
The first example is described in the statement. In the second example the optimal solution is to dot exchange any chizhiks. The girls will buy \(3 + 4 = 7\) coconuts.
|
Input: 5 4 3 | Output: 3 1
|
Beginner
| 2 | 1,605 | 212 | 147 | 11 |
1,179 |
C
|
1179C
|
C. Serge and Dining Room
| 2,200 |
binary search; data structures; graph matchings; greedy; implementation; math; trees
|
Serge came to the school dining room and discovered that there is a big queue here. There are \(m\) pupils in the queue. He's not sure now if he wants to wait until the queue will clear, so he wants to know which dish he will receive if he does. As Serge is very tired, he asks you to compute it instead of him.Initially there are \(n\) dishes with costs \(a_1, a_2, \ldots, a_n\). As you already know, there are the queue of \(m\) pupils who have \(b_1, \ldots, b_m\) togrogs respectively (pupils are enumerated by queue order, i.e the first pupil in the queue has \(b_1\) togrogs and the last one has \(b_m\) togrogs)Pupils think that the most expensive dish is the most delicious one, so every pupil just buys the most expensive dish for which he has money (every dish has a single copy, so when a pupil has bought it nobody can buy it later), and if a pupil doesn't have money for any dish, he just leaves the queue (so brutal capitalism...)But money isn't a problem at all for Serge, so Serge is buying the most expensive dish if there is at least one remaining.Moreover, Serge's school has a very unstable economic situation and the costs of some dishes or number of togrogs of some pupils can change. More formally, you must process \(q\) queries: change \(a_i\) to \(x\). It means that the price of the \(i\)-th dish becomes \(x\) togrogs. change \(b_i\) to \(x\). It means that the \(i\)-th pupil in the queue has \(x\) togrogs now. Nobody leaves the queue during those queries because a saleswoman is late.After every query, you must tell Serge price of the dish which he will buy if he has waited until the queue is clear, or \(-1\) if there are no dishes at this point, according to rules described above.
|
The first line contains integers \(n\) and \(m\) (\(1 \leq n, m \leq 300\ 000\)) β number of dishes and pupils respectively. The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^{6}\)) β elements of array \(a\). The third line contains \(m\) integers \(b_1, b_2, \ldots, b_{m}\) (\(1 \leq b_i \leq 10^{6}\)) β elements of array \(b\). The fourth line conatins integer \(q\) (\(1 \leq q \leq 300\ 000\)) β number of queries.Each of the following \(q\) lines contains as follows: if a query changes price of some dish, it contains \(1\), and two integers \(i\) and \(x\) (\(1 \leq i \leq n\), \(1 \leq x \leq 10^{6}\)), what means \(a_i\) becomes \(x\). if a query changes number of togrogs of some pupil, it contains \(2\), and two integers \(i\) and \(x\) (\(1 \leq i \leq m\), \(1 \leq x \leq 10^{6}\)), what means \(b_i\) becomes \(x\).
|
For each of \(q\) queries prints the answer as the statement describes, the answer of the \(i\)-th query in the \(i\)-th line (the price of the dish which Serge will buy or \(-1\) if nothing remains)
|
In the first sample after the first query, there is one dish with price \(100\) togrogs and one pupil with one togrog, so Serge will buy the dish with price \(100\) togrogs.In the second sample after the first query, there is one dish with price one togrog and one pupil with \(100\) togrogs, so Serge will get nothing.In the third sample after the first query, nobody can buy the dish with price \(8\), so Serge will take it. After the second query, all dishes will be bought, after the third one the third and fifth pupils will by the first and the second dishes respectively and nobody will by the fourth one.
|
Input: 1 1 1 1 1 1 1 100 | Output: 100
|
Hard
| 7 | 1,717 | 873 | 199 | 11 |
50 |
A
|
50A
|
A. Domino piling
| 800 |
greedy; math
|
You are given a rectangular board of M Γ N squares. Also you are given an unlimited number of standard domino pieces of 2 Γ 1 squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions:1. Each domino completely covers two squares.2. No two dominoes overlap.3. Each domino lies entirely inside the board. It is allowed to touch the edges of the board.Find the maximum number of dominoes, which can be placed under these restrictions.
|
In a single line you are given two integers M and N β board sizes in squares (1 β€ M β€ N β€ 16).
|
Output one number β the maximal number of dominoes, which can be placed.
|
Input: 2 4 | Output: 4
|
Beginner
| 2 | 524 | 94 | 72 | 0 |
|
40 |
A
|
40A
|
A. Find Color
| 1,300 |
constructive algorithms; geometry; implementation; math
|
Not so long ago as a result of combat operations the main Berland place of interest β the magic clock β was damaged. The cannon's balls made several holes in the clock, that's why the residents are concerned about the repair. The magic clock can be represented as an infinite Cartesian plane, where the origin corresponds to the clock center. The clock was painted two colors as is shown in the picture: The picture shows only the central part of the clock. This coloring naturally extends to infinity.The balls can be taken to be points on the plane. Your task is to find the color of the area, damaged by the given ball.All the points located on the border of one of the areas have to be considered painted black.
|
The first and single line contains two integers x and y β the coordinates of the hole made in the clock by the ball. Each of the numbers x and y has an absolute value that does not exceed 1000.
|
Find the required color.All the points between which and the origin of coordinates the distance is integral-value are painted black.
|
Input: -2 1 | Output: white
|
Easy
| 4 | 715 | 193 | 132 | 0 |
|
746 |
B
|
746B
|
B. Decoding
| 900 |
implementation; strings
|
Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it.
|
The first line contains a positive integer n (1 β€ n β€ 2000) β the length of the encoded word.The second line contains the string s of length n consisting of lowercase English letters β the encoding.
|
Print the word that Polycarp encoded.
|
In the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba.
|
Input: 5logva | Output: volga
|
Beginner
| 2 | 715 | 198 | 37 | 7 |
809 |
E
|
809E
|
E. Surprise me!
| 3,100 |
divide and conquer; math; number theory; trees
|
Tired of boring dates, Leha and Noora decided to play a game.Leha found a tree with n vertices numbered from 1 to n. We remind you that tree is an undirected graph without cycles. Each vertex v of a tree has a number av written on it. Quite by accident it turned out that all values written on vertices are distinct and are natural numbers between 1 and n.The game goes in the following way. Noora chooses some vertex u of a tree uniformly at random and passes a move to Leha. Leha, in his turn, chooses (also uniformly at random) some vertex v from remaining vertices of a tree (v β u). As you could guess there are n(n - 1) variants of choosing vertices by players. After that players calculate the value of a function f(u, v) = Ο(auΒ·av) Β· d(u, v) of the chosen vertices where Ο(x) is Euler's totient function and d(x, y) is the shortest distance between vertices x and y in a tree.Soon the game became boring for Noora, so Leha decided to defuse the situation and calculate expected value of function f over all variants of choosing vertices u and v, hoping of at least somehow surprise the girl.Leha asks for your help in calculating this expected value. Let this value be representable in the form of an irreducible fraction . To further surprise Noora, he wants to name her the value . Help Leha!
|
The first line of input contains one integer number n (2 β€ n β€ 2Β·105) β number of vertices in a tree.The second line contains n different numbers a1, a2, ..., an (1 β€ ai β€ n) separated by spaces, denoting the values written on a tree vertices.Each of the next n - 1 lines contains two integer numbers x and y (1 β€ x, y β€ n), describing the next edge of a tree. It is guaranteed that this set of edges describes a tree.
|
In a single line print a number equal to PΒ·Q - 1 modulo 109 + 7.
|
Euler's totient function Ο(n) is the number of such i that 1 β€ i β€ n,and gcd(i, n) = 1, where gcd(x, y) is the greatest common divisor of numbers x and y.There are 6 variants of choosing vertices by Leha and Noora in the first testcase: u = 1, v = 2, f(1, 2) = Ο(a1Β·a2)Β·d(1, 2) = Ο(1Β·2)Β·1 = Ο(2) = 1 u = 2, v = 1, f(2, 1) = f(1, 2) = 1 u = 1, v = 3, f(1, 3) = Ο(a1Β·a3)Β·d(1, 3) = Ο(1Β·3)Β·2 = 2Ο(3) = 4 u = 3, v = 1, f(3, 1) = f(1, 3) = 4 u = 2, v = 3, f(2, 3) = Ο(a2Β·a3)Β·d(2, 3) = Ο(2Β·3)Β·1 = Ο(6) = 2 u = 3, v = 2, f(3, 2) = f(2, 3) = 2 Expected value equals to . The value Leha wants to name Noora is 7Β·3 - 1 = 7Β·333333336 = 333333338 .In the second testcase expected value equals to , so Leha will have to surprise Hoora by number 8Β·1 - 1 = 8 .
|
Input: 31 2 31 22 3 | Output: 333333338
|
Master
| 4 | 1,302 | 418 | 64 | 8 |
501 |
E
|
501E
|
E. Misha and Palindrome Degree
| 2,500 |
binary search; combinatorics; implementation
|
Misha has an array of n integers indexed by integers from 1 to n. Let's define palindrome degree of array a as the number of such index pairs (l, r)(1 β€ l β€ r β€ n), that the elements from the l-th to the r-th one inclusive can be rearranged in such a way that the whole array will be a palindrome. In other words, pair (l, r) should meet the condition that after some rearranging of numbers on positions from l to r, inclusive (it is allowed not to rearrange the numbers at all), for any 1 β€ i β€ n following condition holds: a[i] = a[n - i + 1]. Your task is to find the palindrome degree of Misha's array.
|
The first line contains integer n (1 β€ n β€ 105).The second line contains n positive integers a[i] (1 β€ a[i] β€ n), separated by spaces β the elements of Misha's array.
|
In a single line print the answer to the problem.
|
In the first sample test any possible pair (l, r) meets the condition.In the third sample test following pairs (1, 3), (1, 4), (1, 5), (2, 5) meet the condition.
|
Input: 32 2 2 | Output: 6
|
Expert
| 3 | 606 | 166 | 49 | 5 |
1,178 |
B
|
1178B
|
B. WOW Factor
| 1,300 |
dp; strings
|
Recall that string \(a\) is a subsequence of a string \(b\) if \(a\) can be obtained from \(b\) by deletion of several (possibly zero or all) characters. For example, for the string \(a\)=""wowwo"", the following strings are subsequences: ""wowwo"", ""wowo"", ""oo"", ""wow"", """", and others, but the following are not subsequences: ""owoo"", ""owwwo"", ""ooo"".The wow factor of a string is the number of its subsequences equal to the word ""wow"". Bob wants to write a string that has a large wow factor. However, the ""w"" key on his keyboard is broken, so he types two ""v""s instead. Little did he realise that he may have introduced more ""w""s than he thought. Consider for instance the string ""ww"". Bob would type it as ""vvvv"", but this string actually contains three occurrences of ""w"": ""vvvv"" ""vvvv"" ""vvvv"" For example, the wow factor of the word ""vvvovvv"" equals to four because there are four wows: ""vvvovvv"" ""vvvovvv"" ""vvvovvv"" ""vvvovvv"" Note that the subsequence ""vvvovvv"" does not count towards the wow factor, as the ""v""s have to be consecutive.For a given string \(s\), compute and output its wow factor. Note that it is not guaranteed that it is possible to get \(s\) from another string replacing ""w"" with ""vv"". For example, \(s\) can be equal to ""vov"".
|
The input contains a single non-empty string \(s\), consisting only of characters ""v"" and ""o"". The length of \(s\) is at most \(10^6\).
|
Output a single integer, the wow factor of \(s\).
|
The first example is explained in the legend.
|
Input: vvvovvv | Output: 4
|
Easy
| 2 | 1,306 | 139 | 49 | 11 |
724 |
E
|
724E
|
E. Goods transportation
| 2,900 |
dp; flows; greedy
|
There are n cities located along the one-way road. Cities are numbered from 1 to n in the direction of the road.The i-th city had produced pi units of goods. No more than si units of goods can be sold in the i-th city.For each pair of cities i and j such that 1 β€ i < j β€ n you can no more than once transport no more than c units of goods from the city i to the city j. Note that goods can only be transported from a city with a lesser index to the city with a larger index. You can transport goods between cities in any order.Determine the maximum number of produced goods that can be sold in total in all the cities after a sequence of transportations.
|
The first line of the input contains two integers n and c (1 β€ n β€ 10 000, 0 β€ c β€ 109) β the number of cities and the maximum amount of goods for a single transportation.The second line contains n integers pi (0 β€ pi β€ 109) β the number of units of goods that were produced in each city.The third line of input contains n integers si (0 β€ si β€ 109) β the number of units of goods that can be sold in each city.
|
Print the maximum total number of produced goods that can be sold in all cities after a sequence of transportations.
|
Input: 3 01 2 33 2 1 | Output: 4
|
Master
| 3 | 655 | 411 | 116 | 7 |
|
1,973 |
F
|
1973F
|
F. Maximum GCD Sum Queries
| 3,100 |
bitmasks; brute force; dp; implementation; number theory
|
For \(k\) positive integers \(x_1, x_2, \ldots, x_k\), the value \(\gcd(x_1, x_2, \ldots, x_k)\) is the greatest common divisor of the integers \(x_1, x_2, \ldots, x_k\) β the largest integer \(z\) such that all the integers \(x_1, x_2, \ldots, x_k\) are divisible by \(z\).You are given three arrays \(a_1, a_2, \ldots, a_n\), \(b_1, b_2, \ldots, b_n\) and \(c_1, c_2, \ldots, c_n\) of length \(n\), containing positive integers. You also have a machine that allows you to swap \(a_i\) and \(b_i\) for any \(i\) (\(1 \le i \le n\)). Each swap costs you \(c_i\) coins.Find the maximum possible value of $$$\(\gcd(a_1, a_2, \ldots, a_n) + \gcd(b_1, b_2, \ldots, b_n)\)\( that you can get by paying in total at most \)d\( coins for swapping some elements. The amount of coins you have changes a lot, so find the answer to this question for each of the \)q\( possible values \)d_1, d_2, \ldots, d_q$$$.
|
There are two integers on the first line β the numbers \(n\) and \(q\) (\(1 \leq n \leq 5 \cdot 10^5\), \(1 \leq q \leq 5 \cdot 10^5\)).On the second line, there are \(n\) integers β the numbers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^8\)).On the third line, there are \(n\) integers β the numbers \(b_1, b_2, \ldots, b_n\) (\(1 \leq b_i \leq 10^8\)).On the fourth line, there are \(n\) integers β the numbers \(c_1, c_2, \ldots, c_n\) (\(1 \leq c_i \leq 10^9\)).On the fifth line, there are \(q\) integers β the numbers \(d_1, d_2, \ldots, d_q\) (\(0 \leq d_i \leq 10^{15}\)).
|
Print \(q\) integers β the maximum value you can get for each of the \(q\) possible values \(d\).
|
In the first query of the first example, we are not allowed to do any swaps at all, so the answer is \(\gcd(1, 2, 3) + \gcd(4, 5, 6) = 2\). In the second query, one of the ways to achieve the optimal value is to swap \(a_2\) and \(b_2\), then the answer is \(\gcd(1, 5, 3) + \gcd(4, 2, 6) = 3\).In the second query of the second example, it's optimal to perform swaps on positions \(1\) and \(3\), then the answer is \(\gcd(3, 3, 6, 9, 3) + \gcd(8, 4, 4, 8, 4) = 7\) and we have to pay \(40\) coins in total.
|
Input: 3 41 2 34 5 61 1 10 1 2 3 | Output: 2 3 3 3
|
Master
| 5 | 899 | 585 | 97 | 19 |
2,128 |
C
|
2128C
|
C. Leftmost Below
| 1,200 |
greedy; math
|
Consider an array \(a_1, \ldots, a_n\). Initially, \(a_i = 0\) for every \(i\).You can do operations of the following form. You choose an integer \(x\) greater than \(\min(a)\). Then, \(i\) is defined as the minimum index such that \(a_i < x\). In other words, \(i\) is the unique integer between \(1\) and \(n\) inclusive such that \(a_i < x\) and \(a_j \geq x\) for every \(1 \leq j \leq i-1\). Finally, \(a_i\) is incremented by \(x\). For example, if \(a = [6, 8, 2, 1]\) and you choose \(x = 6\), then \(i\) will be equal to \(3\) (since \(a_1 \geq 6\), \(a_2 \geq 6\), and \(a_3 < 6\)) and \(a\) will become \([6, 8, 8, 1]\).You can do as many operations as you want. Can you reach a target array \(b_1, \ldots, b_n\)?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10\,000\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 200\,000\)).The second line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le 10^9\)).The sum of \(n\) over all test cases does not exceed \(200\,000\).
|
For each test case, print YES if you can reach the target array 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 test case, we can do the following sequence of operations: we choose \(x=2\), \(a\) becomes \([2, 0, 0, 0]\) we choose \(x=2\), \(a\) becomes \([2, 2, 0, 0]\) we choose \(x=3\), \(a\) becomes \([5, 2, 0, 0]\) we choose \(x=4\), \(a\) becomes \([5, 6, 0, 0]\) we choose \(x=1\), \(a\) becomes \([5, 6, 1, 0]\) we choose \(x=1\), \(a\) becomes \([5, 6, 1, 1]\) In the second test case, we can prove there is no way to reach \([3, 1, 2]\).
|
Input: 445 6 1 133 1 2340 60 9021 1 | Output: YES NO NO YES
|
Easy
| 2 | 724 | 434 | 242 | 21 |
1,599 |
G
|
1599G
|
G. Shortest path
| 2,700 |
brute force; geometry; math; shortest paths
|
You are given \(N\) points on an infinite plane with the Cartesian coordinate system on it. \(N-1\) points lay on one line, and one point isn't on that line. You are on point \(K\) at the start, and the goal is to visit every point. You can move between any two points in a straight line, and you can revisit points. What is the minimum length of the path?
|
The first line contains two integers: \(N\) (\(3 \leq N \leq 2*10^5\)) - the number of points, and \(K\) (\(1 \leq K \leq N\)) - the index of the starting point.Each of the next \(N\) lines contain two integers, \(A_i\), \(B_i\) (\(-10^6 \leq A_i, B_i \leq 10^6\)) - coordinates of the \(i-th\) point.
|
The output contains one number - the shortest path to visit all given points starting from point \(K\). The absolute difference between your solution and the main solution shouldn't exceed \(10^-6\);
|
The shortest path consists of these moves: 2 -> 5 5 -> 4 4 -> 1 1 -> 3 There isn't any shorter path possible.
|
Input: 5 2 0 0 -1 1 2 -2 0 1 -2 2 | Output: 7.478709
|
Master
| 4 | 356 | 301 | 199 | 15 |
1,532 |
F
|
1532F
|
F. Prefixes and Suffixes
| 0 |
*special; strings
|
Ivan wants to play a game with you. He picked some string \(s\) of length \(n\) consisting only of lowercase Latin letters. You don't know this string. Ivan has informed you about all its improper prefixes and suffixes (i.e. prefixes and suffixes of lengths from \(1\) to \(n-1\)), but he didn't tell you which strings are prefixes and which are suffixes.Ivan wants you to guess which of the given \(2n-2\) strings are prefixes of the given string and which are suffixes. It may be impossible to guess the string Ivan picked (since multiple strings may give the same set of suffixes and prefixes), but Ivan will accept your answer if there is at least one string that is consistent with it. Let the game begin!
|
The first line of the input contains one integer number \(n\) (\(2 \le n \le 100\)) β the length of the guessed string \(s\).The next \(2n-2\) lines are contain prefixes and suffixes, one per line. Each of them is the string of length from \(1\) to \(n-1\) consisting only of lowercase Latin letters. They can be given in arbitrary order.It is guaranteed that there are exactly \(2\) strings of each length from \(1\) to \(n-1\). It is also guaranteed that these strings are prefixes and suffixes of some existing string of length \(n\).
|
Print one string of length \(2n-2\) β the string consisting only of characters 'P' and 'S'. The number of characters 'P' should be equal to the number of characters 'S'. The \(i\)-th character of this string should be 'P' if the \(i\)-th of the input strings is the prefix and 'S' otherwise.If there are several possible answers, you can print any.
|
The only string which Ivan can guess in the first example is ""ababa"".The only string which Ivan can guess in the second example is ""aaa"". Answers ""SPSP"", ""SSPP"" and ""PSPS"" are also acceptable.In the third example Ivan can guess the string ""ac"" or the string ""ca"". The answer ""SP"" is also acceptable.
|
Input: 5 ba a abab a aba baba ab aba | Output: SPPSPSPS
|
Beginner
| 2 | 710 | 537 | 348 | 15 |
235 |
B
|
235B
|
B. Let's Play Osu!
| 2,000 |
dp; math; probabilities
|
You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as ""O"", bad as ""X"", then the whole play can be encoded as a sequence of n characters ""O"" and ""X"".Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive ""O""s block, add the square of its length (the number of characters ""O"") to the score. For example, if your play can be encoded as ""OOXOOOXXOO"", then there's three maximal consecutive ""O""s block ""OO"", ""OOO"", ""OO"", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.You know that the probability to click the i-th (1 β€ i β€ n) click correctly is pi. In other words, the i-th character in the play sequence has pi probability to be ""O"", 1 - pi to be ""X"". You task is to calculate the expected score for your play.
|
The first line contains an integer n (1 β€ n β€ 105) β the number of clicks. The second line contains n space-separated real numbers p1, p2, ..., pn (0 β€ pi β€ 1).There will be at most six digits after the decimal point in the given pi.
|
Print a single real number β the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
|
For the first example. There are 8 possible outcomes. Each has a probability of 0.125. ""OOO"" β 32 = 9; ""OOX"" β 22 = 4; ""OXO"" β 12 + 12 = 2; ""OXX"" β 12 = 1; ""XOO"" β 22 = 4; ""XOX"" β 12 = 1; ""XXO"" β 12 = 1; ""XXX"" β 0. So the expected score is
|
Input: 30.5 0.5 0.5 | Output: 2.750000000000000
|
Hard
| 3 | 986 | 233 | 159 | 2 |
1,632 |
E1
|
1632E1
|
E1. Distance Tree (easy version)
| 2,400 |
binary search; data structures; dfs and similar; graphs; shortest paths; trees
|
This version of the problem differs from the next one only in the constraint on \(n\).A tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The distance between two vertices is the minimum sum of weights on the path connecting them.You are given a weighted tree with \(n\) vertices, each edge has a weight of \(1\). Denote \(d(v)\) as the distance between vertex \(1\) and vertex \(v\).Let \(f(x)\) be the minimum possible value of \(\max\limits_{1 \leq v \leq n} \ {d(v)}\) if you can temporarily add an edge with weight \(x\) between any two vertices \(a\) and \(b\) \((1 \le a, b \le n)\). Note that after this operation, the graph is no longer a tree.For each integer \(x\) from \(1\) to \(n\), find \(f(x)\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 3000\)).Each of the next \(nβ1\) lines contains two integers \(u\) and \(v\) (\(1 \le u,v \le n\)) indicating that there is an edge between vertices \(u\) and \(v\). It is guaranteed that the given edges form a tree.It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(3000\).
|
For each test case, print \(n\) integers in a single line, \(x\)-th of which is equal to \(f(x)\) for all \(x\) from \(1\) to \(n\).
|
In the first testcase: For \(x = 1\), we can an edge between vertices \(1\) and \(3\), then \(d(1) = 0\) and \(d(2) = d(3) = d(4) = 1\), so \(f(1) = 1\). For \(x \ge 2\), no matter which edge we add, \(d(1) = 0\), \(d(2) = d(4) = 1\) and \(d(3) = 2\), so \(f(x) = 2\).
|
Input: 341 22 31 421 271 21 33 43 53 65 7 | Output: 1 2 2 2 1 1 2 2 3 3 3 3 3
|
Expert
| 6 | 766 | 476 | 132 | 16 |
1,637 |
C
|
1637C
|
C. Andrew and Stones
| 1,200 |
greedy; implementation
|
Andrew has \(n\) piles with stones. The \(i\)-th pile contains \(a_i\) stones. He wants to make his table clean so he decided to put every stone either to the \(1\)-st or the \(n\)-th pile.Andrew can perform the following operation any number of times: choose \(3\) indices \(1 \le i < j < k \le n\), such that the \(j\)-th pile contains at least \(2\) stones, then he takes \(2\) stones from the pile \(j\) and puts one stone into pile \(i\) and one stone into pile \(k\). Tell Andrew what is the minimum number of operations needed to move all the stones to piles \(1\) and \(n\), or determine if it's impossible.
|
The input contains several test cases. The first line contains one integer \(t\) (\(1 \leq t \leq 10\,000\)) β the number of test cases.The first line for each test case contains one integer \(n\) (\(3 \leq n \leq 10^5\)) β the length of the array.The second line contains a sequence of integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) β the array elements.It is guaranteed that the sum of the values \(n\) over all test cases does not exceed \(10^5\).
|
For each test case print the minimum number of operations needed to move stones to piles \(1\) and \(n\), or print \(-1\) if it's impossible.
|
In the first test case, it is optimal to do the following: Select \((i, j, k) = (1, 2, 5)\). The array becomes equal to \([2, 0, 2, 3, 7]\). Select \((i, j, k) = (1, 3, 4)\). The array becomes equal to \([3, 0, 0, 4, 7]\). Twice select \((i, j, k) = (1, 4, 5)\). The array becomes equal to \([5, 0, 0, 0, 9]\). This array satisfy the statement, because every stone is moved to piles \(1\) and \(5\). There are \(4\) operations in total.In the second test case, it's impossible to put all stones into piles with numbers \(1\) and \(3\): At the beginning there's only one possible operation with \((i, j, k) = (1, 2, 3)\). The array becomes equal to \([2, 1, 2]\). Now there is no possible operation and the array doesn't satisfy the statement, so the answer is \(-1\). In the third test case, it's optimal to do the following: Select \((i, j, k) = (1, 2, 3)\). The array becomes equal to \([2, 0, 2]\). This array satisfies the statement, because every stone is moved to piles \(1\) and \(3\). The is \(1\) operation in total.In the fourth test case, it's impossible to do any operation, and the array doesn't satisfy the statement, so the answer is \(-1\).
|
Input: 451 2 2 3 631 3 131 2 143 1 1 2 | Output: 4 -1 1 -1
|
Easy
| 2 | 615 | 465 | 141 | 16 |
1,485 |
F
|
1485F
|
F. Copy or Prefix Sum
| 2,400 |
combinatorics; data structures; dp; sortings
|
You are given an array of integers \(b_1, b_2, \ldots, b_n\).An array \(a_1, a_2, \ldots, a_n\) of integers is hybrid if for each \(i\) (\(1 \leq i \leq n\)) at least one of these conditions is true: \(b_i = a_i\), or \(b_i = \sum_{j=1}^{i} a_j\). Find the number of hybrid arrays \(a_1, a_2, \ldots, a_n\). As the result can be very large, you should print the answer modulo \(10^9 + 7\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(-10^9 \le b_i \le 10^9\)).It is guaranteed that the sum of \(n\) for all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print a single integer: the number of hybrid arrays \(a_1, a_2, \ldots, a_n\) modulo \(10^9 + 7\).
|
In the first test case, the hybrid arrays are \([1, -2, 1]\), \([1, -2, 2]\), \([1, -1, 1]\).In the second test case, the hybrid arrays are \([1, 1, 1, 1]\), \([1, 1, 1, 4]\), \([1, 1, 3, -1]\), \([1, 1, 3, 4]\), \([1, 2, 0, 1]\), \([1, 2, 0, 4]\), \([1, 2, 3, -2]\), \([1, 2, 3, 4]\).In the fourth test case, the only hybrid array is \([0, 0, 0, 1]\).
|
Input: 4 3 1 -1 1 4 1 2 3 4 10 2 -1 1 -2 2 3 -5 0 2 -1 4 0 0 0 1 | Output: 3 8 223 1
|
Expert
| 4 | 389 | 397 | 118 | 14 |
1,442 |
A
|
1442A
|
A. Extreme Subtraction
| 1,800 |
constructive algorithms; dp; greedy
|
You are given an array \(a\) of \(n\) positive integers.You can use the following operation as many times as you like: select any integer \(1 \le k \le n\) and do one of two things: decrement by one \(k\) of the first elements of the array. decrement by one \(k\) of the last elements of the array. For example, if \(n=5\) and \(a=[3,2,2,1,4]\), then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation \(a=[2, 1, 2, 1, 4]\); decrement from the last three elements of the array. After this operation \(a=[3, 2, 1, 0, 3]\); decrement from the first five elements of the array. After this operation \(a=[2, 1, 1, 0, 3]\); Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
|
The first line contains one positive integer \(t\) (\(1 \le t \le 30000\)) β the number of test cases. Then \(t\) test cases follow.Each test case begins with a line containing one integer \(n\) (\(1 \le n \le 30000\)) β the number of elements in the array.The second line of each test case contains \(n\) integers \(a_1 \ldots a_n\) (\(1 \le a_i \le 10^6\)).The sum of \(n\) over all test cases does not exceed \(30000\).
|
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise. The letters in the words YES and NO can be outputed in any case.
|
Input: 4 3 1 2 1 5 11 7 9 6 8 5 1 3 1 3 1 4 5 2 1 10 | Output: YES YES NO YES
|
Medium
| 3 | 861 | 422 | 241 | 14 |
|
2,126 |
C
|
2126C
|
C. I Will Definitely Make It
| 1,100 |
greedy; sortings
|
You are given \(n\) towers, numbered from \(1\) to \(n\). Tower \(i\) has a height of \(h_i\). At time \(0\), you are on the tower with index \(k\), and the current water level is \(1\).Every second, the water level rises by \(1\) unit. At any moment, if the water level becomes strictly greater than the height of the tower you are on, you perish.You have a magical ability: at moment \(x\), you can start teleporting from tower \(i\) to tower \(j\), which will take \(\lvert h_i - h_j \rvert\) seconds. That is, until moment \(x + \lvert h_i - h_j \rvert\), you will be on tower \(i\), and at moment \(x + \lvert h_i - h_j \rvert\), you will move to tower \(j\). You can start a new teleportation at the same moment you just arrived at tower \(j\).For example, if \(n=k=4\), \(h=[4, 4, 4, 2]\), then if you start teleporting from tower \(4\) to tower \(1\) at moment \(0\), the movement will look as follows: Note that if the height of tower \(1\) were \(5\), you would not be able to teleport to it immediately, as you would be submerged at moment \(2\).Your goal is to reach any tower with the maximum height before the water covers you.Determine if this is possible.
|
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. The 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 10^5\)) β the number of towers and the index of the tower you are initially on.The second line contains \(n\) integers \(h_1, h_2, \dots, h_n\) (\(1 \le h_i \le 10^9\)) β the heights of the towers.It is guaranteed that the sum of all \(n\) across all test cases does not exceed \(10^5\).
|
For each test case, output one line: ""YES"", if you can reach the tower with the maximum height before the water covers you, or ""NO"" otherwise.You may output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be accepted as a positive answer.
|
In the first test case, the only possible path is: \(3 \rightarrow 2 \rightarrow 1 \rightarrow 4 \rightarrow 5\).In the second test case, regardless of the order, it will not be possible to reach the tallest tower.In the third test case, one of the possible paths is: \(4 \rightarrow 1\).
|
Input: 55 33 2 1 4 53 11 3 44 44 4 4 26 22 3 6 9 1 24 21 2 5 6 | Output: YES NO YES YES NO
|
Easy
| 2 | 1,171 | 561 | 313 | 21 |
277 |
D
|
277D
|
D. Google Code Jam
| 2,800 |
dp; probabilities
|
Many of you must be familiar with the Google Code Jam round rules. Let us remind you of some key moments that are crucial to solving this problem. During the round, the participants are suggested to solve several problems, each divided into two subproblems: an easy one with small limits (Small input), and a hard one with large limits (Large input). You can submit a solution for Large input only after you've solved the Small input for this problem. There are no other restrictions on the order of solving inputs. In particular, the participant can first solve the Small input, then switch to another problem, and then return to the Large input. Solving each input gives the participant some number of points (usually different for each problem). This takes into account only complete solutions that work correctly on all tests of the input. The participant gets the test result of a Small input right after he submits it, but the test result of a Large input are out only after the round's over. In the final results table the participants are sorted by non-increasing of received points. If the points are equal, the participants are sorted by ascending of time penalty. By the Google Code Jam rules the time penalty is the time when the last correct solution was submitted.Vasya decided to check out a new tactics on another round. As soon as the round begins, the boy quickly read all the problems and accurately evaluated the time it takes to solve them. Specifically, for each one of the n problems Vasya knows five values: Solving the Small input of the i-th problem gives to the participant scoreSmalli points, and solving the Large input gives scoreLargei more points. That is, the maximum number of points you can get for the i-th problem equals scoreSmalli + scoreLargei. Writing the solution for the Small input of the i-th problem takes exactly timeSmalli minutes for Vasya. Improving this code and turning it into the solution of the Large input takes another timeLargei minutes. Vasya's had much practice, so he solves all Small inputs from the first attempt. But it's not so easy with the Large input: there is the probFaili probability that the solution to the Large input will turn out to be wrong at the end of the round. Please keep in mind that these solutions do not affect the participants' points and the time penalty.A round lasts for t minutes. The time for reading problems and submitting solutions can be considered to equal zero. Vasya is allowed to submit a solution exactly at the moment when the round ends.Vasya wants to choose a set of inputs and the order of their solution so as to make the expectation of the total received points maximum possible. If there are multiple ways to do this, he needs to minimize the expectation of the time penalty. Help Vasya to cope with this problem.
|
The first line contains two integers n and t (1 β€ n β€ 1000, 1 β€ t β€ 1560). Then follow n lines, each containing 5 numbers: scoreSmalli, scoreLargei, timeSmalli, timeLargei, probFaili (1 β€ scoreSmalli, scoreLargei β€ 109, 1 β€ timeSmalli, timeLargei β€ 1560, 0 β€ probFaili β€ 1).probFaili are real numbers, given with at most 6 digits after the decimal point. All other numbers in the input are integers.
|
Print two real numbers β the maximum expectation of the total points and the corresponding minimum possible time penalty expectation. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 9.
|
In the first sample one of the optimal orders of solving problems is: The Small input of the third problem. The Small input of the first problem. The Large input of the third problem. The Large input of the first problem.Note that if you solve the Small input of the second problem instead of two inputs of the third one, then total score expectation will be the same but the time penalty expectation will be worse (38).
|
Input: 3 4010 20 15 4 0.54 100 21 1 0.991 4 1 1 0.25 | Output: 24.0 18.875
|
Master
| 2 | 2,822 | 399 | 228 | 2 |
2,111 |
F
|
2111F
|
F. Puzzle
| 2,400 |
brute force; constructive algorithms; greedy; math
|
You have been gifted a puzzle, where each piece of this puzzle is a square with a side length of one. You can glue any picture onto this puzzle, cut it, and obtain an almost ordinary jigsaw puzzle.Your friend is an avid mathematician, so he suggested you consider the following problem. Is it possible to arrange the puzzle pieces in such a way that the following conditions are met: the pieces are aligned parallel to the coordinate axes; the pieces do not overlap each other; all pieces form a single connected component (i.e., there exists a path from each piece to every other piece along the pieces, where each two consecutive pieces share a side); the ratio of the perimeter of this component to the area of this component equals \(\frac{p}{s}\); the number of pieces used does not exceed \(50\,000\). Can you handle it? For this figure, the ratio of the perimeter to the area is \(\frac{11}{9}\)
|
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains two integers \(p\) and \(s\) (\(1 \le p, s \le 50\)).
|
For each test case: if it is impossible to arrange the pieces as described above, output a single integer \(-1\); otherwise, in the first line output a single integer \(k\) (\(1 \le k \le 50\,000\)), and then in \(k\) lines output the coordinates of the pieces: each line should contain two integers \(x_{i}\) and \(y_{i}\) (\(-10^{9} \le x_{i}, y_{i} \le 10^{9}\)). If there are multiple suitable arrangements of the pieces, output any of them.
|
In the first test case of the first test, the figure may look like this: In the second test, the figures look like this: Note that the internal perimeter is also taken into account!
|
Input: 21 131 4 | Output: 20 3 7 3 8 6 4 6 5 3 5 4 4 4 5 4 3 3 4 5 3 5 4 5 7 3 6 4 6 5 5 5 6 4 7 4 8 6 6 6 7 -1
|
Expert
| 4 | 902 | 274 | 445 | 21 |
1,725 |
K
|
1725K
|
K. Kingdom of Criticism
| 2,500 |
data structures; dsu
|
Pak Chanek is visiting a kingdom that earned a nickname ""Kingdom of Criticism"" because of how often its residents criticise each aspect of the kingdom. One aspect that is often criticised is the heights of the buildings. The kingdom has \(N\) buildings. Initially, building \(i\) has a height of \(A_i\) units.At any point in time, the residents can give a new criticism, namely they currently do not like buildings with heights between \(l\) and \(r\) units inclusive for some two integers \(l\) and \(r\). It is known that \(r-l\) is always odd.In \(1\) minute, the kingdom's construction team can increase or decrease the height of any building by \(1\) unit as long as the height still becomes a positive number. Each time they receive the current criticism from the residents, the kingdom's construction team makes it so that there are no buildings with heights between \(l\) and \(r\) units inclusive in the minimum time possible. It can be obtained that there is only one way to do this. Note that the construction team only cares about the current criticism from the residents. All previous criticisms are forgotten.There will be \(Q\) queries that you must solve. Each query is one of the three following possibilities: 1 k w: The kingdom's construction team changes the height of building \(k\) to be \(w\) units (\(1 \leq k \leq N\), \(1 \leq w \leq 10^9\)). 2 k: The kingdom's construction team wants you to find the height of building \(k\) (\(1 \leq k \leq N\)). 3 l r: The residents currently do not like buildings with heights between \(l\) and \(r\) units inclusive (\(2 \leq l \leq r \leq 10^9-1\), \(r-l\) is odd). Note that each change in height still persists to the next queries.
|
The first line contains a single integer \(N\) (\(1 \leq N \leq 4 \cdot 10^5\)) β the number buildings in the kingdom.The second line contains \(N\) integers \(A_1, A_2, \ldots, A_N\) (\(1 \leq A_i \leq 10^9\)) β the initial heights of the buildings.The next line contains a single integer \(Q\) (\(1 \leq Q \leq 4 \cdot 10^5\)) β the number of queries.The \(j\)-th of the next \(Q\) lines contains the \(j\)-th query as described. There is at least one query of type \(2\).
|
For each query of type \(2\), output a line containing an integer representing the height of the building asked.
|
After the \(1\)-st query, the height of each building is \(2, 6, 5, 6, 10\).After the \(3\)-rd query, the height of each building is \(3, 6, 5, 6, 10\).After the \(4\)-th query, the height of each building is \(2, 7, 7, 7, 10\).After the \(5\)-th query, the height of each building is \(2, 7, 7, 7, 10\).After the \(6\)-th query, the height of each building is \(2, 9, 7, 7, 10\).
|
Input: 5 2 6 5 6 2 9 1 5 10 2 5 1 1 3 3 3 6 3 8 9 1 2 9 2 3 2 2 2 4 | Output: 10 7 9 7
|
Expert
| 2 | 1,703 | 474 | 112 | 17 |
549 |
D
|
549D
|
D. Haar Features
| 1,900 |
greedy; implementation
|
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.Let's consider a rectangular image that is represented with a table of size n Γ m. The table elements are integers that specify the brightness of each pixel in the image.A feature also is a rectangular table of size n Γ m. Each cell of a feature is painted black or white.To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.Some examples of the most popular Haar features are given below. Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values ββat any prefix rectangle, multiply it by any integer and add to variable value.You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
|
The first line contains two space-separated integers n and m (1 β€ n, m β€ 100) β the number of rows and columns in the feature.Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to ""W"", if this element of the feature is white and ""B"" if it is black.
|
Print a single number β the minimum number of operations that you need to make to calculate the value of the feature.
|
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 Γ 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations: add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required.
|
Input: 6 8BBBBBBBBBBBBBBBBBBBBBBBBWWWWWWWWWWWWWWWWWWWWWWWW | Output: 2
|
Hard
| 2 | 1,812 | 333 | 117 | 5 |
303 |
C
|
303C
|
C. Minimum Modular
| 2,400 |
brute force; graphs; math; number theory
|
You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the minimum modular m (m > 0), so that for every pair of the remaining integers (ai, aj), the following unequality holds: .
|
The first line contains two integers n and k (1 β€ n β€ 5000, 0 β€ k β€ 4), which we have mentioned above. The second line contains n distinct integers a1, a2, ..., an (0 β€ ai β€ 106).
|
Print a single positive integer β the minimum m.
|
Input: 7 00 2 3 6 7 12 18 | Output: 13
|
Expert
| 4 | 219 | 179 | 48 | 3 |
|
449 |
E
|
449E
|
E. Jzzhu and Squares
| 2,900 |
dp; math; number theory
|
Jzzhu has two integers, n and m. He calls an integer point (x, y) of a plane special if 0 β€ x β€ n and 0 β€ y β€ m. Jzzhu defines a unit square as a square with corners at points (x, y), (x + 1, y), (x + 1, y + 1), (x, y + 1), where x and y are some integers.Let's look at all the squares (their sides not necessarily parallel to the coordinate axes) with corners at the special points. For each such square Jzzhu paints a dot in every unit square that is fully inside it. After that some unit squares can contain several dots. Now Jzzhu wonders, how many dots he has painted on the plane. Find this number modulo 1000000007 (109 + 7).
|
The first line contains a single integer t (1 β€ t β€ 105) β the number of tests.Each of the next t lines contains the description of the test: two integers n and m (1 β€ n, m β€ 106) β the value of variables for the current test.
|
For each test output the total number of dots modulo 1000000007 (109 + 7).
|
Input: 41 32 22 53 4 | Output: 382658
|
Master
| 3 | 632 | 226 | 74 | 4 |
|
1,499 |
E
|
1499E
|
E. Chaotic Merge
| 2,400 |
combinatorics; dp; math; strings
|
You are given two strings \(x\) and \(y\), both consist only of lowercase Latin letters. Let \(|s|\) be the length of string \(s\).Let's call a sequence \(a\) a merging sequence if it consists of exactly \(|x|\) zeros and exactly \(|y|\) ones in some order.A merge \(z\) is produced from a sequence \(a\) by the following rules: if \(a_i=0\), then remove a letter from the beginning of \(x\) and append it to the end of \(z\); if \(a_i=1\), then remove a letter from the beginning of \(y\) and append it to the end of \(z\). Two merging sequences \(a\) and \(b\) are different if there is some position \(i\) such that \(a_i \neq b_i\).Let's call a string \(z\) chaotic if for all \(i\) from \(2\) to \(|z|\) \(z_{i-1} \neq z_i\).Let \(s[l,r]\) for some \(1 \le l \le r \le |s|\) be a substring of consecutive letters of \(s\), starting from position \(l\) and ending at position \(r\) inclusive.Let \(f(l_1, r_1, l_2, r_2)\) be the number of different merging sequences of \(x[l_1,r_1]\) and \(y[l_2,r_2]\) that produce chaotic merges. Note that only non-empty substrings of \(x\) and \(y\) are considered.Calculate \(\sum \limits_{1 \le l_1 \le r_1 \le |x| \\ 1 \le l_2 \le r_2 \le |y|} f(l_1, r_1, l_2, r_2)\). Output the answer modulo \(998\,244\,353\).
|
The first line contains a string \(x\) (\(1 \le |x| \le 1000\)).The second line contains a string \(y\) (\(1 \le |y| \le 1000\)).Both strings consist only of lowercase Latin letters.
|
Print a single integer β the sum of \(f(l_1, r_1, l_2, r_2)\) over \(1 \le l_1 \le r_1 \le |x|\) and \(1 \le l_2 \le r_2 \le |y|\) modulo \(998\,244\,353\).
|
In the first example there are: \(6\) pairs of substrings ""a"" and ""b"", each with valid merging sequences ""01"" and ""10""; \(3\) pairs of substrings ""a"" and ""bb"", each with a valid merging sequence ""101""; \(4\) pairs of substrings ""aa"" and ""b"", each with a valid merging sequence ""010""; \(2\) pairs of substrings ""aa"" and ""bb"", each with valid merging sequences ""0101"" and ""1010""; \(2\) pairs of substrings ""aaa"" and ""b"", each with no valid merging sequences; \(1\) pair of substrings ""aaa"" and ""bb"" with a valid merging sequence ""01010""; Thus, the answer is \(6 \cdot 2 + 3 \cdot 1 + 4 \cdot 1 + 2 \cdot 2 + 2 \cdot 0 + 1 \cdot 1 = 24\).
|
Input: aaa bb | Output: 24
|
Expert
| 4 | 1,257 | 182 | 156 | 14 |
921 |
03
|
92103
|
03. Labyrinth-3
| 3,200 |
See the problem statement here: http://codeforces.com/contest/921/problem/01.
|
Master
| 0 | 77 | 0 | 0 | 9 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.