question_text
stringlengths
2
3.82k
input_outputs
stringlengths
23
941
algo_tags
sequence
Given an array a=[a_1,a_2,...,a_n] of n positive integers, you can do operations of two types on it: Add 1 to every element with an odd index. In other words change the array as follows: a_1 := a_1 +1, a_3 := a_3 + 1, a_5 := a_5+1, .... Add 1 to every element with an even index. In other words change the array as follows: a_2 := a_2 +1, a_4 := a_4 + 1, a_6 := a_6+1, ....Determine if after any number of operations it is possible to make the final array contain only even numbers or only odd numbers. In other words, determine if you can make all elements of the array have the same parity after any number of operations.Note that you can do operations of both types any number of times (even none). Operations of different types can be performed a different number of times.
Input: ['431 2 142 2 2 342 2 2 251000 1 1000 1 1000'] Output:['YES', 'NO', 'YES', 'YES', '']
[ 2, 2, 3 ]
m chairs are arranged in a circle sequentially. The chairs are numbered from 0 to m-1. n people want to sit in these chairs. The i-th of them wants at least a[i] empty chairs both on his right and left side. More formally, if the i-th person sits in the j-th chair, then no one else should sit in the following chairs: (j-a[i]) \bmod m, (j-a[i]+1) \bmod m, ... (j+a[i]-1) \bmod m, (j+a[i]) \bmod m.Decide if it is possible to sit down for all of them, under the given limitations.
Input: ['6', '3 2', '1 1 1', '2 4', '1 1', '2 5', '2 1', '3 8', '1 2 1', '4 12', '1 2 1 3', '4 19', '1 2 1 3', ''] Output:['NO', 'YES', 'NO', 'YES', 'NO', 'YES', '']
[ 2, 3 ]
You are given a grid with n rows and m columns. Rows and columns are numbered from 1 to n, and from 1 to m. The intersection of the a-th row and b-th column is denoted by (a, b). Initially, you are standing in the top left corner (1, 1). Your goal is to reach the bottom right corner (n, m).You can move in four directions from (a, b): up to (a-1, b), down to (a+1, b), left to (a, b-1) or right to (a, b+1).You cannot move in the same direction in two consecutive moves, and you cannot leave the grid. What is the minimum number of moves to reach (n, m)?
Input: ['61 12 11 34 24 610 5'] Output:['0', '1', '-1', '6', '10', '17', '']
[ 3 ]
Consider every tree (connected undirected acyclic graph) with n vertices (n is odd, vertices numbered from 1 to n), and for each 2 <= i <= n the i-th vertex is adjacent to exactly one vertex with a smaller index.For each i (1 <= i <= n) calculate the number of trees for which the i-th vertex will be the centroid. The answer can be huge, output it modulo 998\,244\,353.A vertex is called a centroid if its removal splits the tree into subtrees with at most (n-1)/2 vertices each.
Input: ['3', ''] Output:['1 1 0 ', '']
[ 3 ]
You are given a board with n rows and n columns, numbered from 1 to n. The intersection of the a-th row and b-th column is denoted by (a, b).A half-queen attacks cells in the same row, same column, and on one diagonal. More formally, a half-queen on (a, b) attacks the cell (c, d) if a=c or b=d or a-b=c-d. The blue cells are under attack. What is the minimum number of half-queens that can be placed on that board so as to ensure that each square is attacked by at least one half-queen?Construct an optimal solution.
Input: ['1', ''] Output:['1', '1 1', '']
[ 3 ]
You are given an array a consisting of n positive integers, and an array b, with length n. Initially b_i=0 for each 1 <=q i <=q n.In one move you can choose an integer i (1 <=q i <=q n), and add a_i to b_i or subtract a_i from b_i. What is the minimum number of moves needed to make b increasing (that is, every element is strictly greater than every element before it)?
Input: ['5', '1 2 3 4 5', ''] Output:['4', '']
[ 0, 2, 3 ]
This is an interactive problem.There is a grid of n* m cells. Two treasure chests are buried in two different cells of the grid. Your task is to find both of them. You can make two types of operations: DIG r c: try to find the treasure in the cell (r, c). The interactor will tell you if you found the treasure or not. SCAN r c: scan from the cell (r, c). The result of this operation is the sum of Manhattan distances from the cell (r, c) to the cells where the treasures are hidden. Manhattan distance from a cell (r_1, c_1) to a cell (r_2, c_2) is calculated as |r_1 - r_2| + |c_1 - c_2|. You need to find the treasures in at most 7 operations. This includes both DIG and SCAN operations in total. To solve the test you need to call DIG operation at least once in both of the cells where the treasures are hidden.
Input: ['1', '2 3', '', '1', '', '1', '', '3', '', '0', '', '1', ''] Output:['', '', 'SCAN 1 2', '', 'DIG 1 2', '', 'SCAN 2 2', '', 'DIG 1 1', '', 'DIG 1 3']
[ 0, 3 ]
A revolution has recently happened in Segmentland. The new government is committed to equality, and they hired you to help with land redistribution in the country.Segmentland is a segment of length l kilometers, with the capital in one of its ends. There are n citizens in Segmentland, the home of i-th citizen is located at the point a_i kilometers from the capital. No two homes are located at the same point. Each citizen should receive a segment of positive length with ends at integer distances from the capital that contains her home. The union of these segments should be the whole of Segmentland, and they should not have common points besides their ends. To ensure equality, the difference between the lengths of the longest and the shortest segments should be as small as possible.
Input: ['6 3', '1 3 5', ''] Output:['0 2', '2 4', '4 6', '']
[ 2, 3, 4 ]
Daisy loves playing games with words. Recently, she has been playing the following Deletive Editing word game with Daniel. Daisy picks a word, for example, "DETERMINED". On each game turn, Daniel calls out a letter, for example, 'E', and Daisy removes the first occurrence of this letter from the word, getting "DTERMINED". On the next turn, Daniel calls out a letter again, for example, 'D', and Daisy removes its first occurrence, getting "TERMINED". They continue with 'I', getting "TERMNED", with 'N', getting "TERMED", and with 'D', getting "TERME". Now, if Daniel calls out the letter 'E', Daisy gets "TRME", but there is no way she can get the word "TERM" if they start playing with the word "DETERMINED".Daisy is curious if she can get the final word of her choice, starting from the given initial word, by playing this game for zero or more turns. Your task it help her to figure this out.
Input: ['6DETERMINED TRMEDETERMINED TERMPSEUDOPSEUDOHYPOPARATHYROIDISM PEPADEINSTITUTIONALIZATION DONATIONCONTEST CODESOLUTION SOLUTION'] Output:['YES', 'NO', 'NO', 'YES', 'NO', 'YES', '']
[ 2 ]
You are given three points on a plane. You should choose some segments on the plane that are parallel to coordinate axes, so that all three points become connected. The total length of the chosen segments should be the minimal possible.Two points a and b are considered connected if there is a sequence of points p_0 = a, p_1, ..., p_k = b such that points p_i and p_{i+1} lie on the same segment.
Input: ['1 1', '3 5', '8 6', ''] Output:['3', '1 1 1 5', '1 5 8 5', '8 5 8 6', '']
[ 0 ]
You are given an array a of n non-negative integers, numbered from 1 to n.Let's define the cost of the array a as \displaystyle \min_{i \neq j} a_i | a_j, where | denotes the bitwise OR operation.There are q queries. For each query you are given two integers l and r (l < r). For each query you should find the cost of the subarray a_{l}, a_{l + 1}, ..., a_{r}.
Input: ['256 1 3 2 141 22 32 42 540 2 1 107374182341 22 31 33 4'] Output:['7', '3', '3', '1', '2', '3', '1', '1073741823', '']
[ 0, 2 ]
This is an interactive problem.There is a positive integer 1 <= x <= 10^9 that you have to guess.In one query you can choose two positive integers a \neq b. As an answer to this query you will get \gcd(x + a, x + b), where \gcd(n, m) is the greatest common divisor of the numbers n and m.To guess one hidden number x you are allowed to make no more than 30 queries.
Input: ['2', '', '1', '', '8', '', '', '1', ''] Output:['', '? 1 2', '', '? 12 4', '', '! 4', '? 2000000000 1999999999', '', '! 1000000000', '']
[ 3 ]
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex v (different from root) is the previous to v vertex on the shortest path from the root to the vertex v. Children of the vertex v are all vertices for which v is the parent.You are given a rooted tree with n vertices. The vertex 1 is the root. Initially, all vertices are healthy.Each second you do two operations, the spreading operation and, after that, the injection operation: Spreading: for each vertex v, if at least one child of v is infected, you can spread the disease by infecting at most one other child of v of your choice. Injection: you can choose any healthy vertex and infect it. This process repeats each second until the whole tree is infected. You need to find the minimal number of seconds needed to infect the whole tree.
Input: ['571 1 1 2 2 455 5 1 42133 161 1 1 1 1'] Output:['4', '4', '2', '3', '4', '']
[ 2, 4 ]
You are given an array a of n integers. Initially there is only one copy of the given array.You can do operations of two types: Choose any array and clone it. After that there is one more copy of the chosen array. Swap two elements from any two copies (maybe in the same copy) on any positions. You need to find the minimal number of operations needed to obtain a copy where all elements are equal.
Input: ['61178960 1 3 3 7 02-1000000000 100000000044 3 2 152 5 7 6 371 1 1 1 1 1 1'] Output:['0', '6', '2', '5', '7', '0', '']
[ 2 ]
You are given a positive integer n. You have to find 4 positive integers a, b, c, d such that a + b + c + d = n, and \gcd(a, b) = \operatorname{lcm}(c, d).If there are several possible answers you can output any of them. It is possible to show that the answer always exists.In this problem \gcd(a, b) denotes the greatest common divisor of a and b, and \operatorname{lcm}(c, d) denotes the least common multiple of c and d.
Input: ['5478910'] Output:['1 1 1 1', '2 2 2 1', '2 2 2 2', '2 4 2 1', '3 5 1 1']
[ 3 ]
You won't find this sequence on OEIS.
Input: ['2999', ''] Output:['3000', '']
[ 3 ]
You are given a circular maze such as the ones shown in the figures. Determine if it can be solved, i.e., if there is a path which goes from the center to the outside of the maze which does not touch any wall. The maze is described by n walls. Each wall can be either circular or straight. Circular walls are described by a radius r, the distance from the center, and two angles \theta_1, \theta_2 describing the beginning and the end of the wall in the clockwise direction. Notice that swapping the two angles changes the wall. Straight walls are described by an angle \theta, the direction of the wall, and two radii r_1 < r_2 describing the beginning and the end of the wall. Angles are measured in degrees; the angle 0 corresponds to the upward pointing direction; and angles increase clockwise (hence the east direction corresponds to the angle 90).
Input: ['2', '5', 'C 1 180 90', 'C 5 250 230', 'C 10 150 140', 'C 20 185 180', 'S 1 20 180', '6', 'C 1 180 90', 'C 5 250 230', 'C 10 150 140', 'C 20 185 180', 'S 1 20 180', 'S 5 10 0', ''] Output:['YES', 'NO', '']
[ 0 ]
Today, like every year at SWERC, the n^2 contestants have gathered outside the venue to take a drone photo. Jennifer, the social media manager for the event, has arranged them into an n* n square. Being very good at her job, she knows that the contestant standing on the intersection of the i-th row with the j-th column is a_{i,j} years old. Coincidentally, she notices that no two contestants have the same age, and that everyone is between 1 and n^2 years old.Jennifer is planning to have some contestants hold a banner with the ICPC logo parallel to the ground, so that it is clearly visible in the aerial picture. Here are the steps that she is going to follow in order to take the perfect SWERC drone photo. First of all, Jennifer is going to select four contestants standing on the vertices of an axis-aligned rectangle. Then, she will have the two younger contestants hold one of the poles, while the two older contestants will hold the other pole. Finally, she will unfold the banner, using the poles to support its two ends. Obviously, this can only be done if the two poles are parallel and do not cross, as shown in the pictures below. Being very indecisive, Jennifer would like to try out all possible arrangements for the banner, but she is worried that this may cause the contestants to be late for the competition. How many different ways are there to choose the four contestants holding the poles in order to take a perfect photo? Two choices are considered different if at least one contestant is included in one but not the other.
Input: ['2', '1 3', '4 2', ''] Output:['0', '']
[ 3 ]
The derby between Milan and Inter is happening soon, and you have been chosen as the assistant referee for the match, also known as linesman. Your task is to move along the touch-line, namely the side of the field, always looking very carefully at the match to check for offside positions and other offences.Football is an extremely serious matter in Italy, and thus it is fundamental that you keep very close track of the ball for as much time as possible. This means that you want to maximise the number of kicks which you monitor closely. You are able to monitor closely a kick if, when it happens, you are in the position along the touch-line with minimum distance from the place where the kick happens.Fortunately, expert analysts have been able to accurately predict all the kicks which will occur during the game. That is, you have been given two lists of integers, t_1, ..., t_n and a_1, ..., a_n, indicating that t_i seconds after the beginning of the match the ball will be kicked and you can monitor closely such kick if you are at the position a_i along the touch-line. At the beginning of the game you start at position 0 and the maximum speed at which you can walk along the touch-line is v units per second (i.e., you can change your position by at most v each second). What is the maximum number of kicks that you can monitor closely?
Input: ['3 2', '5 10 15', '7 17 29', ''] Output:['2', '']
[ 3 ]
After a long time living abroad, you have decided to move back to Italy and have to find a place to live, but things are not so easy due to the ongoing global pandemic.Your three friends Fabio, Flavio and Francesco live at the points with coordinates (x_1, y_1), (x_2, y_2) and (x_3, y_3), respectively. Due to the mobility restrictions in response to the pandemic, meetings are limited to 3 persons, so you will only be able to meet 2 of your friends at a time. Moreover, in order to contain the spread of the infection, the authorities have imposed the following additional measure: for each meeting, the sum of the lengths travelled by each of the attendees from their residence place to the place of the meeting must not exceed r.What is the minimum value of r (which can be any nonnegative real number) for which there exists a place of residence that allows you to hold the three possible meetings involving you and two of your friends? Note that the chosen place of residence need not have integer coordinates.
Input: ['0 0', '5 0', '3 3', ''] Output:['5.0686143166', '']
[ 4 ]
On a beach there are n huts in a perfect line, hut 1 being at the left and hut i+1 being 100 meters to the right of hut i, for all 1 <= i <= n - 1. In hut i there are p_i people.There are m ice cream sellers, also aligned in a perfect line with all the huts. The i-th ice cream seller has their shop x_i meters to the right of the first hut. All ice cream shops are at distinct locations, but they may be at the same location as a hut.You want to open a new ice cream shop and you wonder what the best location for your shop is. You can place your ice cream shop anywhere on the beach (not necessarily at an integer distance from the first hut) as long as it is aligned with the huts and the other ice cream shops, even if there is already another ice cream shop or a hut at that location. You know that people would come to your shop only if it is strictly closer to their hut than any other ice cream shop.If every person living in the huts wants to buy exactly one ice cream, what is the maximum number of ice creams that you can sell if you place the shop optimally?
Input: ['3 1', '2 5 6', '169', ''] Output:['7', '']
[ 0 ]
Bethany would like to tile her bathroom. The bathroom has width w centimeters and length l centimeters. If Bethany simply used the basic tiles of size 1 * 1 centimeters, she would use w \cdot l of them. However, she has something different in mind. On the interior of the floor she wants to use the 1 * 1 tiles. She needs exactly (w-2) \cdot (l-2) of these. On the floor boundary she wants to use tiles of size 1 * a for some positive integer a. The tiles can also be rotated by 90 degrees. For which values of a can Bethany tile the bathroom floor as described? Note that a can also be 1.
Input: ['33 512 12314159265 358979323'] Output:['3 1 2 3', '3 1 2 11', '2 1 2', '']
[ 0, 3 ]
SWERC organizers want to hold a gastronomic event.The location of the event is a building with n rooms connected by n-1 corridors (each corridor connects two rooms) so that it is possible to go from any room to any other room.In each room you have to set up the tasting of a typical Italian dish. You can choose from n typical Italian dishes rated from 1 to n depending on how good they are (n is the best possible rating). The n dishes have distinct ratings.You want to assign the n dishes to the n rooms so that the number of pleasing tours is maximal. A pleasing tour is a nonempty sequence of rooms so that: Each room in the sequence is connected to the next one in the sequence by a corridor. The ratings of the dishes in the rooms (in the order given by the sequence) are increasing. If you assign the n dishes optimally, what is the maximum number of pleasing tours?
Input: ['5', '1 2 2 2', ''] Output:['13', '']
[ 2 ]
There are n people, numbered from 1 to n, sitting at a round table. Person i+1 is sitting to the right of person i (with person 1 sitting to the right of person n).You have come up with a better seating arrangement, which is given as a permutation p_1, p_2, ..., p_n. More specifically, you want to change the seats of the people so that at the end person p_{i+1} is sitting to the right of person p_i (with person p_1 sitting to the right of person p_n). Notice that for each seating arrangement there are n permutations that describe it (which can be obtained by rotations).In order to achieve that, you can swap two people sitting at adjacent places; but there is a catch: for all 1 <= x <= n-1 you cannot swap person x and person x+1 (notice that you can swap person n and person 1). What is the minimum number of swaps necessary? It can be proven that any arrangement can be achieved.
Input: ['342 3 1 455 4 3 2 174 1 6 5 3 7 2'] Output:['1', '10', '22', '']
[ 3 ]
A wild basilisk just appeared at your doorstep. You are not entirely sure what a basilisk is and you wonder whether it evolved from your favorite animal, the weasel. How can you find out whether basilisks evolved from weasels? Certainly, a good first step is to sequence both of their DNAs. Then you can try to check whether there is a sequence of possible mutations from the DNA of the weasel to the DNA of the basilisk. Your friend Ron is a talented alchemist and has studied DNA sequences in many of his experiments. He has found out that DNA strings consist of the letters A, B and C and that single mutations can only remove or add substrings at any position in the string (a substring is a contiguous sequence of characters). The substrings that can be removed or added by a mutation are AA, BB, CC, ABAB or BCBC. During a sequence of mutations a DNA string may even become empty.Ron has agreed to sequence the DNA of the weasel and the basilisk for you, but finding out whether there is a sequence of possible mutations that leads from one to the other is too difficult for him, so you have to do it on your own.
Input: ['8ABBCCAAABBBBCCCCAAABABBCBCABCCBA'] Output:['NO', 'NO', 'NO', 'YES', 'YES', 'YES', 'YES', 'NO', '']
[ 2 ]
The map of Europe can be represented by a set of n cities, numbered from 1 through n, which are connected by m bidirectional roads, each of which connects two distinct cities. A trip of length k is a sequence of k+1 cities v_1, v_2, ..., v_{k+1} such that there is a road connecting each consecutive pair v_i, v_{i+1} of cities, for all 1 <= i <= k. A special trip is a trip that does not use the same road twice in a row, i.e., a sequence of k+1 cities v_1, v_2, ..., v_{k+1} such that it forms a trip and v_i \neq v_{i + 2}, for all 1 <= i <= k - 1.Given an integer k, compute the number of distinct special trips of length k which begin and end in the same city. Since the answer might be large, give the answer modulo 998\,244\,353.
Input: ['4 5 2', '4 1', '2 3', '3 1', '4 3', '2 4', ''] Output:['0', '']
[ 3 ]
Vittorio has three favorite toys: a teddy bear, an owl, and a raccoon. Each of them has a name. Vittorio takes several sheets of paper and writes a letter on each side of every sheet so that it is possible to spell any of the three names by arranging some of the sheets in a row (sheets can be reordered and flipped as needed). The three names do not have to be spelled at the same time, it is sufficient that it is possible to spell each of them using all the available sheets (and the same sheet can be used to spell different names).Find the minimum number of sheets required. In addition, produce a list of sheets with minimum cardinality which can be used to spell the three names (if there are multiple answers, print any).
Input: ['AA', 'GA', 'MA', ''] Output:['2', 'AG', 'AM', '']
[ 2 ]
Gianni, SWERC's chief judge, received a huge amount of high quality problems from the judges and now he has to choose a problem set for SWERC.He received n problems and he assigned a beauty score and a difficulty to each of them. The i-th problem has beauty score equal to b_i and difficulty equal to d_i. The beauty and the difficulty are integers between 1 and 10. If there are no problems with a certain difficulty (the possible difficulties are 1,2,...,10) then Gianni will ask for more problems to the judges.Otherwise, for each difficulty between 1 and 10, he will put in the problem set one of the most beautiful problems with such difficulty (so the problem set will contain exactly 10 problems with distinct difficulties). You shall compute the total beauty of the problem set, that is the sum of the beauty scores of the problems chosen by Gianni.
Input: ['238 49 36 7123 1010 110 210 310 43 1010 510 610 710 810 91 10'] Output:['MOREPROBLEMS', '93', '']
[ 0 ]
There are n+1 teleporters on a straight line, located in points 0, a_1, a_2, a_3, ..., a_n. It's possible to teleport from point x to point y if there are teleporters in both of those points, and it costs (x-y)^2 energy.You want to install some additional teleporters so that it is possible to get from the point 0 to the point a_n (possibly through some other teleporters) spending no more than m energy in total. Each teleporter you install must be located in an integer point.What is the minimum number of teleporters you have to install?
Input: ['2', '1 5', '7', ''] Output:['2', '']
[ 2, 4 ]
You are given a matrix a, consisting of 3 rows and n columns. Each cell of the matrix is either free or taken.A free cell y is reachable from a free cell x if at least one of these conditions hold: x and y share a side; there exists a free cell z such that z is reachable from x and y is reachable from z. A connected component is a set of free cells of the matrix such that all cells in it are reachable from one another, but adding any other free cell to the set violates this rule.You are asked q queries about the matrix. Each query is the following: l r β€” count the number of connected components of the matrix, consisting of columns from l to r of the matrix a, inclusive. Print the answers to all queries.
Input: ['12', '100101011101', '110110010110', '010001011101', '8', '1 12', '1 1', '1 2', '9 9', '8 11', '9 12', '11 12', '4 6', ''] Output:['7', '1', '1', '2', '1', '3', '3', '3', '']
[ 0, 3 ]
You are given two arrays: an array a consisting of n zeros and an array b consisting of n integers.You can apply the following operation to the array a an arbitrary number of times: choose some subsegment of a of length k and add the arithmetic progression 1, 2, ..., k to this subsegment β€” i. e. add 1 to the first element of the subsegment, 2 to the second element, and so on. The chosen subsegment should be inside the borders of the array a (i.e., if the left border of the chosen subsegment is l, then the condition 1 <= l <= l + k - 1 <= n should be satisfied). Note that the progression added is always 1, 2, ..., k but not the k, k - 1, ..., 1 or anything else (i.e., the leftmost element of the subsegment always increases by 1, the second element always increases by 2 and so on).Your task is to find the minimum possible number of operations required to satisfy the condition a_i >= b_i for each i from 1 to n. Note that the condition a_i >= b_i should be satisfied for all elements at once.
Input: ['3 3', '5 4 6', ''] Output:['5', '']
[ 2 ]
There are n trees in a park, numbered from 1 to n. The initial height of the i-th tree is h_i.You want to water these trees, so they all grow to the same height.The watering process goes as follows. You start watering trees at day 1. During the j-th day you can: Choose a tree and water it. If the day is odd (e.g. 1, 3, 5, 7, ...), then the height of the tree increases by 1. If the day is even (e.g. 2, 4, 6, 8, ...), then the height of the tree increases by 2. Or skip a day without watering any tree. Note that you can't water more than one tree in a day. Your task is to determine the minimum number of days required to water the trees so they grow to the same height.You have to answer t independent test cases.
Input: ['331 2 454 4 3 5 572 5 4 8 3 7 4'] Output:['4', '3', '16', '']
[ 2, 3, 4 ]
Suppose you have an integer v. In one operation, you can: either set v = (v + 1) \bmod 32768 or set v = (2 \cdot v) \bmod 32768. You are given n integers a_1, a_2, ..., a_n. What is the minimum number of operations you need to make each a_i equal to 0?
Input: ['4', '19 32764 10240 49', ''] Output:['14 4 4 15 ']
[ 0, 2 ]
You are given two arrays of length n: a_1, a_2, ..., a_n and b_1, b_2, ..., b_n.You can perform the following operation any number of times: Choose integer index i (1 <= i <= n); Swap a_i and b_i. What is the minimum possible sum |a_1 - a_2| + |a_2 - a_3| + ... + |a_{n-1} - a_n| + |b_1 - b_2| + |b_2 - b_3| + ... + |b_{n-1} - b_n| (in other words, \sum\limits_{i=1}^{n - 1}{<=ft(|a_i - a_{i+1}| + |b_i - b_{i+1}|\right)}) you can achieve after performing several (possibly, zero) operations?
Input: ['343 3 10 1010 10 3 351 2 3 4 56 7 8 9 10672 101 108 108 111 4410 87 111 114 108 100'] Output:['0', '8', '218', '']
[ 2, 3 ]
This is the hard version of Problem F. The only difference between the easy version and the hard version is the constraints.We will call a non-empty string balanced if it contains the same number of plus and minus signs. For example: strings "+--+" and "++-+--" are balanced, and strings "+--", "--" and "" are not balanced.We will call a string promising if the string can be made balanced by several (possibly zero) uses of the following operation: replace two adjacent minus signs with one plus sign. In particular, every balanced string is promising. However, the converse is not true: not every promising string is balanced.For example, the string "-+---" is promising, because you can replace two adjacent minuses with plus and get a balanced string "-++-", or get another balanced string "-+-+".How many non-empty substrings of the given string s are promising? Each non-empty promising substring must be counted in the answer as many times as it occurs in string s.Recall that a substring is a sequence of consecutive characters of the string. For example, for string "+-+" its substring are: "+-", "-+", "+", "+-+" (the string is a substring of itself) and some others. But the following strings are not its substring: "--", "++", "-++".
Input: ['53+-+5-+---4----7--+---+6+++---'] Output:['2', '4', '2', '7', '4', '']
[ 3 ]
This is the easy version of Problem F. The only difference between the easy version and the hard version is the constraints.We will call a non-empty string balanced if it contains the same number of plus and minus signs. For example: strings "+--+" and "++-+--" are balanced, and strings "+--", "--" and "" are not balanced.We will call a string promising if the string can be made balanced by several (possibly zero) uses of the following operation: replace two adjacent minus signs with one plus sign. In particular, every balanced string is promising. However, the converse is not true: not every promising string is balanced.For example, the string "-+---" is promising, because you can replace two adjacent minuses with plus and get a balanced string "-++-", or get another balanced string "-+-+".How many non-empty substrings of the given string s are promising? Each non-empty promising substring must be counted in the answer as many times as it occurs in string s.Recall that a substring is a sequence of consecutive characters of the string. For example, for string "+-+" its substring are: "+-", "-+", "+", "+-+" (the string is a substring of itself) and some others. But the following strings are not its substring: "--", "++", "-++".
Input: ['53+-+5-+---4----7--+---+6+++---'] Output:['2', '4', '2', '7', '4', '']
[ 0, 3 ]
You are given a binary matrix A of size n * n. Rows are numbered from top to bottom from 1 to n, columns are numbered from left to right from 1 to n. The element located at the intersection of row i and column j is called A_{ij}. Consider a set of 4 operations: Cyclically shift all rows up. The row with index i will be written in place of the row i-1 (2 <= i <= n), the row with index 1 will be written in place of the row n. Cyclically shift all rows down. The row with index i will be written in place of the row i+1 (1 <= i <= n - 1), the row with index n will be written in place of the row 1. Cyclically shift all columns to the left. The column with index j will be written in place of the column j-1 (2 <= j <= n), the column with index 1 will be written in place of the column n. Cyclically shift all columns to the right. The column with index j will be written in place of the column j+1 (1 <= j <= n - 1), the column with index n will be written in place of the column 1. The 3 * 3 matrix is shown on the left before the 3-rd operation is applied to it, on the right β€” after. You can perform an arbitrary (possibly zero) number of operations on the matrix; the operations can be performed in any order.After that, you can perform an arbitrary (possibly zero) number of new xor-operations: Select any element A_{ij} and assign it with new value A_{ij} \oplus 1. In other words, the value of (A_{ij} + 1) \bmod 2 will have to be written into element A_{ij}. Each application of this xor-operation costs one burl. Note that the 4 shift operations β€” are free. These 4 operations can only be performed before xor-operations are performed.Output the minimum number of burles you would have to pay to make the A matrix unitary. A unitary matrix is a matrix with ones on the main diagonal and the rest of its elements are zeros (that is, A_{ij} = 1 if i = j and A_{ij} = 0 otherwise).
Input: ['43010011100500010000011000001000001002101041111101111111111'] Output:['1', '0', '2', '11', '']
[ 0, 2 ]
You are given an array a consisting of n integers. For each i (1 <= i <= n) the following inequality is true: -2 <= a_i <= 2.You can remove any number (possibly 0) of elements from the beginning of the array and any number (possibly 0) of elements from the end of the array. You are allowed to delete the whole array.You need to answer the question: how many elements should be removed from the beginning of the array, and how many elements should be removed from the end of the array, so that the result will be an array whose product (multiplication) of elements is maximal. If there is more than one way to get an array with the maximum product of elements on it, you are allowed to output any of them. The product of elements of an empty array (array of length 0) should be assumed to be 1.
Input: ['541 2 -1 231 1 -252 0 -2 2 -13-2 -1 -13-1 -2 -2'] Output:['0 2', '3 0', '2 0', '0 1', '1 0', '']
[ 0, 3 ]
A string a=a_1a_2... a_n is called even if it consists of a concatenation (joining) of strings of length 2 consisting of the same characters. In other words, a string a is even if two conditions are satisfied at the same time: its length n is even; for all odd i (1 <= i <= n - 1), a_i = a_{i+1} is satisfied. For example, the following strings are even: "" (empty string), "tt", "aabb", "oooo", and "ttrrrroouuuuuuuukk". The following strings are not even: "aaa", "abab" and "abba".Given a string s consisting of lowercase Latin letters. Find the minimum number of characters to remove from the string s to make it even. The deleted characters do not have to be consecutive.
Input: ['6aabbdabdccczyxaaababbbaabbccoaoaaaoobmefbmuyw'] Output:['3', '3', '2', '0', '2', '7', '']
[ 2 ]
Not so long ago, Vlad had a birthday, for which he was presented with a package of candies. There were n types of candies, there are a_i candies of the type i (1 <= i <= n).Vlad decided to eat exactly one candy every time, choosing any of the candies of a type that is currently the most frequent (if there are several such types, he can choose any of them). To get the maximum pleasure from eating, Vlad does not want to eat two candies of the same type in a row.Help him figure out if he can eat all the candies without eating two identical candies in a row.
Input: ['622 31251 6 2 4 342 2 2 131 1000000000 99999999911'] Output:['YES', 'NO', 'NO', 'YES', 'YES', 'YES', '']
[ 3 ]
Vasya decided to go to the grocery store. He found in his wallet a coins of 1 burle and b coins of 2 burles. He does not yet know the total cost of all goods, so help him find out s (s > 0): the minimum positive integer amount of money he cannot pay without change or pay at all using only his coins.For example, if a=1 and b=1 (he has one 1-burle coin and one 2-burle coin), then: he can pay 1 burle without change, paying with one 1-burle coin, he can pay 2 burle without change, paying with one 2-burle coin, he can pay 3 burle without change by paying with one 1-burle coin and one 2-burle coin, he cannot pay 4 burle without change (moreover, he cannot pay this amount at all). So for a=1 and b=1 the answer is s=4.
Input: ['51 14 00 20 02314 2374'] Output:['4', '5', '1', '1', '7063', '']
[ 2, 3 ]
There is an undirected, connected graph with n vertices and m weighted edges. A walk from vertex u to vertex v is defined as a sequence of vertices p_1,p_2,...,p_k (which are not necessarily distinct) starting with u and ending with v, such that p_i and p_{i+1} are connected by an edge for 1 <=q i < k.We define the length of a walk as follows: take the ordered sequence of edges and write down the weights on each of them in an array. Now, write down the bitwise AND of every nonempty prefix of this array. The length of the walk is the MEX of all these values.More formally, let us have [w_1,w_2,...,w_{k-1}] where w_i is the weight of the edge between p_i and p_{i+1}. Then the length of the walk is given by \mathrm{MEX}(\{w_1,\,w_1\& w_2,\,...,\,w_1\& w_2\& ...\& w_{k-1}\}), where \& denotes the bitwise AND operation.Now you must process q queries of the form u v. For each query, find the minimum possible length of a walk from u to v.The MEX (minimum excluded) of a set is the smallest non-negative integer that does not belong to the set. For instance: The MEX of \{2,1\} is 0, because 0 does not belong to the set. The MEX of \{3,1,0\} is 2, because 0 and 1 belong to the set, but 2 does not. The MEX of \{0,3,1,2\} is 4 because 0, 1, 2 and 3 belong to the set, but 4 does not.
Input: ['6 7', '1 2 1', '2 3 3', '3 1 5', '4 5 2', '5 6 4', '6 4 6', '3 4 1', '3', '1 5', '1 2', '5 3', ''] Output:['2', '0', '1', '']
[ 0 ]
Suppose you had an array A of n elements, each of which is 0 or 1.Let us define a function f(k,A) which returns another array B, the result of sorting the first k elements of A in non-decreasing order. For example, f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]. Note that the first 4 elements were sorted.Now consider the arrays B_1, B_2,..., B_n generated by f(1,A), f(2,A),...,f(n,A). Let C be the array obtained by taking the element-wise sum of B_1, B_2,..., B_n.For example, let A=[0,1,0,1]. Then we have B_1=[0,1,0,1], B_2=[0,1,0,1], B_3=[0,0,1,1], B_4=[0,0,1,1]. Then C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4].You are given C. Determine a binary array A that would give C when processed as above. It is guaranteed that an array A exists for given C in the input.
Input: ['542 4 2 470 3 4 2 3 2 730 0 040 0 0 431 2 3'] Output:['1 1 0 1 ', '0 1 1 0 0 0 1 ', '0 0 0 ', '0 0 0 1 ', '1 0 1 ', '']
[ 2, 3 ]
You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at 0. There are n unconquered kingdoms at positions 0<x_1<x_2<...<x_n. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be c_1) to any other conquered kingdom (let its position be c_2) at a cost of a\cdot |c_1-c_2|. From the current capital (let its current position be c_1) you can conquer an unconquered kingdom (let its position be c_2) at a cost of b\cdot |c_1-c_2|. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at 0 or one of x_1,x_2,...,x_n. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
Input: ['45 2 73 5 12 13 215 6 31 5 6 21 302 9 310 1511 27182 3141516 18 33 98 874 989 4848 20458 34365 38117 72030'] Output:['173', '171', '75', '3298918744', '']
[ 0, 2, 3, 4 ]
You are given a binary string of length n. You have exactly k moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped (0 becomes 1, 1 becomes 0). You need to output the lexicographically largest string that you can get after using all k moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string a is lexicographically larger than a binary string b of the same length, if and only if the following holds: in the first position where a and b differ, the string a contains a 1, and the string b contains a 0.
Input: ['66 31000016 41000116 00000006 11110016 111011006 12001110'] Output:['111110', '1 0 0 2 0 0 ', '111110', '0 1 1 1 0 1 ', '000000', '0 0 0 0 0 0 ', '100110', '1 0 0 0 0 0 ', '111111', '1 2 1 3 0 4 ', '111110', '1 1 4 2 0 4']
[ 2 ]
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of n matches.In the end, it turned out Team Red won r times and Team Blue won b times. Team Blue was less skilled than Team Red, so b was strictly less than r.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length n where the i-th character denotes who won the i-th match β€” it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won 3 times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
Input: ['37 4 36 5 119 13 6'] Output:['RBRBRBR', 'RRRBRR', 'RRBRRBRRBRRBRRBRRBR', '']
[ 2, 3 ]
The cuteness of a binary string is the number of \texttt{1}s divided by the length of the string. For example, the cuteness of \texttt{01101} is \frac{3}{5}.Juju has a binary string s of length n. She wants to choose some non-intersecting subsegments of s such that their concatenation has length m and it has the same cuteness as the string s. More specifically, she wants to find two arrays l and r of equal length k such that 1 <=q l_1 <=q r_1 < l_2 <=q r_2 < ... < l_k <=q r_k <=q n, and also: \sum\limits_{i=1}^k (r_i - l_i + 1) = m; The cuteness of s[l_1,r_1]+s[l_2,r_2]+...+s[l_k,r_k] is equal to the cuteness of s, where s[x, y] denotes the subsegment s_x s_{x+1} ... s_y, and + denotes string concatenation. Juju does not like splitting the string into many parts, so she also wants to minimize the value of k. Find the minimum value of k such that there exist l and r that satisfy the constraints above or determine that it is impossible to find such l and r for any k.
Input: ['44 200118 6110000114 301015 511111'] Output:['1', '2 3', '2', '2 3', '5 8', '-1', '1', '1 5', '']
[ 0, 2, 3 ]
Marin feels exhausted after a long day of cosplay, so Gojou invites her to play a game!Marin and Gojou take turns to place one of their tokens on an n * n grid with Marin starting first. There are some restrictions and allowances on where to place tokens: Apart from the first move, the token placed by a player must be more than Manhattan distance k away from the previous token placed on the matrix. In other words, if a player places a token at (x_1, y_1), then the token placed by the other player in the next move must be in a cell (x_2, y_2) satisfying |x_2 - x_1| + |y_2 - y_1| > k. Apart from the previous restriction, a token can be placed anywhere on the matrix, including cells where tokens were previously placed by any player. Whenever a player places a token on cell (x, y), that player gets v_{x,\ y} points. All values of v on the grid are distinct. You still get points from a cell even if tokens were already placed onto the cell. The game finishes when each player makes 10^{100} moves.Marin and Gojou will play n^2 games. For each cell of the grid, there will be exactly one game where Marin places a token on that cell on her first move. Please answer for each game, if Marin and Gojou play optimally (after Marin's first move), who will have more points at the end? Or will the game end in a draw (both players have the same points at the end)?
Input: ['3 1', '1 2 4', '6 8 3', '9 5 7', ''] Output:['GGG', 'MGG', 'MGG', '']
[ 3 ]
This is the hard version of the problem. The difference in the constraints between both versions are colored below in red. You can make hacks only if all versions of the problem are solved.Marin and Gojou are playing hide-and-seek with an array.Gojou initially perform the following steps: First, Gojou chooses 2 integers l and r such that l <=q r. Then, Gojou will make an array a of length r-l+1 which is a permutation of the array [l,l+1,...,r]. Finally, Gojou chooses a secret integer x and sets a_i to a_i \oplus x for all i (where \oplus denotes the bitwise XOR operation). Marin is then given the values of l,r and the final array a. She needs to find the secret integer x to win. Can you help her?Note that there may be multiple possible x that Gojou could have chosen. Marin can find any possible x that could have resulted in the final value of a.
Input: ['34 73 2 1 04 74 7 6 51 30 2 1'] Output:['4', '0', '3', '']
[ 0, 3 ]
This is the easy version of the problem. The difference in the constraints between both versions is colored below in red. You can make hacks only if all versions of the problem are solved.Marin and Gojou are playing hide-and-seek with an array.Gojou initially performs the following steps: First, Gojou chooses 2 integers l and r such that l <=q r. Then, Gojou makes an array a of length r-l+1 which is a permutation of the array [l,l+1,...,r]. Finally, Gojou chooses a secret integer x and sets a_i to a_i \oplus x for all i (where \oplus denotes the bitwise XOR operation). Marin is then given the values of l,r and the final array a. She needs to find the secret integer x to win. Can you help her?Note that there may be multiple possible x that Gojou could have chosen. Marin can find any possible x that could have resulted in the final value of a.
Input: ['30 33 2 1 00 34 7 6 50 21 2 3'] Output:['0', '4', '3', '']
[ 3 ]
Shinju loves permutations very much! Today, she has borrowed a permutation p from Juju to play with.The i-th cyclic shift of a permutation p is a transformation on the permutation such that p = [p_1, p_2, ..., p_n] will now become p = [p_{n-i+1}, ..., p_n, p_1,p_2, ..., p_{n-i}].Let's define the power of permutation p as the number of distinct elements in the prefix maximums array b of the permutation. The prefix maximums array b is the array of length n such that b_i = \max(p_1, p_2, ..., p_i). For example, the power of [1, 2, 5, 4, 6, 3] is 4 since b=[1,2,5,5,6,6] and there are 4 distinct elements in b.Unfortunately, Shinju has lost the permutation p! The only information she remembers is an array c, where c_i is the power of the (i-1)-th cyclic shift of the permutation p. She's also not confident that she remembers it correctly, so she wants to know if her memory is good enough.Given the array c, determine if there exists a permutation p that is consistent with c. You do not have to construct the permutation p.A permutation 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).
Input: ['61121 222 261 2 4 6 3 562 3 1 2 3 433 2 1'] Output:['YES', 'YES', 'NO', 'NO', 'YES', 'NO', '']
[ 3 ]
Marin wants you to count number of permutations that are beautiful. A beautiful permutation of length n is a permutation that has the following property: \gcd (1 \cdot p_1, \, 2 \cdot p_2, \, ..., \, n \cdot p_n) > 1, where \gcd is the greatest common divisor.A permutation 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).
Input: ['71234561000'] Output:['0', '1', '0', '4', '0', '36', '665702330', '']
[ 3 ]
Today, Marin is at a cosplay exhibition and is preparing for a group photoshoot!For the group picture, the cosplayers form a horizontal line. A group picture is considered beautiful if for every contiguous segment of at least 2 cosplayers, the number of males does not exceed the number of females (obviously).Currently, the line has n cosplayers which can be described by a binary string s. The i-th cosplayer is male if s_i = 0 and female if s_i = 1. To ensure that the line is beautiful, you can invite some additional cosplayers (possibly zero) to join the line at any position. You can't remove any cosplayer from the line.Marin wants to know the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful. She can't do this on her own, so she's asking you for help. Can you help her?
Input: ['930003001301030113100310131103111191010110000100000101'] Output:['4', '2', '1', '0', '2', '0', '0', '0', '17', '']
[ 3 ]
In this problem, we will consider complete undirected graphs consisting of n vertices with weighted edges. The weight of each edge is an integer from 1 to k.An undirected graph is considered beautiful if the sum of weights of all edges incident to vertex 1 is equal to the weight of MST in the graph. MST is the minimum spanning tree β€” a tree consisting of n-1 edges of the graph, which connects all n vertices and has the minimum sum of weights among all such trees; the weight of MST is the sum of weights of all edges in it.Calculate the number of complete beautiful graphs having exactly n vertices and the weights of edges from 1 to k. Since the answer might be large, print it modulo 998244353.
Input: ['3 2', ''] Output:['5', '']
[ 3 ]
Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has C coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with C coins.There are n types of units. Every unit type has three parameters: c_i β€” the cost of recruiting one unit of the i-th type; d_i β€” the damage that one unit of the i-th type deals in a second; h_i β€” the amount of health of one unit of the i-th type. Monocarp has to face m monsters. Every monster has two parameters: D_j β€” the damage that the j-th monster deals in a second; H_j β€” the amount of health the j-th monster has. Monocarp has to fight only the j-th monster during the j-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than C, then report that it's impossible to kill that monster.
Input: ['3 10', '3 4 6', '5 5 5', '10 3 4', '3', '8 3', '5 4', '10 15', ''] Output:['5 3 -1 ', '']
[ 0, 2, 3, 4 ]
You are given a bracket sequence consisting of n characters '(' and/or )'. You perform several operations with it.During one operation, you choose the shortest prefix of this string (some amount of first characters of the string) that is good and remove it from the string.The prefix is considered good if one of the following two conditions is satisfied: this prefix is a regular bracket sequence; this prefix is a palindrome of length at least two. A bracket sequence is called regular if it is possible to obtain a correct arithmetic expression by inserting characters '+' and '1' into this sequence. For example, sequences (())(), () and (()(())) are regular, while )(, (() and (()))( are not.The bracket sequence is called palindrome if it reads the same back and forth. For example, the bracket sequences )), (( and )(() are palindromes, while bracket sequences (), )( and ))( are not palindromes.You stop performing the operations when it's not possible to find a good prefix. Your task is to find the number of operations you will perform on the given string and the number of remaining characters in the string.You have to answer t independent test cases.
Input: ['52()3())4((((5)((()6)((()('] Output:['1 0', '1 1', '2 0', '1 0', '1 1', '']
[ 2 ]
You are given four integers n, B, x and y. You should build a sequence a_0, a_1, a_2, ..., a_n where a_0 = 0 and for each i >= 1 you can choose: either a_i = a_{i - 1} + x or a_i = a_{i - 1} - y. Your goal is to build such a sequence a that a_i <= B for all i and \sum\limits_{i=0}^{n}{a_i} is maximum possible.
Input: ['35 100 1 307 1000000000 1000000000 10000000004 1 7 3'] Output:['15', '4000000000', '-10', '']
[ 2 ]
There's a chip in the point (0, 0) of the coordinate plane. In one operation, you can move the chip from some point (x_1, y_1) to some point (x_2, y_2) if the Euclidean distance between these two points is an integer (i.e. \sqrt{(x_1-x_2)^2+(y_1-y_2)^2} is integer).Your task is to determine the minimum number of operations required to move the chip from the point (0, 0) to the point (x, y).
Input: ['38 60 09 15'] Output:['1', '0', '2', '']
[ 0, 3 ]
You are given two sets of positive integers A and B. You have to find two non-empty subsets S_A \subseteq A, S_B \subseteq B so that the least common multiple (LCM) of the elements of S_A is equal to the least common multiple (LCM) of the elements of S_B.
Input: ['4', '3 4', '5 6 7', '2 8 9 10', '4 4', '5 6 7 8', '2 3 4 9', '1 3', '1', '1 2 3', '5 6', '3 4 9 7 8', '2 15 11 14 20 12', ''] Output:['NO', 'YES', '1 2', '6', '2 3', 'YES', '1 1', '1', '1', 'YES', '3 2', '3 7 4', '12 14', '']
[ 3 ]
We say that a sequence of n integers a_1, a_2, ..., a_n is a palindrome if for all 1 <=q i <=q n, a_i = a_{n-i+1}. You are given a sequence of n integers a_1, a_2, ..., a_n and you have to find, if it exists, a cycle permutation \sigma so that the sequence a_{\sigma(1)}, a_{\sigma(2)}, ..., a_{\sigma(n)} is a palindrome. A permutation of 1, 2, ..., n is a bijective function from \{1, 2, ..., n\} to \{1, 2, ..., n\}. We say that a permutation \sigma is a cycle permutation if 1, \sigma(1), \sigma^2(1), ..., \sigma^{n-1}(1) are pairwise different numbers. Here \sigma^m(1) denotes \underbrace{\sigma(\sigma(... \sigma}_{m \text{ times}}(1) ...)).
Input: ['3', '4', '1 2 2 1', '3', '1 2 1', '7', '1 3 3 3 1 2 2', ''] Output:['YES', '3 1 4 2 ', 'NO', 'YES', '5 3 7 2 6 4 1 ', '']
[ 3 ]
You are given n integers a_1, a_2, ..., a_n. For any real number t, consider the complete weighted graph on n vertices K_n(t) with weight of the edge between vertices i and j equal to w_{ij}(t) = a_i \cdot a_j + t \cdot (a_i + a_j). Let f(t) be the cost of the minimum spanning tree of K_n(t). Determine whether f(t) is bounded above and, if so, output the maximum value it attains.
Input: ['521 02-1 131 -1 -233 -1 -241 2 3 -4'] Output:['INF', '-1', 'INF', '-6', '-18', '']
[ 2, 3, 4 ]
You are given an undirected unrooted tree, i.e. a connected undirected graph without cycles.You must assign a nonzero integer weight to each vertex so that the following is satisfied: if any vertex of the tree is removed, then each of the remaining connected components has the same sum of weights in its vertices.
Input: ['2', '5', '1 2', '1 3', '3 4', '3 5', '3', '1 2', '1 3', ''] Output:['-3 5 1 2 2', '1 1 1', '']
[ 3 ]
We say that a positive integer n is k-good for some positive integer k if n can be expressed as a sum of k positive integers which give k distinct remainders when divided by k.Given a positive integer n, find some k >=q 2 so that n is k-good or tell that such a k does not exist.
Input: ['5', '2', '4', '6', '15', '20', ''] Output:['-1', '-1', '3', '3', '5', '']
[ 3 ]
You are given an array of n non-negative integers a_1, a_2, ..., a_n. You can make the following operation: choose an integer x >=q 2 and replace each number of the array by the remainder when dividing that number by x, that is, for all 1 <=q i <=q n set a_i to a_i \bmod x.Determine if it is possible to make all the elements of the array equal by applying the operation zero or more times.
Input: ['442 5 6 831 1 154 1 7 0 845 9 17 5'] Output:['YES', 'YES', 'NO', 'YES', '']
[ 3 ]
You are given a list of n integers. You can perform the following operation: you choose an element x from the list, erase x from the list, and subtract the value of x from all the remaining elements. Thus, in one operation, the length of the list is decreased by exactly 1.Given an integer k (k>0), find if there is some sequence of n-1 operations such that, after applying the operations, the only remaining element of the list is equal to k.
Input: ['44 54 2 2 75 41 9 1 3 42 1717 02 1718 18'] Output:['YES', 'NO', 'YES', 'NO', '']
[ 2, 3 ]
You are given an array a_1, a_2, ..., a_n of positive integers. A good pair is a pair of indices (i, j) with 1 <=q i, j <=q n such that, for all 1 <=q k <=q n, the following equality holds: |a_i - a_k| + |a_k - a_j| = |a_i - a_j|, where |x| denotes the absolute value of x.Find a good pair. Note that i can be equal to j.
Input: ['335 2 751 4 2 2 312'] Output:['2 3', '1 2', '1 1', '']
[ 3 ]
Given a list of distinct values, we denote with first minimum, second minimum, and third minimum the three smallest values (in increasing order).A permutation p_1, p_2, ..., p_n is good if the following statement holds for all pairs (l,r) with 1<= l < l+2 <= r<= n. If \{p_l, p_r\} are (not necessarily in this order) the first and second minimum of p_l, p_{l+1}, ..., p_r then the third minimum of p_l, p_{l+1},..., p_r is either p_{l+1} or p_{r-1}. You are given an integer n and a string s of length m consisting of characters "<" and ">".Count the number of good permutations p_1, p_2,..., p_n such that, for all 1<= i<= m, p_i < p_{i+1} if s_i = "<"; p_i > p_{i+1} if s_i = ">". As the result can be very large, you should print it modulo 998\,244\,353.
Input: ['5 3', '>>>', ''] Output:['5', '']
[ 3 ]
There are n locations on a snowy mountain range (numbered from 1 to n), connected by n-1 trails in the shape of a tree. Each trail has length 1. Some of the locations are base lodges. The height h_i of each location is equal to the distance to the nearest base lodge (a base lodge has height 0).There is a skier at each location, each skier has initial kinetic energy 0. Each skier wants to ski along as many trails as possible. Suppose that the skier is skiing along a trail from location i to j. Skiers are not allowed to ski uphill (i.e., if h_i < h_j). It costs one unit of kinetic energy to ski along flat ground (i.e., if h_i = h_j), and a skier gains one unit of kinetic energy by skiing downhill (i.e., if h_i > h_j). For each location, compute the length of the longest sequence of trails that the skier starting at that location can ski along without their kinetic energy ever becoming negative. Skiers are allowed to visit the same location or trail multiple times.
Input: ['6', '1 1 0 0 0 0', '1 3', '2 4', '3 4', '4 5', '5 6', ''] Output:['0 0 1 1 3 5 ', '']
[ 2 ]
You are given an integer n and a string s consisting of 2^n lowercase letters of the English alphabet. The characters of the string s are s_0s_1s_2\cdots s_{2^n-1}.A string t of length 2^n (whose characters are denoted by t_0t_1t_2\cdots t_{2^n-1}) is a xoration of s if there exists an integer j (0<= j <=q 2^n-1) such that, for each 0 <=q i <=q 2^n-1, t_i = s_{i \oplus j} (where \oplus denotes the operation bitwise XOR).Find the lexicographically minimal xoration of s.A string a is lexicographically smaller than a string b if and only if one of the following holds: a is a prefix of b, but a!=b; in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
Input: ['2', 'acba', ''] Output:['abca', '']
[ 2 ]
You are given an array of integers a_1, a_2, ..., a_n.You can do the following operation any number of times (possibly zero): Choose any index i and set a_i to any integer (positive, negative or 0). What is the minimum number of operations needed to turn a into an arithmetic progression? The array a is an arithmetic progression if a_{i+1}-a_i=a_i-a_{i-1} for any 2 <=q i <=q n-1.
Input: ['9', '3 2 7 8 6 9 5 4 1', ''] Output:['6', '']
[ 0, 3 ]
Alice's potion making professor gave the following assignment to his students: brew a potion using n ingredients, such that the proportion of ingredient i in the final potion is r_i > 0 (and r_1 + r_2 + \cdots + r_n = 1).He forgot the recipe, and now all he remembers is a set of n-1 facts of the form, "ingredients i and j should have a ratio of x to y" (i.e., if a_i and a_j are the amounts of ingredient i and j in the potion respectively, then it must hold a_i/a_j = x/y), where x and y are positive integers. However, it is guaranteed that the set of facts he remembers is sufficient to uniquely determine the original values r_i.He decided that he will allow the students to pass the class as long as they submit a potion which satisfies all of the n-1 requirements (there may be many such satisfactory potions), and contains a positive integer amount of each ingredient.Find the minimum total amount of ingredients needed to make a potion which passes the class. As the result can be very large, you should print the answer modulo 998\,244\,353.
Input: ['343 2 3 41 2 4 31 4 2 485 4 2 36 4 5 41 3 5 26 8 2 13 5 3 43 2 2 56 7 4 3178 7 4 169 17 4 55 14 13 1211 1 17 146 13 8 92 11 3 114 17 7 217 16 8 615 5 1 1416 7 1 1012 17 13 1011 16 7 210 11 6 413 17 14 63 11 15 815 6 12 8'] Output:['69', '359', '573672453', '']
[ 3 ]
Alice has a cake, and she is going to cut it. She will perform the following operation n-1 times: choose a piece of the cake (initially, the cake is all one piece) with weight w>= 2 and cut it into two smaller pieces of weight \lfloor\frac{w}{2}\rfloor and \lceil\frac{w}{2}\rceil (\lfloor x \rfloor and \lceil x \rceil denote floor and ceiling functions, respectively).After cutting the cake in n pieces, she will line up these n pieces on a table in an arbitrary order. Let a_i be the weight of the i-th piece in the line.You are given the array a. Determine whether there exists an initial weight and sequence of operations which results in a.
Input: ['1413272869 5412985214736 98521473732 3 132 3 361 1 1 1 1 16100 100 100 100 100 1008100 100 100 100 100 100 100 10082 16 1 8 64 1 4 32101 2 4 7 1 1 1 1 7 2107 1 1 1 3 1 3 3 2 3101 4 4 1 1 1 3 3 3 1102 3 2 2 1 2 2 2 2 24999999999 999999999 999999999 999999999'] Output:['YES', 'NO', 'YES', 'YES', 'NO', 'YES', 'NO', 'YES', 'YES', 'YES', 'YES', 'NO', 'NO', 'YES', '']
[ 2 ]
There are n pieces of cake on a line. The i-th piece of cake has weight a_i (1 <=q i <=q n).The tastiness of the cake is the maximum total weight of two adjacent pieces of cake (i. e., \max(a_1+a_2,\, a_2+a_3,\, ...,\, a_{n-1} + a_{n})).You want to maximize the tastiness of the cake. You are allowed to do the following operation at most once (doing more operations would ruin the cake): Choose a contiguous subsegment a[l, r] of pieces of cake (1 <=q l <=q r <=q n), and reverse it. The subsegment a[l, r] of the array a is the sequence a_l, a_{l+1}, ..., a_r.If you reverse it, the array will become a_1, a_2, ..., a_{l-2}, a_{l-1}, \underline{a_r}, \underline{a_{r-1}}, \underline{...}, \underline{a_{l+1}}, \underline{a_l}, a_{r+1}, a_{r+2}, ..., a_{n-1}, a_n.For example, if the weights are initially [5, 2, 1, 4, 7, 3], you can reverse the subsegment a[2, 5], getting [5, \underline{7}, \underline{4}, \underline{1}, \underline{2}, 3]. The tastiness of the cake is now 5 + 7 = 12 (while before the operation the tastiness was 4+7=11).Find the maximum tastiness of the cake after doing the operation at most once.
Input: ['565 2 1 4 7 3332 78 78369 54 918999021 999021 999021 999021 999652 999021 999021 99902121000000000 1000000000'] Output:['12', '156', '160', '1998673', '2000000000', '']
[ 0, 2 ]
Monocarp is playing a tower defense game. A level in the game can be represented as an OX axis, where each lattice point from 1 to n contains a tower in it.The tower in the i-th point has c_i mana capacity and r_i mana regeneration rate. In the beginning, before the 0-th second, each tower has full mana. If, at the end of some second, the i-th tower has x mana, then it becomes \mathit{min}(x + r_i, c_i) mana for the next second.There are q monsters spawning on a level. The j-th monster spawns at point 1 at the beginning of t_j-th second, and it has h_j health. Every monster is moving 1 point per second in the direction of increasing coordinate.When a monster passes the tower, the tower deals \mathit{min}(H, M) damage to it, where H is the current health of the monster and M is the current mana amount of the tower. This amount gets subtracted from both monster's health and tower's mana.Unfortunately, sometimes some monsters can pass all n towers and remain alive. Monocarp wants to know what will be the total health of the monsters after they pass all towers.
Input: ['3', '5 1', '7 4', '4 2', '4', '0 14', '1 10', '3 16', '10 16', ''] Output:['4', '']
[ 0, 4 ]
Let's denote the size of the maximum matching in a graph G as \mathit{MM}(G).You are given a bipartite graph. The vertices of the first part are numbered from 1 to n, the vertices of the second part are numbered from n+1 to 2n. Each vertex's degree is 2.For a tuple of four integers (l, r, L, R), where 1 <= l <= r <= n and n+1 <= L <= R <= 2n, let's define G'(l, r, L, R) as the graph which consists of all vertices of the given graph that are included in the segment [l, r] or in the segment [L, R], and all edges of the given graph such that each of their endpoints belongs to one of these segments. In other words, to obtain G'(l, r, L, R) from the original graph, you have to remove all vertices i such that i \notin [l, r] and i \notin [L, R], and all edges incident to these vertices.Calculate the sum of \mathit{MM}(G(l, r, L, R)) over all tuples of integers (l, r, L, R) having 1 <= l <= r <= n and n+1 <= L <= R <= 2n.
Input: ['5', '4 6', '4 9', '2 6', '3 9', '1 8', '5 10', '2 7', '3 7', '1 10', '5 8', ''] Output:['314', '']
[ 0, 2, 3 ]
You are given n distinct points on a plane. The coordinates of the i-th point are (x_i, y_i).For each point i, find the nearest (in terms of Manhattan distance) point with integer coordinates that is not among the given n points. If there are multiple such points β€” you can choose any of them.The Manhattan distance between two points (x_1, y_1) and (x_2, y_2) is |x_1 - x_2| + |y_1 - y_2|.
Input: ['6', '2 2', '1 2', '2 1', '3 2', '2 3', '5 5', ''] Output:['1 1', '1 1', '2 0', '3 1', '2 4', '5 4', '']
[ 4 ]
There is a classroom with two rows of computers. There are n computers in each row and each computer has its own grade. Computers in the first row has grades a_1, a_2, ..., a_n and in the second row β€” b_1, b_2, ..., b_n.Initially, all pairs of neighboring computers in each row are connected by wire (pairs (i, i + 1) for all 1 <= i < n), so two rows form two independent computer networks.Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the i-th computer from the first row and the j-th computer from the second row costs |a_i - b_j|.You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts.That is the minimum total cost to make a fault-tolerant network?
Input: ['231 10 120 4 2541 1 1 11000000000 1000000000 1000000000 1000000000'] Output:['31', '1999999998', '']
[ 0 ]
Recently, your friend discovered one special operation on an integer array a: Choose two indices i and j (i \neq j); Set a_i = a_j = |a_i - a_j|. After playing with this operation for a while, he came to the next conclusion: For every array a of n integers, where 1 <= a_i <= 10^9, you can find a pair of indices (i, j) such that the total sum of a will decrease after performing the operation. This statement sounds fishy to you, so you want to find a counterexample for a given integer n. Can you find such counterexample and prove him wrong?In other words, find an array a consisting of n integers a_1, a_2, ..., a_n (1 <= a_i <= 10^9) such that for all pairs of indices (i, j) performing the operation won't decrease the total sum (it will increase or not change the sum).
Input: ['325123'] Output:['YES', '1 337', 'NO', 'YES', '31 4 159', '']
[ 2 ]
Vitaly enrolled in the course Advanced Useless Algorithms. The course consists of n tasks. Vitaly calculated that he has a_i hours to do the task i from the day he enrolled in the course. That is, the deadline before the i-th task is a_i hours. The array a is sorted in ascending order, in other words, the job numbers correspond to the order in which the assignments are turned in.Vitaly does everything conscientiously, so he wants to complete each task by 100 percent, or more. Initially, his completion rate for each task is 0 percent.Vitaly has m training options, each option can be used not more than once. The ith option is characterized by three integers: e_i, t_i and p_i. If Vitaly uses the ith option, then after t_i hours (from the current moment) he will increase the progress of the task e_i by p_i percent. For example, let Vitaly have 3 of tasks to complete. Let the array a have the form: a = [5, 7, 8]. Suppose Vitaly has 5 of options: [e_1=1, t_1=1, p_1=30], [e_2=2, t_2=3, p_2=50], [e_3=2, t_3=3, p_3=100], [e_4=1, t_4=1, p_4=80], [e_5=3, t_5=3, p_5=100]. Then, if Vitaly prepares in the following way, he will be able to complete everything in time: Vitaly chooses the 4-th option. Then in 1 hour, he will complete the 1-st task at 80 percent. He still has 4 hours left before the deadline for the 1-st task. Vitaly chooses the 3-rd option. Then in 3 hours, he will complete the 2-nd task in its entirety. He has another 1 hour left before the deadline for the 1-st task and 4 hours left before the deadline for the 3-rd task. Vitaly chooses the 1-st option. Then after 1 hour, he will complete the 1-st task for 110 percent, which means that he will complete the 1-st task just in time for the deadline. Vitaly chooses the 5-th option. He will complete the 3-rd task for 2 hours, and after another 1 hour, Vitaly will complete the 3-rd task in its entirety. Thus, Vitaly has managed to complete the course completely and on time, using the 4 options.Help Vitaly β€” print the options for Vitaly to complete the tasks in the correct order. Please note: each option can be used not more than once. If there are several possible answers, it is allowed to output any of them.
Input: ['53 55 7 81 1 302 3 502 3 1001 1 803 3 1001 5511 36 911 8 401 42 831 3 451 13 402 99 202 8 642 7 641 20 562 8 762 20 481 2 891 3 382 18 661 7 513 27 18 331 5 803 4 372 5569452312 7035659751 928391659 661 915310 822 87017081 921 415310 542 567745964 82'] Output:['4', '1 4 3 5 ', '3', '2 4 5 ', '4', '6 7 1 2 ', '-1', '4', '2 4 3 5 ', '']
[ 2 ]
Now Dmitry has a session, and he has to pass n exams. The session starts on day 1 and lasts d days. The ith exam will take place on the day of a_i (1 <= a_i <= d), all a_i β€” are different. Sample, where n=3, d=12, a=[3,5,9]. Orange β€” exam days. Before the first exam Dmitry will rest 2 days, before the second he will rest 1 day and before the third he will rest 3 days. For the session schedule, Dmitry considers a special value \mu β€” the smallest of the rest times before the exam for all exams. For example, for the image above, \mu=1. In other words, for the schedule, he counts exactly n numbers β€” how many days he rests between the exam i-1 and i (for i=0 between the start of the session and the exam i). Then it finds \mu β€” the minimum among these n numbers.Dmitry believes that he can improve the schedule of the session. He may ask to change the date of one exam (change one arbitrary value of a_i). Help him change the date so that all a_i remain different, and the value of \mu is as large as possible.For example, for the schedule above, it is most advantageous for Dmitry to move the second exam to the very end of the session. The new schedule will take the form: Now the rest periods before exams are equal to [2,2,5]. So, \mu=2. Dmitry can leave the proposed schedule unchanged (if there is no way to move one exam so that it will lead to an improvement in the situation).
Input: ['93 123 5 92 51 52 1001 25 153 6 9 12 153 10000000001 400000000 5000000002 103 42 21 24 156 11 12 132 2017 20'] Output:['2', '1', '1', '2', '99999999', '3', '0', '1', '9', '']
[ 2, 3, 4 ]
Petya got an array a of numbers from 1 to n, where a[i]=i.He performed n operations sequentially. In the end, he received a new state of the a array.At the i-th operation, Petya chose the first i elements of the array and cyclically shifted them to the right an arbitrary number of times (elements with indexes i+1 and more remain in their places). One cyclic shift to the right is such a transformation that the array a=[a_1, a_2, ..., a_n] becomes equal to the array a = [a_i, a_1, a_2, ..., a_{i-2}, a_{i-1}, a_{i+1}, a_{i+2}, ..., a_n].For example, if a = [5,4,2,1,3] and i=3 (that is, this is the third operation), then as a result of this operation, he could get any of these three arrays: a = [5,4,2,1,3] (makes 0 cyclic shifts, or any number that is divisible by 3); a = [2,5,4,1,3] (makes 1 cyclic shift, or any number that has a remainder of 1 when divided by 3); a = [4,2,5,1,3] (makes 2 cyclic shifts, or any number that has a remainder of 2 when divided by 3). Let's look at an example. Let n=6, i.e. initially a=[1,2,3,4,5,6]. A possible scenario is described below. i=1: no matter how many cyclic shifts Petya makes, the array a does not change. i=2: let's say Petya decided to make a 1 cyclic shift, then the array will look like a = [\textbf{2}, \textbf{1}, 3, 4, 5, 6]. i=3: let's say Petya decided to make 1 cyclic shift, then the array will look like a = [\textbf{3}, \textbf{2}, \textbf{1}, 4, 5, 6]. i=4: let's say Petya decided to make 2 cyclic shifts, the original array will look like a = [\textbf{1}, \textbf{4}, \textbf{3}, \textbf{2}, 5, 6]. i=5: let's say Petya decided to make 0 cyclic shifts, then the array won't change. i=6: let's say Petya decided to make 4 cyclic shifts, the array will look like a = [\textbf{3}, \textbf{2}, \textbf{5}, \textbf{6}, \textbf{1}, \textbf{4}]. You are given a final array state a after all n operations. Determine if there is a way to perform the operation that produces this result. In this case, if an answer exists, print the numbers of cyclical shifts that occurred during each of the n operations.
Input: ['363 2 5 6 1 433 1 285 8 1 3 2 6 4 7'] Output:['0 1 1 2 0 4 ', '0 0 1 ', '0 1 2 0 2 5 6 2 ', '']
[ 0, 3 ]
On the number line there are m points, i-th of which has integer coordinate x_i and integer weight w_i. The coordinates of all points are different, and the points are numbered from 1 to m.A sequence of n segments [l_1, r_1], [l_2, r_2], ..., [l_n, r_n] is called system of nested segments if for each pair i, j (1 <= i < j <= n) the condition l_i < l_j < r_j < r_i is satisfied. In other words, the second segment is strictly inside the first one, the third segment is strictly inside the second one, and so on.For a given number n, find a system of nested segments such that: both ends of each segment are one of m given points; the sum of the weights 2\cdot n of the points used as ends of the segments is minimal. For example, let m = 8. The given points are marked in the picture, their weights are marked in red, their coordinates are marked in blue. Make a system of three nested segments: weight of the first segment: 1 + 1 = 2 weight of the second segment: 10 + (-1) = 9 weight of the third segment: 3 + (-2) = 1 sum of the weights of all the segments in the system: 2 + 9 + 1 = 12 System of three nested segments
Input: ['3', '', '3 8', '0 10', '-2 1', '4 10', '11 20', '7 -1', '9 1', '2 3', '5 -2', '', '3 6', '-1 2', '1 3', '3 -1', '2 4', '4 0', '8 2', '', '2 5', '5 -1', '3 -2', '1 0', '-2 0', '-5 -3', ''] Output:['12', '2 6', '5 1', '7 8', '', '10', '1 6', '5 2', '3 4', '', '-6', '5 1', '4 2']
[ 2 ]
Not so long ago, Vlad came up with an interesting function: f_a(x)=<=ft\lfloor\frac{x}{a}\right\rfloor + x \bmod a, where <=ft\lfloor\frac{x}{a}\right\rfloor is \frac{x}{a}, rounded down, x \bmod a β€” the remainder of the integer division of x by a.For example, with a=3 and x=11, the value f_3(11) = <=ft\lfloor\frac{11}{3}\right\rfloor + 11 \bmod 3 = 3 + 2 = 5.The number a is fixed and known to Vlad. Help Vlad find the maximum value of f_a(x) if x can take any integer value from l to r inclusive (l <= x <= r).
Input: ['51 4 35 8 46 10 61 1000000000 100000000010 12 8'] Output:['2', '4', '5', '999999999', '5', '']
[ 3 ]
Daniel is watching a football team playing a game during their training session. They want to improve their passing skills during that session.The game involves n players, making multiple passes towards each other. Unfortunately, since the balls were moving too fast, after the session Daniel is unable to know how many balls were involved during the game. The only thing he knows is the number of passes delivered by each player during all the session.Find the minimum possible amount of balls that were involved in the game.
Input: ['442 3 3 231 5 220 041000000000 1000000000 1000000000 1000000000'] Output:['1', '2', '0', '1', '']
[ 2 ]
You are given an array a of n positive integers numbered from 1 to n. Let's call an array integral if for any two, not necessarily different, numbers x and y from this array, x >= y, the number <=ft \lfloor \frac{x}{y} \right \rfloor (x divided by y with rounding down) is also in this array.You are guaranteed that all numbers in a do not exceed c. Your task is to check whether this array is integral.
Input: ['43 51 2 54 101 3 3 71 221 11'] Output:['Yes', 'No', 'No', 'Yes', '']
[ 0, 3 ]
Egor has a table of size n * m, with lines numbered from 1 to n and columns numbered from 1 to m. Each cell has a color that can be presented as an integer from 1 to 10^5.Let us denote the cell that lies in the intersection of the r-th row and the c-th column as (r, c). We define the manhattan distance between two cells (r_1, c_1) and (r_2, c_2) as the length of a shortest path between them where each consecutive cells in the path must have a common side. The path can go through cells of any color. For example, in the table 3 * 4 the manhattan distance between (1, 2) and (3, 3) is 3, one of the shortest paths is the following: (1, 2) \to (2, 2) \to (2, 3) \to (3, 3). Egor decided to calculate the sum of manhattan distances between each pair of cells of the same color. Help him to calculate this sum.
Input: ['2 3', '1 2 3', '3 2 1', ''] Output:['7', '']
[ 3 ]
Madoka has become too lazy to write a legend, so let's go straight to the formal description of the problem.An array of integers a_1, a_2, ..., a_n is called a hill if it is not empty and there is an index i in it, for which the following is true: a_1 < a_2 < ... < a_i > a_{i + 1} > a_{i + 2} > ... > a_n.A sequence x is a subsequence of a sequence y if x can be obtained from y by deletion of several (possibly, zero or all) elements keeping the order of the other elements. For example, for an array [69, 1000, 228, -7] the array [1000, -7] is a subsequence, while [1] and [-7, 1000] are not.Splitting an array into two subsequences is called good if each element belongs to exactly one subsequence, and also each of these subsequences is a hill.You are given an array of distinct positive integers a_1, a_2, ... a_n. It is required to find the number of different pairs of maxima of the first and second subsequences among all good splits. Two pairs that only differ in the order of elements are considered same.
Input: ['4', '1 2 4 3', ''] Output:['3', '']
[ 2 ]
After the most stunning success with the fifth-graders, Madoka has been trusted with teaching the sixth-graders.There's n single-place desks in her classroom. At the very beginning Madoka decided that the student number b_i (1 <= b_i <= n) will sit at the desk number i. Also there's an infinite line of students with numbers n + 1, n + 2, n + 3, ... waiting at the door with the hope of being able to learn something from the Madoka herself. Pay attention that each student has his unique number.After each lesson, the following happens in sequence. The student sitting at the desk i moves to the desk p_i. All students move simultaneously. If there is more than one student at a desk, the student with the lowest number keeps the place, and the others are removed from the class forever. For all empty desks in ascending order, the student from the lowest number from the outside line occupies the desk. Note that in the end there is exactly one student at each desk again. It is guaranteed that the numbers p are such that at least one student is removed after each lesson. Check out the explanation to the first example for a better understanding.After several (possibly, zero) lessons the desk i is occupied by student a_i. Given the values a_1, a_2, ..., a_n and p_1, p_2, ..., p_n, find the lexicographically smallest suitable initial seating permutation b_1, b_2, ..., b_n.The permutation is an array of n different integers from 1 up to n in any order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not (2 occurs twice). [1,3,4] is not a permutation either (n=3 but there's 4 in the array).For two different permutations a and b of the same length, a is lexicographically less than b if in the first position where a and b differ, the permutation a has a smaller element than the corresponding element in b.
Input: ['5', '5 5 3 3 1', '1 8 2 9 4', ''] Output:['1 3 2 5 4 ', '']
[ 2 ]
Madoka is going to enroll in "TSUNS PTU". But she stumbled upon a difficult task during the entrance computer science exam: A number is called good if it is a multiple of d. A number is called beatiful if it is good and it cannot be represented as a product of two good numbers. Notice that a beautiful number must be good.Given a good number x, determine whether it can be represented in at least two different ways as a product of several (possibly, one) beautiful numbers. Two ways are different if the sets of numbers used are different.Solve this problem for Madoka and help her to enroll in the best school in Russia!
Input: ['86 212 236 28 21000 102376 6128 416384 4'] Output:['NO', 'NO', 'YES', 'NO', 'YES', 'YES', 'NO', 'YES', '']
[ 3 ]
Madoka as a child was an extremely capricious girl, and one of her favorite pranks was drawing on her wall. According to Madoka's memories, the wall was a table of n rows and m columns, consisting only of zeroes and ones. The coordinate of the cell in the i-th row and the j-th column (1 <= i <= n, 1 <= j <= m) is (i, j).One day she saw a picture "Mahou Shoujo Madoka Magica" and decided to draw it on her wall. Initially, the Madoka's table is a table of size n * m filled with zeroes. Then she applies the following operation any number of times:Madoka selects any rectangular subtable of the table and paints it in a chess coloring (the upper left corner of the subtable always has the color 0). Note that some cells may be colored several times. In this case, the final color of the cell is equal to the color obtained during the last repainting. White color means 0, black means 1. So, for example, the table in the first picture is painted in a chess coloring, and the others are not. For better understanding of the statement, we recommend you to read the explanation of the first test.Help Madoka and find some sequence of no more than n \cdot m operations that allows you to obtain the picture she wants, or determine that this is impossible.
Input: ['44 5010001010001010001102 30010103 31101010001 10'] Output:['4', '1 1 3 3', '3 3 4 4', '4 3 4 4', '4 2 4 3', '1', '1 2 2 3', '-1', '0', '']
[ 2 ]
Madoka's father just reached 1 million subscribers on Mathub! So the website decided to send him a personalized award β€” The Mathhub's Bit Button! The Bit Button is a rectangular table with n rows and m columns with 0 or 1 in each cell. After exploring the table Madoka found out that: A subrectangle A is contained in a subrectangle B if there's no cell contained in A but not contained in B. Two subrectangles intersect if there is a cell contained in both of them. A subrectangle is called black if there's no cell with value 0 inside it. A subrectangle is called nice if it's black and it's not contained in another black subrectangle. The table is called elegant if there are no two nice intersecting subrectangles.For example, in the first illustration the red subrectangle is nice, but in the second one it's not, because it's contained in the purple subrectangle. Help Madoka to determine whether the table is elegant.
Input: ['53 31000110113 31101111101 5011114 5111110101001000010003 2110011'] Output:['YES', 'NO', 'YES', 'NO', 'YES', '']
[ 0 ]
Madoka finally found the administrator password for her computer. Her father is a well-known popularizer of mathematics, so the password is the answer to the following problem.Find the maximum decimal number without zeroes and with no equal digits in a row, such that the sum of its digits is n.Madoka is too tired of math to solve it herself, so help her to solve this problem!
Input: ['512345'] Output:['1', '2', '21', '121', '212', '']
[ 3 ]
There are n players, numbered from 1 to n sitting around a round table. The (i+1)-th player sits to the right of the i-th player for 1 <= i < n, and the 1-st player sits to the right of the n-th player.There are n^2 cards, each of which has an integer between 1 and n written on it. For each integer from 1 to n, there are exactly n cards having this number.Initially, all these cards are distributed among all the players, in such a way that each of them has exactly n cards. In one operation, each player chooses one of his cards and passes it to the player to his right. All these actions are performed simultaneously. Player i is called solid if all his cards have the integer i written on them. Their objective is to reach a configuration in which everyone is solid. Find a way to do it using at most (n^2-n) operations. You do not need to minimize the number of operations.
Input: ['2', '2 1', '1 2', ''] Output:['1', '2 1 ', '']
[ 2 ]
You have a rectangular board of size n* m (n rows, m columns). The n rows are numbered from 1 to n from top to bottom, and the m columns are numbered from 1 to m from left to right. The cell at the intersection of row i and column j contains the number i^j (i raised to the power of j). For example, if n=3 and m=3 the board is as follows: Find the number of distinct integers written on the board.
Input: ['3 3', ''] Output:['7', '']
[ 0, 3 ]
A number is called powerful if it is a power of two or a factorial. In other words, the number m is powerful if there exists a non-negative integer d such that m=2^d or m=d!, where d!=1\cdot 2\cdot ... \cdot d (in particular, 0! = 1). For example 1, 4, and 6 are powerful numbers, because 1=1!, 4=2^2, and 6=3! but 7, 10, or 18 are not.You are given a positive integer n. Find the minimum number k such that n can be represented as the sum of k distinct powerful numbers, or say that there is no such k.
Input: ['471124017179869184'] Output:['2', '3', '4', '1', '']
[ 0, 3 ]
\def\myred#1{\color{red}{\underline{\bf{#1}}}} \def\myblue#1{\color{blue}{\overline{\bf{#1}}}} \def\RED{\myred{Red}} \def\BLUE{\myblue{Blue}}You are given a sequence of n non-negative integers a_1, a_2, ..., a_n. Initially, all the elements of the sequence are unpainted. You can paint each number \RED or \BLUE (but not both), or leave it unpainted. For a color c, \text{Count}(c) is the number of elements in the sequence painted with that color and \text{Sum}(c) is the sum of the elements in the sequence painted with that color.For example, if the given sequence is [2, 8, 6, 3, 1] and it is painted this way: [\myblue{2}, 8, \myred{6}, \myblue{3}, 1] (where 6 is painted red, 2 and 3 are painted blue, 1 and 8 are unpainted) then \text{Sum}(\RED)=6, \text{Sum}(\BLUE)=2+3=5, \text{Count}(\RED)=1, and \text{Count}(\BLUE)=2.Determine if it is possible to paint the sequence so that \text{Sum}(\RED) > \text{Sum}(\BLUE) and \text{Count}(\RED) < \text{Count}(\BLUE).
Input: ['431 2 352 8 6 3 143 5 4 251000000000 1000000000 1000000000 1000000000 1000000000'] Output:['NO', 'YES', 'NO', 'NO', '']
[ 0, 2 ]
Luis has a sequence of n+1 integers a_1, a_2, ..., a_{n+1}. For each i = 1, 2, ..., n+1 it is guaranteed that 0<=q a_i < n, or a_i=n^2. He has calculated the sum of all the elements of the sequence, and called this value s. Luis has lost his sequence, but he remembers the values of n and s. Can you find the number of elements in the sequence that are equal to n^2?We can show that the answer is unique under the given constraints.
Input: ['47 01 12 123 12'] Output:['0', '1', '3', '1', '']
[ 3 ]
For an array of integers a, let's define |a| as the number of elements in it.Let's denote two functions: F(a, k) is a function that takes an array of integers a and a positive integer k. The result of this function is the array containing |a| first elements of the array that you get by replacing each element of a with exactly k copies of that element.For example, F([2, 2, 1, 3, 5, 6, 8], 2) is calculated as follows: first, you replace each element of the array with 2 copies of it, so you obtain [2, 2, 2, 2, 1, 1, 3, 3, 5, 5, 6, 6, 8, 8]. Then, you take the first 7 elements of the array you obtained, so the result of the function is [2, 2, 2, 2, 1, 1, 3]. G(a, x, y) is a function that takes an array of integers a and two different integers x and y. The result of this function is the array a with every element equal to x replaced by y, and every element equal to y replaced by x.For example, G([1, 1, 2, 3, 5], 3, 1) = [3, 3, 2, 1, 5].An array a is a parent of the array b if: either there exists a positive integer k such that F(a, k) = b; or there exist two different integers x and y such that G(a, x, y) = b. An array a is an ancestor of the array b if there exists a finite sequence of arrays c_0, c_1, ..., c_m (m >= 0) such that c_0 is a, c_m is b, and for every i \in [1, m], c_{i-1} is a parent of c_i.And now, the problem itself.You are given two integers n and k. Your goal is to construct a sequence of arrays s_1, s_2, ..., s_m in such a way that: every array s_i contains exactly n elements, and all elements are integers from 1 to k; for every array a consisting of exactly n integers from 1 to k, the sequence contains at least one array s_i such that s_i is an ancestor of a. Print the minimum number of arrays in such sequence.
Input: ['3 2', ''] Output:['2', '']
[ 3 ]
Consider a grid of size n * n. The rows are numbered top to bottom from 1 to n, the columns are numbered left to right from 1 to n.The robot is positioned in a cell (1, 1). It can perform two types of moves: D β€” move one cell down; R β€” move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves s β€” the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid.
Input: ['34RD5DRDRDRDR3D'] Output:['13', '9', '3', '']
[ 0, 3 ]
There is a sheet of paper that can be represented with a grid of size n * m: n rows and m columns of cells. All cells are colored in white initially.q operations have been applied to the sheet. The i-th of them can be described as follows: x_i y_i β€” choose one of k non-white colors and color the entire row x_i and the entire column y_i in it. The new color is applied to each cell, regardless of whether the cell was colored before the operation. The sheet after applying all q operations is called a coloring. Two colorings are different if there exists at least one cell that is colored in different colors.How many different colorings are there? Print the number modulo 998\,244\,353.
Input: ['21 1 3 21 11 12 2 2 32 11 12 2'] Output:['3', '4', '']
[ 3 ]