question_text
stringlengths
2
3.82k
input_outputs
stringlengths
23
941
algo_tags
sequence
You are given a string s. You can reorder the characters to form a string t. Define t_{\mathrm{max}} to be the lexicographical maximum of t and t in reverse order.Given s determine the lexicographically minimum value of t_{\mathrm{max}} over all reorderings t 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: ['12aaababbabcaabbaabbbaaabbabbbabbbbabbcceagaffcaba'] Output:['a', 'aba', 'bab', 'bca', 'abba', 'abbba', 'ababa', 'bbab', 'bbabb', 'bbcca', 'agea', 'acffba', '']
[ 2 ]
You are given an array a_1, a_2, ..., a_n of positive integers.You can make this operation multiple (possibly zero) times: Choose two indices i, j (1 <=q i, j <=q n, i \neq j). Assign a_i := \lceil \frac{a_i}{a_j} \rceil. Here \lceil x \rceil denotes x rounded up to the smallest integer >=q x. Is it possible to make all array elements equal by some sequence of operations (possibly empty)? If yes, print any way to do it in at most 30n operations.It can be proven, that under the problem constraints, if some way exists to make all elements equal, there exists a way with at most 30n operations.
Input: ['10110031 1 122 125 534 3 243 3 4 422 10055 3 6 7 863 3 80 3 8 3419 40 19 55'] Output:['0', '0', '-1', '0', '2', '1 3', '2 1', '4', '3 1', '4 2', '1 3', '2 4', '6', '2 1', '2 1', '2 1', '2 1', '2 1', '2 1', '8', '5 2', '4 2', '3 2', '1 3', '1 3', '2 1', '4 1', '5 1', '4', '5 1', '3 1', '3 1', '3 1', '9', '4 2', '2 1', '1 2', '1 2', '3 2', '3 2', '1 4', '2 4', '3 4', '']
[ 0, 2, 3 ]
On Codeforces the "Recent Actions" field shows the last n posts with recent actions.Initially, there are posts 1, 2, ..., n in the field (this is in order from top to down). Also there are infinitely many posts not in the field, numbered with integers n + 1, n + 2, ....When recent action happens in the post p: If it is in the "Recent Actions" field, it moves from its position to the top position. Otherwise, it is added to the top position, and the post on the down position is removed from the "Recent Actions" field. You know, that the next m recent actions will happen in the posts p_1, p_2, ..., p_m (n + 1 <=q p_i <=q n + m) in the moments of time 1, 2, ..., m. Note, that recent actions only happen with posts with numbers >=q n + 1.For each post i (1 <=q i <=q n), find the first time it will be removed from the "Recent Actions" field or say, that it won't be removed.
Input: ['101 123 25 44 55 9 9 5 75 56 7 8 9 103 44 4 4 44 45 5 6 63 54 5 5 5 44 205 5 24 24 24 5 6 7 8 9 10 12 13 14 15 16 17 18 19 205 77 8 7 11 7 12 106 78 11 7 8 8 8 12'] Output:['1 ', '-1 2 1 ', '-1 5 2 1 ', '5 4 3 2 1 ', '-1 -1 1 ', '-1 -1 3 1 ', '-1 2 1 ', '8 7 3 1 ', '7 6 4 2 1 ', '-1 -1 7 3 2 1 ', '']
[ 2, 3 ]
Grandfather Ahmed's School has n+1 students. The students are divided into k classes, and s_i students study in the i-th class. So, s_1 + s_2 + ... + s_k = n+1.Due to the upcoming April Fools' Day, all students will receive gifts!Grandfather Ahmed planned to order n+1 boxes of gifts. Each box can contain one or more gifts. He plans to distribute the boxes between classes so that the following conditions are satisfied: Class number i receives exactly s_i boxes (so that each student can open exactly one box). The total number of gifts in the boxes received by the i-th class should be a multiple of s_i (it should be possible to equally distribute the gifts among the s_i students of this class). Unfortunately, Grandfather Ahmed ordered only n boxes with gifts, the i-th of which contains a_i gifts.Ahmed has to buy the missing gift box, and the number of gifts in the box should be an integer between 1 and 10^6. Help Ahmed to determine, how many gifts should the missing box contain, and build a suitable distribution of boxes to classes, or report that this is impossible.
Input: ['4 2', '7 7 7 127', '2 3', ''] Output:['1', '7 7 ', '7 127 1 ', '']
[ 3 ]
Let's call an array b_1, b_2, ..., b_m a test if b_1 = m - 1.Let's call an array b_1, b_2, ..., b_m a multitest if the array b_2, b_3, ..., b_m can be split into b_1 non-empty subarrays so that each of these subarrays is a test. Note that each element of the array must be included in exactly one subarray, and the subarrays must consist of consecutive elements.Let's define the function f from the array b_1, b_2, ..., b_m as the minimum number of operations of the form "Replace any b_i with any non-negative integer x", which needs to be done so that the array b_1, b_2, ..., b_m becomes a multitest.You are given an array of positive integers a_1, a_2, ..., a_n. For each i from 1 to n - 1, find f([a_i, a_{i+1}, ..., a_n]).Below are some examples of tests and multitests. Tests: [\underline{1}, 5], [\underline{2}, 2, 2], [\underline{3}, 4, 1, 1], [\underline{5}, 0, 0, 0, 0, 0], [\underline{7}, 1, 2, 3, 4, 5, 6, 7], [\underline{0}]. These arrays are tests since their first element (underlined) is equal to the length of the array minus one. Multitests: [1, \underline{\underline{1}, 1}], [2, \underline{\underline{3}, 0, 0, 1}, \underline{\underline{1}, 12}], [3, \underline{\underline{2}, 2, 7}, \underline{\underline{1}, 1}, \underline{\underline{3}, 4, 4, 4}], [4, \underline{\underline{0}}, \underline{\underline{3}, 1, 7, 9}, \underline{\underline{4}, 2, 0, 0, 9}, \underline{\underline{1}, 777}]. Underlined are the subarrays after the split, and double underlined are the first elements of each subarray.
Input: ['341 2 1 773 1 3 1 2 1 142 7 1 1'] Output:['0 1 1 ', '0 1 1 0 1 1 ', '1 1 1 ', '']
[ 0 ]
You are given an array a_1, a_2, ..., a_n consisting of integers such that a_1 + a_2 + ... + a_n = 0.You have to rearrange the elements of the array a so that the following condition is satisfied:\max\limits_{1 <= l <= r <= n} \lvert a_l + a_{l+1} + ... + a_r \rvert < \max(a_1, a_2, ..., a_n) - \min(a_1, a_2, ..., a_n), where |x| denotes the absolute value of x.More formally, determine if there exists a permutation p_1, p_2, ..., p_n that for the array a_{p_1}, a_{p_2}, ..., a_{p_n}, the condition above is satisfied, and find the corresponding array.Recall that the array p_1, p_2, ..., p_n is called a permutation if for each integer x from 1 to n there is exactly one i from 1 to n such that p_i = x.
Input: ['7', '4', '3 4 -2 -5', '5', '2 2 2 -3 -3', '8', '-3 -3 1 1 1 1 1 1', '3', '0 1 -1', '7', '-3 4 3 4 -4 -4 0', '1', '0', '7', '-18 13 -18 -17 12 15 13', ''] Output:['Yes', '-5 -2 3 4', 'Yes', '-3 2 -3 2 2', 'Yes', '1 1 1 -3 1 1 1 -3', 'Yes', '-1 0 1', 'Yes', '4 -4 4 -4 0 3 -3', 'No', 'Yes', '13 12 -18 15 -18 13 -17', '']
[ 2, 3 ]
The store sells n types of candies with numbers from 1 to n. One candy of type i costs b_i coins. In total, there are a_i candies of type i in the store.You need to pack all available candies in packs, each pack should contain only one type of candies. Formally, for each type of candy i you need to choose the integer d_i, denoting the number of type i candies in one pack, so that a_i is divided without remainder by d_i.Then the cost of one pack of candies of type i will be equal to b_i \cdot d_i. Let's denote this cost by c_i, that is, c_i = b_i \cdot d_i.After packaging, packs will be placed on the shelf. Consider the cost of the packs placed on the shelf, in order c_1, c_2, ..., c_n. Price tags will be used to describe costs of the packs. One price tag can describe the cost of all packs from l to r inclusive if c_l = c_{l+1} = ... = c_r. Each of the packs from 1 to n must be described by at least one price tag. For example, if c_1, ..., c_n = [4, 4, 2, 4, 4], to describe all the packs, a 3 price tags will be enough, the first price tag describes the packs 1, 2, the second: 3, the third: 4, 5.You are given the integers a_1, b_1, a_2, b_2, ..., a_n, b_n. Your task is to choose integers d_i so that a_i is divisible by d_i for all i, and the required number of price tags to describe the values of c_1, c_2, ..., c_n is the minimum possible.For a better understanding of the statement, look at the illustration of the first test case of the first test: Let's repeat the meaning of the notation used in the problem:a_i β€” the number of candies of type i available in the store.b_i β€” the cost of one candy of type i.d_i β€” the number of candies of type i in one pack.c_i β€” the cost of one pack of candies of type i is expressed by the formula c_i = b_i \cdot d_i.
Input: ['5420 36 214 520 73444 52002 102020 257 76 515 210 37 7510 111 55 12 28 267 1212 35 39 129 31000000000 10000'] Output:['2', '1', '3', '2', '5', '']
[ 2, 3 ]
Lottery "Three Sevens" was held for m days. On day i, n_i people with the numbers a_{i, 1}, ..., a_{i, n_i} participated in the lottery.It is known that in each of the m days, only one winner was selected from the lottery participants. The lottery winner on day i was not allowed to participate in the lottery in the days from i+1 to m.Unfortunately, the information about the lottery winners has been lost. You need to find any possible list of lottery winners on days from 1 to m or determine that no solution exists.
Input: ['3341 2 4 832 9 121 4221 222 1441 2 3 4111413'] Output:['8 2 1 ', '-1', '2 1 4 3 ', '']
[ 0, 2 ]
You are given two arrays a_1, a_2, ..., a_n and b_1, b_2, ..., b_n.In one operation, you can choose any integer i from 1 to n and swap the numbers a_i and b_i.Determine whether, after using any (possibly zero) number of operations, the following two conditions can be satisfied simultaneously: a_n = \max(a_1, a_2, ..., a_n), b_n = \max(b_1, b_2, ..., b_n). Here \max(c_1, c_2, ..., c_k) denotes the maximum number among c_1, c_2, ..., c_k. For example, \max(3, 5, 4) = 5, \max(1, 7, 7) = 7, \max(6, 2) = 6.
Input: ['737 9 77 6 9410 10 15 1510 16 15 152100 9999 10011191 2 3 4 5 6 7 8 99 9 9 9 9 9 6 6 671 1 2 2 1 1 21 2 1 2 1 2 1230 45 30'] Output:['Yes', 'No', 'Yes', 'Yes', 'Yes', 'No', 'No', '']
[ 2 ]
Li Hua wants to solve a problem about \varphi β€” Euler's totient function. Please recall that \varphi(x)=\sum\limits_{i=1}^x[\gcd(i,x)=1].^{,\ddagger}He has a sequence a_1,a_2,\cdots,a_n and he wants to perform m operations: "1 l r" (1<= l<= r<= n) β€” for each x\in[l,r], change a_x into \varphi(a_x). "2 l r" (1<= l<= r<= n) β€” find out the minimum changes needed to make sure a_l=a_{l+1}=\cdots=a_r. In each change, he chooses one x\in[l,r], change a_x into \varphi(a_x). Each operation of this type is independent, which means the array doesn't actually change. Suppose you were Li Hua, please solve this problem.^ \gcd(x,y) denotes the greatest common divisor (GCD) of integers x and y. ^\ddagger The notation [\textrm{cond}] equals 1 if the condition \textrm{cond} is true, and 0 otherwise.
Input: ['5 4', '8 1 6 3 7', '2 1 5', '2 3 4', '1 1 3', '2 3 4', ''] Output:['10', '2', '1', '']
[ 0, 3 ]
Li Hua has a tree of n vertices and n-1 edges. The root of the tree is vertex 1. Each vertex i has importance a_i. Denote the size of a subtree as the number of vertices in it, and the importance as the sum of the importance of vertices in it. Denote the heavy son of a non-leaf vertex as the son with the largest subtree size. If multiple of them exist, the heavy son is the one with the minimum index.Li Hua wants to perform m operations: "1 x" (1<=q x <=q n) β€” calculate the importance of the subtree whose root is x. "2 x" (2<=q x <=q n) β€” rotate the heavy son of x up. Formally, denote son_x as the heavy son of x, fa_x as the father of x. He wants to remove the edge between x and fa_x and connect an edge between son_x and fa_x. It is guaranteed that x is not root, but not guaranteed that x is not a leaf. If x is a leaf, please ignore the operation. Suppose you were Li Hua, please solve this problem.
Input: ['7 4', '1 1 1 1 1 1 1', '1 2', '1 3', '2 4', '2 5', '3 6', '6 7', '1 6', '2 3', '1 6', '1 2', ''] Output:['2', '3', '3', '']
[ 0 ]
This is an interactive problem.Li Ming and Li Hua are playing a game. Li Hua has a chessboard of size n* m. Denote (r, c) (1<= r<= n, 1<= c<= m) as the cell on the r-th row from the top and on the c-th column from the left. Li Ming put a king on the chessboard and Li Hua needs to guess its position.Li Hua can ask Li Ming no more than 3 questions. In each question, he can choose a cell and ask the minimum steps needed to move the king to the chosen cell. Each question is independent, which means the king doesn't actually move.A king can move from (x,y) to (x',y') if and only if \max\{|x-x'|,|y-y'|\}=1 (shown in the following picture). The position of the king is chosen before the interaction.Suppose you were Li Hua, please solve this problem.
Input: ['2', '3 4', '', '1', '', '2', '', '5 3', '', '3', '', '1', '', '2'] Output:['', '', '? 2 3', '', '? 2 4', '', '! 2 2', '', '? 2 2', '', '? 5 2', '', '? 5 3', '', '! 5 1']
[ 2 ]
Li Hua has a pattern of size n* n, each cell is either blue or red. He can perform exactly k operations. In each operation, he chooses a cell and changes its color from red to blue or from blue to red. Each cell can be chosen as many times as he wants. Is it possible to make the pattern, that matches its rotation by 180^{\circ}?Suppose you were Li Hua, please solve this problem.
Input: ['34 01 1 1 10 0 0 11 0 1 01 1 1 14 31 0 1 11 0 0 00 1 0 11 1 0 15 40 0 0 0 00 1 1 1 10 1 0 0 01 1 1 1 10 0 0 0 0'] Output:['NO', 'YES', 'YES', '']
[ 2 ]
There is a rectangular maze of size n* m. Denote (r,c) as the cell on the r-th row from the top and the c-th column from the left. Two cells are adjacent if they share an edge. A path is a sequence of adjacent empty cells.Each cell is initially empty. Li Hua can choose some cells (except (x_1, y_1) and (x_2, y_2)) and place an obstacle in each of them. He wants to know the minimum number of obstacles needed to be placed so that there isn't a path from (x_1, y_1) to (x_2, y_2).Suppose you were Li Hua, please solve this problem.
Input: ['34 42 2 3 36 71 1 2 39 95 1 3 6'] Output:['4', '2', '3', '']
[ 2 ]
Let's call a triple of positive integers (a, b, n) strange if the equality \frac{an}{nb} = \frac{a}{b} holds, where an is the concatenation of a and n and nb is the concatenation of n and b. For the purpose of concatenation, the integers are considered without leading zeroes.For example, if a = 1, b = 5 and n = 9, then the triple is strange, because \frac{19}{95} = \frac{1}{5}. But a = 7, b = 3 and n = 11 is not strange, because \frac{711}{113}!=\frac{7}{3}.You are given three integers A, B and N. Calculate the number of strange triples (a, b, n), such that 1 <= a < A, 1 <= b < B and 1 <= n < N.
Input: ['5 6 10', ''] Output:['7', '']
[ 0, 3 ]
Monocarp has a tree, consisting of n vertices.He is going to select some vertex r and perform the following operations on each vertex v from 1 to n: set d_v equal to the distance from v to r (the number of edges on the shortest path); color v some color. A nice coloring satisfies two conditions: for each pair of vertices of the same color (v, u), there exists a path from v to u that only visits vertices of the same color; for each pair of vertices of the same color (v, u), d_v \neq d_u. Note that Monocarp can choose any amount of different colors he wants to use.For each used color, he then counts the number of vertices of this color. The cost of the tree is the minimum of these numbers.What can be the maximum cost of the tree?
Input: ['441 22 33 451 21 31 41 531 33 273 22 57 53 11 61 4'] Output:['4', '1', '3', '3', '']
[ 2 ]
You are given an array a_1, a_2, ..., a_n, consisting of n integers. You are also given two integers k and x.You have to perform the following operation exactly once: add x to the elements on exactly k distinct positions, and subtract x from all the others.For example, if a = [2, -1, 2, 3], k = 1, x = 2, and we have picked the first element, then after the operation the array a = [4, -3, 0, 1].Let f(a) be the maximum possible sum of a subarray of a. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 <= i <= j <= n. An empty subarray should also be considered, it has sum 0.Let the array a' be the array a after applying the aforementioned operation. Apply the operation in such a way that f(a') is the maximum possible, and print the maximum possible value of f(a').
Input: ['44 1 22 -1 2 32 2 3-1 23 0 53 2 46 2 -84 -1 9 -3 7 -8'] Output:['5', '7', '0', '44', '']
[ 2 ]
A set of positive integers S is called beautiful if, for every two integers x and y from this set, either x divides y or y divides x (or both).You are given two integers l and r. Consider all beautiful sets consisting of integers not less than l and not greater than r. You have to print two numbers: the maximum possible size of a beautiful set where all elements are from l to r; the number of beautiful sets consisting of integers from l to r with the maximum possible size. Since the second number can be very large, print it modulo 998244353.
Input: ['43 1113 371 224 100'] Output:['2 4', '2 6', '5 1', '5 7', '']
[ 3, 4 ]
The FB-string is formed as follows. Initially, it is empty. We go through all positive integers, starting from 1, in ascending order, and do the following for each integer: if the current integer is divisible by 3, append F to the end of the FB-string; if the current integer is divisible by 5, append B to the end of the FB-string. Note that if an integer is divisible by both 3 and 5, we append F, and then B, not in the opposite order.The first 10 characters of the FB-string are FBFFBFFBFB: the first F comes from the integer 3, the next character (B) comes from 5, the next F comes from the integer 6, and so on. It's easy to see that this string is infinitely long. Let f_i be the i-th character of FB-string; so, f_1 is F, f_2 is B, f_3 is F, f_4 is F, and so on.You are given a string s, consisting of characters F and/or B. You have to determine whether it is a substring (contiguous subsequence) of the FB-string. In other words, determine if it is possible to choose two integers l and r (1 <= l <= r) so that the string f_l f_{l+1} f_{l+2} ... f_r is exactly s.For example: FFB is a substring of the FB-string: if we pick l = 3 and r = 5, the string f_3 f_4 f_5 is exactly FFB; BFFBFFBF is a substring of the FB-string: if we pick l = 2 and r = 9, the string f_2 f_3 f_4 ... f_9 is exactly BFFBFFBF; BBB is not a substring of the FB-string.
Input: ['33FFB8BFFBFFBF3BBB'] Output:['YES', 'YES', 'NO', '']
[ 0 ]
You are given a tree, consisting of n vertices. There are k chips, placed in vertices a_1, a_2, ..., a_k. All a_i are distinct. Vertices a_1, a_2, ..., a_k are colored black initially. The remaining vertices are white.You are going to play a game where you perform some moves (possibly, zero). On the i-th move (1-indexed) you are going to move the ((i - 1) \bmod k + 1)-st chip from its current vertex to an adjacent white vertex and color that vertex black. So, if k=3, you move chip 1 on move 1, chip 2 on move 2, chip 3 on move 3, chip 1 on move 4, chip 2 on move 5 and so on. If there is no adjacent white vertex, then the game ends.What's the maximum number of moves you can perform?
Input: ['551 22 33 44 51351 22 33 44 521 251 22 33 44 522 161 21 32 42 53 631 4 6111'] Output:['2', '0', '1', '2', '0', '']
[ 2, 4 ]
You are playing yet another game where you kill monsters using magic spells. There are n cells in the row, numbered from 1 to n. Initially, the i-th cell contains the i-th monster with h_i health.You have a basic spell that costs 1 MP and deals 1 damage to the monster you choose. You can cast it any number of times. Also, you have a special scroll with "Explosion" spell you can use only once. You want to finish killing monsters with explosion, that's why you, firstly, cast the basic spell several times (possibly, zero), and then after that, you cast one "Explosion".How does "Explosion" spell work? Firstly, you choose the power of the spell: if you pour x MP into it, "Explosion" will deal x damage. Secondly, you choose some monster i, which will be targeted by the spell. That's what happens next: if its current health h_i > x, then he stays alive with health decreased by x; if h_i <= x, the i-th monster dies with an explosion that deals h_i - 1 damage to monsters in the neighboring cells i - 1 and i + 1, if these cells exist and monsters inside are still alive; if the damage dealt by the explosion is enough to kill the monster i - 1 (or i + 1), i. e. the current h_{i - 1} <= h_i - 1 (or h_{i + 1} <= h_i - 1), then that monster also dies creating a secondary explosion of power h_{i-1} - 1 (or h_{i+1} - 1) that may deals damage to their neighbors, and so on, until the explosions end. Your goal is to kill all the remaining monsters with those "chaining" explosions, that's why you need a basic spell to decrease h_i of some monsters or even kill them beforehand (monsters die when their current health h_i becomes less or equal to zero). Note that monsters don't move between cells, so, for example, monsters i and i + 2 will never become neighbors.What is the minimum total MP you need to kill all monsters in the way you want? The total MP is counted as the sum of the number of basic spells you cast and the power x of explosion scroll you've chosen.
Input: ['531 1 144 1 2 145 10 15 1014291 2 3 2 2 2 3 2 1'] Output:['3', '6', '15', '42', '12', '']
[ 2, 3, 4 ]
You are given an undirected graph consisting of n vertices and n edges, where n is divisible by 6. Each edge has a weight, which is a positive (greater than zero) integer.The graph has the following structure: it is split into \frac{n}{3} triples of vertices, the first triple consisting of vertices 1, 2, 3, the second triple consisting of vertices 4, 5, 6, and so on. Every pair of vertices from the same triple is connected by an edge. There are no edges between vertices from different triples.You have to paint the vertices of this graph into two colors, red and blue. Each vertex should have exactly one color, there should be exactly \frac{n}{2} red vertices and \frac{n}{2} blue vertices. The coloring is called valid if it meets these constraints.The weight of the coloring is the sum of weights of edges connecting two vertices with different colors.Let W be the maximum possible weight of a valid coloring. Calculate the number of valid colorings with weight W, and print it modulo 998244353.
Input: ['12', '1 3 3 7 8 5 2 2 2 2 4 2', ''] Output:['36', '']
[ 3 ]
A tea manufacturer decided to conduct a massive tea tasting. n sorts of tea will be tasted by n tasters. Both the sorts of tea and the tasters are numbered from 1 to n. The manufacturer prepared a_i milliliters of the i-th sort of tea. The j-th taster can drink b_j milliliters of tea at once.The tasting will be conducted in steps. During the first step, the i-th taster tastes the i-th sort of tea. The i-th taster drinks \min(a_i, b_i) tea (how much is available of the i-th sort and how much the i-th taster can drink). a_i also decreases by this amount.Then all tasters move to the previous sort of tea. Thus, during the second step, the i-th taster tastes the (i-1)-st sort of tea. The i-th taster drinks \min(a_{i-1}, b_i) tea. The 1-st person ends the tasting.During the third step, the i-th taster tastes the (i-2)-nd sort of tea. The 2-nd taster ends the tasting. This goes on until everyone ends the tasting.Take a look at the tasting process for n = 3, a = [10, 20, 15], b = [9, 8, 6]. In the left row, there are the current amounts of each sort of tea. In the right column, there are current amounts of tea each taster has drunk in total. The arrow tells which taster each tea goes to on the current step. The number on the arrow is the amount β€” minimum of how much is available of the sort of tea and how much the taster can drink. For each taster, print how many milliliters of tea he/she will drink in total.
Input: ['4310 20 159 8 6157413 8 5 43 4 2 131000000000 1000000000 10000000001 1 1000000000'] Output:['9 9 12 ', '5 ', '3 8 6 4 ', '1 2 2999999997 ', '']
[ 4 ]
You are given n one-dimensional segments (each segment is denoted by two integers β€” its endpoints).Let's define the function f(x) as the number of segments covering point x (a segment covers the point x if l <= x <= r, where l is the left endpoint and r is the right endpoint of the segment).An integer point x is called ideal if it belongs to more segments than any other integer point, i. e. f(y) < f(x) is true for any other integer point y.You are given an integer k. Your task is to determine whether it is possible to remove some (possibly zero) segments, so that the given point k becomes ideal.
Input: ['44 31 37 92 53 62 91 43 71 32 43 51 46 75 5'] Output:['YES', 'NO', 'NO', 'YES', '']
[ 0, 2 ]
There are two towers consisting of blocks of two colors: red and blue. Both towers are represented by strings of characters B and/or R denoting the order of blocks in them from the bottom to the top, where B corresponds to a blue block, and R corresponds to a red block. These two towers are represented by strings BRBB and RBR. You can perform the following operation any number of times: choose a tower with at least two blocks, and move its top block to the top of the other tower. The pair of towers is beautiful if no pair of touching blocks has the same color; i. e. no red block stands on top of another red block, and no blue block stands on top of another blue block.You have to check if it is possible to perform any number of operations (possibly zero) to make the pair of towers beautiful.
Input: ['44 3BRBBRBR4 7BRBRRRBRBRB3 4RBRBRBR5 4BRBRRBRBR'] Output:['YES', 'YES', 'YES', 'NO', '']
[ 0 ]
You are given an unweighted tree of n vertices numbered from 1 to n and a list of n-1 integers a_1, a_2, ..., a_{n-1}. A tree is a connected undirected graph without cycles. You will use each element of the list to label one vertex. No vertex should be labeled twice. You can label the only remaining unlabeled vertex with any integer. A vertex x is called good if it is possible to do this labeling so that for each vertex i, its label is the distance between x and i. The distance between two vertices s and t on a tree is the minimum number of edges on a path that starts at vertex s and ends at vertex t. Find all good vertices.
Input: ['6', '2 1 0 1 2', '1 2', '2 3', '2 4', '4 5', '4 6', ''] Output:['2', '2 4 ', '']
[ 2 ]
The prime factorization of a positive integer m is the unique way to write it as \displaystyle m=p_1^{e_1}\cdot p_2^{e_2}\cdot ... \cdot p_k^{e_k}, where p_1, p_2, ..., p_k are prime numbers, p_1 < p_2 < ... < p_k and e_1, e_2, ..., e_k are positive integers. For each positive integer m, f(m) is defined as the multiset of all numbers in its prime factorization, that is f(m)=\{p_1,e_1,p_2,e_2,...,p_k,e_k\}. For example, f(24)=\{2,3,3,1\}, f(5)=\{1,5\} and f(1)=\{\}.You are given a list consisting of 2n integers a_1, a_2, ..., a_{2n}. Count how many positive integers m satisfy that f(m)=\{a_1, a_2, ..., a_{2n}\}. Since this value may be large, print it modulo 998\,244\,353.
Input: ['2', '1 3 2 3', ''] Output:['2', '']
[ 3 ]
The score of a sequence [s_1, s_2, ..., s_d] is defined as \displaystyle \frac{s_1\cdot s_2\cdot ... \cdot s_d}{d!}, where d!=1\cdot 2\cdot ... \cdot d. In particular, the score of an empty sequence is 1.For a sequence [s_1, s_2, ..., s_d], let m be the maximum score among all its subsequences. Its cost is defined as the maximum length of a subsequence with a score of m.You are given a non-decreasing sequence [a_1, a_2, ..., a_n] of integers of length n. In other words, the condition a_1 <=q a_2 <=q ... <=q a_n is satisfied. For each k=1, 2, ... , n, find the cost of the sequence [a_1, a_2, ... , a_k]. 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.
Input: ['331 2 321 155 5 5 5 5'] Output:['1 1 2 ', '1 1 ', '1 2 3 4 5 ', '']
[ 2, 3, 4 ]
You are given an array of n positive integers a_1, a_2, ..., a_n. In one operation, you can choose any number of the array and add 1 to it. Make at most 2n operations so that the array satisfies the following property: a_{i+1} is not divisible by a_i, for each i = 1, 2, ..., n-1. You do not need to minimize the number of operations.
Input: ['342 4 3 631 2 324 2'] Output:['4 5 6 7', '3 2 3', '4 2', '']
[ 2, 3 ]
Kostya and Zhenya β€” the creators of the band "Paper" β€” after the release of the legendary album decided to create a new band "Day movers", for this they need to find two new people.They invited n people to the casting. The casting will last q days. On the ith of the days, Kostya and Zhenya want to find two people on the segment from l_i to r_i who are most suitable for their band. Since "Day movers" are doing a modern art, musical skills are not important to them and they look only at other signs: they want the height difference between two people to be as small as possible.Help them, and for each day, find the minimum difference in the growth of people from the casting on this segment!
Input: ['3 31 3 21 22 31 3'] Output:['2', '1', '1', '']
[ 0 ]
The famous writer Velepin is very productive. Recently, he signed a contract with a well-known publication and now he needs to write k_i books for i-th year. This is not a problem for him at all, he can write as much as he wants about samurai, space, emptiness, insects and werewolves.He has n regular readers, each of whom in the i-th year will read one of the k_i books published by Velepin. Readers are very fond of discussing books, so the j-th of them will be satisfied within a year if at least a_j persons read the same book as him (including himself).Velepin has obvious problems with marketing, so he turned to you! A well-known book reading service can control what each of Velepin's regular readers will read, but he does not want books to be wasted, so someone should read each book. And so they turned to you with a request to tell you what the maximum number of regular readers can be made satisfied during each of the years, if you can choose each person the book he will read.
Input: ['5', '1 2 2 2 2', '3', '2', '3', '4', ''] Output:['5', '5', '3', '']
[ 2, 4 ]
In winter, the inhabitants of the Moscow Zoo are very bored, in particular, it concerns gorillas. You decided to entertain them and brought a permutation p of length n to the zoo.A permutation of length n is an array consisting of n distinct integers from 1 to n in any order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 occurs twice in the array) and [1,3,4] is also not a permutation (n=3, but 4 is present in the array).The gorillas had their own permutation q of length n. They suggested that you count the number of pairs of integers l, r (1 <= l <= r <= n) such that \operatorname{MEX}([p_l, p_{l+1}, ..., p_r])=\operatorname{MEX}([q_l, q_{l+1}, ..., q_r]).The \operatorname{MEX} of the sequence is the minimum integer positive number missing from this sequence. For example, \operatorname{MEX}([1, 3]) = 2, \operatorname{MEX}([5]) = 1, \operatorname{MEX}([3, 1, 2, 6]) = 4.You do not want to risk your health, so you will not dare to refuse the gorillas.
Input: ['31 3 22 1 3'] Output:['2', '']
[ 2, 3, 4 ]
For his birthday recently Fedya was given an array a of n integers arranged in a circle, For each pair of neighboring numbers (a_1 and a_2, a_2 and a_3, ..., a_{n - 1} and a_n, a_n and a_1) the absolute difference between them is equal to 1.Let's call a local maximum an element, which is greater than both of its neighboring elements. Also call a local minimum an element, which is less than both of its neighboring elements. Note, that elements a_1 and a_n are neighboring elements.Unfortunately, Fedya lost an array, but he remembered in it the sum of local maximums x and the sum of local minimums y.Given x and y, help Fedya find any matching array of minimum length.
Input: ['43 -24 -42 -15 -3'] Output:['10', '0 1 2 1 0 -1 0 -1 0 1', '16', '-2 -1 -2 -1 0 1 2 3 4 5 4 3 2 1 0 -1 ', '6', '1 0 -1 0 1 0', '16', '2 3 2 1 0 -1 0 -1 0 -1 0 1 2 1 0 1']
[ 3 ]
The famous store "Second Food" sells groceries only two days a month. And the prices in each of days differ. You wanted to buy n kilos of potatoes for a month. You know that on the first day of the month 1 kilo of potatoes costs a coins, and on the second day b coins. In "Second Food" you can buy any integer kilograms of potatoes.Fortunately, "Second Food" has announced a promotion for potatoes, which is valid only on the first day of the month β€” for each m kilos of potatoes you buy, you get 1 kilo as a gift! In other words, you can get m + 1 kilograms by paying for m kilograms.Find the minimum number of coins that you have to spend to buy at least n kilos of potatoes.
Input: ['55 43 15 43 23 43 520 1510 21000000000 9000000001000000000 8'] Output:['9', '10', '9', '135', '888888888900000000', '']
[ 2, 3 ]
The only difference between the easy and the hard version is the constraint on n.You are given an undirected complete graph on n vertices. A complete graph is a graph where each pair of vertices is connected by an edge. You have to paint the edges of the graph into two colors, red and blue (each edge will have one color).A set of vertices S is red-connected if, for every pair of vertices (v_1, v_2) such that v_1 \in S and v_2 \in S, there exists a path from v_1 to v_2 that goes only through red edges and vertices from S. Similarly, a set of vertices S is blue-connected if, for every pair of vertices (v_1, v_2) such that v_1 \in S and v_2 \in S, there exists a path from v_1 to v_2 that goes only through blue edges and vertices from S.You have to paint the graph in such a way that: there is at least one red edge; there is at least one blue edge; for each set of vertices S such that |S| >= 2, S is either red-connected or blue-connected, but not both. Calculate the number of ways to paint the graph, and print it modulo 998244353.
Input: ['3', ''] Output:['6', '']
[ 0 ]
You are given an n * n multiplication table and a positive integer m = m_1 \cdot m_2. A n * n multiplication table is a table with n rows and n columns numbered from 1 to n, where a_{i, j} = i \cdot j.For each divisor d of m, check: does d occur in the table at least once, and if it does, what is the minimum row that contains d.
Input: ['33 72 110 10 156 1 210'] Output:['6 2', '10 0', '8 5', '']
[ 0 ]
You are given n permutations a_1, a_2, ..., a_n, each of length m. Recall that a permutation of length m is a sequence of m distinct integers from 1 to m.Let the beauty of a permutation p_1, p_2, ..., p_m be the largest k such that p_1 = 1, p_2 = 2, ..., p_k = k. If p_1 \neq 1, then the beauty is 0.The product of two permutations p \cdot q is a permutation r such that r_j = q_{p_j}.For each i from 1 to n, print the largest beauty of a permutation a_i \cdot a_j over all j from 1 to n (possibly, i = j).
Input: ['33 42 4 1 31 2 4 32 1 3 42 21 22 18 103 4 9 6 10 2 7 8 1 53 9 1 8 5 7 4 10 2 63 10 1 7 5 9 6 4 2 81 2 3 4 8 6 10 7 9 51 2 3 4 10 6 8 5 7 99 6 1 2 10 4 7 8 3 57 9 3 2 5 6 4 8 1 109 4 3 7 5 6 1 10 8 2'] Output:['1 4 4 ', '2 2 ', '10 8 1 6 8 10 1 7 ', '']
[ 3, 4 ]
You are given a permutation p of length n (a permutation of length n is an array of length n in which each integer from 1 to n occurs exactly once).You can perform the following operation any number of times (possibly zero): choose two different elements x and y and erase them from the permutation; insert the minimum of x and y into the permutation in such a way that it becomes the first element; insert the maximum of x and y into the permutation in such a way that it becomes the last element. For example, if p = [1, 5, 4, 2, 3] and we want to apply the operation to the elements 3 and 5, then after the first step of the operation, the permutation becomes p = [1, 4, 2]; and after we insert the elements, it becomes p = [3, 1, 4, 2, 5].Your task is to calculate the minimum number of operations described above to sort the permutation p in ascending order (i. e. transform p so that p_1 < p_2 < ... < p_n).
Input: ['451 5 4 2 331 2 342 1 4 365 2 4 1 6 3'] Output:['2', '0', '1', '3', '']
[ 0, 2, 3, 4 ]
Eve is a beginner stand-up comedian. Her first show gathered a grand total of two spectators: Alice and Bob.Eve prepared a_1 + a_2 + a_3 + a_4 jokes to tell, grouped by their type: type 1: both Alice and Bob like them; type 2: Alice likes them, but Bob doesn't; type 3: Bob likes them, but Alice doesn't; type 4: neither Alice nor Bob likes them. Initially, both spectators have their mood equal to 0. When a spectator hears a joke he/she likes, his/her mood increases by 1. When a spectator hears a joke he/she doesn't like, his/her mood decreases by 1. If the mood of a spectator becomes negative (strictly below zero), he/she leaves.When someone leaves, Eve gets sad and ends the show. If no one leaves, and Eve is out of jokes, she also ends the show.Thus, Eve wants to arrange her jokes in such a way that the show lasts as long as possible. Help her to calculate the maximum number of jokes she can tell before the show ends.
Input: ['45 0 0 00 0 0 52 5 10 63 0 0 7'] Output:['5', '1', '15', '7', '']
[ 2, 3 ]
Monocarp is playing a computer game. He's going to kill n monsters, the i-th of them has h_i health.Monocarp's character has two spells, either of which he can cast an arbitrary number of times (possibly, zero) and in an arbitrary order: choose exactly two alive monsters and decrease their health by 1; choose a single monster and kill it. When a monster's health becomes 0, it dies.What's the minimum number of spell casts Monocarp should perform in order to kill all monsters?
Input: ['341 2 1 232 4 251 2 3 4 5'] Output:['3', '3', '5', '']
[ 2 ]
The only difference between the easy and hard versions are the locations you can teleport to.Consider the points 0,1,...,n+1 on the number line. There is a teleporter located on each of the points 1,2,...,n. At point i, you can do the following: Move left one unit: it costs 1 coin. Move right one unit: it costs 1 coin. Use a teleporter at point i, if it exists: it costs a_i coins. As a result, you can choose whether to teleport to point 0 or point n+1. Once you use a teleporter, you can't use it again. You have c coins, and you start at point 0. What's the most number of teleporters you can use?
Input: ['105 61 1 1 1 18 32100 52 13 6 9 4 100 351 154 54 3 2 15 92 3 1 4 15 82 3 1 4 14 32 3 4 14 95 4 3 32 147 55 600000000500000000 400000000 300000000 200000000 100000000'] Output:['2', '3', '0', '1', '3', '2', '1', '1', '2', '2', '']
[ 2, 4 ]
The only difference between the easy and hard versions are the locations you can teleport to.Consider the points 0, 1, ..., n on the number line. There is a teleporter located on each of the points 1, 2, ..., n. At point i, you can do the following: Move left one unit: it costs 1 coin. Move right one unit: it costs 1 coin. Use a teleporter at point i, if it exists: it costs a_i coins. As a result, you teleport to point 0. Once you use a teleporter, you can't use it again. You have c coins, and you start at point 0. What's the most number of teleporters you can use?
Input: ['105 61 1 1 1 18 32100 52 13 6 9 4 100 351 154 54 3 2 15 92 3 1 4 15 82 3 1 4 14 32 3 4 14 95 4 3 32 147 55 600000000500000000 400000000 300000000 200000000 100000000'] Output:['2', '2', '0', '1', '2', '2', '1', '1', '1', '2', '']
[ 2 ]
Given an array a_1, a_2, ..., a_n, you need to handle a total of q updates and queries of two types: 1 l r β€” for each index i with l <=q i <=q r, update the value of a_i to the sum of the digits of a_i. 2 x β€” output a_x.
Input: ['35 81 420 69 1434 20231 2 32 22 32 41 2 52 12 32 52 39999 10001 1 22 12 21 112 1'] Output:['6', '15', '1434', '1', '6', '7', '36', '1', '1', '']
[ 0, 4 ]
Given an array a consisting of n elements, find the maximum possible sum the array can have after performing the following operation any number of times: Choose 2 adjacent elements and flip both of their signs. In other words choose an index i such that 1 <=q i <=q n - 1 and assign a_i = -a_i and a_{i+1} = -a_{i+1}.
Input: ['53-1 -1 -151 5 -5 0 231 2 36-1 10 9 8 7 62-1 -1'] Output:['1', '13', '6', '39', '2', '']
[ 2 ]
Let's denote the f(x) function for a string x as the number of distinct characters that the string contains. For example f(\texttt{abc}) = 3, f(\texttt{bbbbb}) = 1, and f(\texttt{babacaba}) = 3.Given a string s, split it into two non-empty strings a and b such that f(a) + f(b) is the maximum possible. In other words, find the maximum possible value of f(a) + f(b) such that a + b = s (the concatenation of string a and string b is equal to string s).
Input: ['52aa7abcabcd5aaaaa10paiumoment4aazz'] Output:['2', '7', '2', '10', '3', '']
[ 0, 2 ]
Timofey came to a famous summer school and found a tree on n vertices. A tree is a connected undirected graph without cycles.Every vertex of this tree, except c_0, is colored white. The vertex c_0 is colored black.Timofey wants to color all the vertices of this tree in black. To do this, he performs n - 1 operations. During the i-th operation, he selects the vertex c_i, which is currently white, and paints it black.Let's call the positivity of tree the minimum distance between all pairs of different black vertices in it. The distance between the vertices v and u is the number of edges on the path from v to u.After each operation, Timofey wants to know the positivity of the current tree.
Input: ['66 64 1 3 5 22 46 55 33 41 34 24 1 33 12 31 410 310 7 6 5 2 9 8 1 41 21 34 54 36 48 79 810 81 87 37 5 1 2 4 61 23 24 53 46 57 69 79 3 1 4 2 6 8 54 18 94 82 67 32 43 55 410 21 8 5 10 6 9 4 3 710 77 83 69 77 64 21 67 59 2'] Output:['3 2 1 1 1 ', '3 1 1 ', '3 2 2 2 2 2 1 1 1 ', '4 2 2 1 1 1 ', '5 1 1 1 1 1 1 1 ', '4 3 2 2 1 1 1 1 1 ', '']
[ 0, 2, 3 ]
Matryoshka is a wooden toy in the form of a painted doll, inside which you can put a similar doll of a smaller size.A set of nesting dolls contains one or more nesting dolls, their sizes are consecutive positive integers. Thus, a set of nesting dolls is described by two numbers: s β€” the size of a smallest nesting doll in a set and m β€” the number of dolls in a set. In other words, the set contains sizes of s, s + 1, ..., s + m - 1 for some integer s and m (s,m > 0).You had one or more sets of nesting dolls. Recently, you found that someone mixed all your sets in one and recorded a sequence of doll sizes β€” integers a_1, a_2, ..., a_n.You do not remember how many sets you had, so you want to find the minimum number of sets that you could initially have.For example, if a given sequence is a=[2, 2, 3, 4, 3, 1]. Initially, there could be 2 sets: the first set consisting of 4 nesting dolls with sizes [1, 2, 3, 4]; a second set consisting of 2 nesting dolls with sizes [2, 3]. According to a given sequence of sizes of nesting dolls a_1, a_2, ..., a_n, determine the minimum number of nesting dolls that can make this sequence.Each set is completely used, so all its nesting dolls are used. Each element of a given sequence must correspond to exactly one doll from some set.
Input: ['1062 2 3 4 3 1511 8 7 10 961000000000 1000000000 1000000000 1000000000 1000000000 100000000081 1 4 4 2 3 2 361 2 3 2 3 4710 11 11 12 12 13 1378 8 9 9 10 10 1184 14 5 15 6 16 7 1785 15 6 14 8 12 9 1154 2 2 3 4'] Output:['2', '1', '6', '2', '2', '2', '2', '2', '4', '3', '']
[ 2 ]
A sequence of n numbers is called permutation if it contains all integers from 1 to n exactly once. For example, the sequences [3, 1, 4, 2], [1] and [2,1] are permutations, but [1,2,1], [0,1] and [1,3,4] β€” are not.Kristina had a permutation p of n elements. She wrote it on the whiteboard n times in such a way that: while writing the permutation at the i-th (1 <= i <= n) time she skipped the element p_i So, she wrote in total n sequences of length n-1 each.For example, suppose Kristina had a permutation p = [4,2,1,3] of length 4. Then she did the following: Wrote the sequence [2, 1, 3], skipping the element p_1=4 from the original permutation. Wrote the sequence [4, 1, 3], skipping the element p_2=2 from the original permutation. Wrote the sequence [4, 2, 3], skipping the element p_3=1 from the original permutation. Wrote the sequence [4, 2, 1], skipping the element p_4=3 from the original permutation. You know all n of sequences that have been written on the whiteboard, but you do not know the order in which they were written. They are given in arbitrary order. Reconstruct the original permutation from them.For example, if you know the sequences [4, 2, 1], [4, 2, 3], [2, 1, 3], [4, 1, 3], then the original permutation will be p = [4, 2, 1, 3].
Input: ['544 2 14 2 32 1 34 1 332 31 31 254 2 1 32 1 3 54 2 3 54 1 3 54 2 1 542 3 41 3 41 2 31 2 432 11 32 3'] Output:['4 2 1 3 ', '1 2 3 ', '4 2 1 3 5 ', '1 2 3 4 ', '2 1 3 ', '']
[ 0, 3 ]
Taisia has n six-sided dice. Each face of the die is marked with a number from 1 to 6, each number from 1 to 6 is used once.Taisia rolls all n dice at the same time and gets a sequence of values a_1, a_2, ..., a_n (1 <= a_i <= 6), where a_i is the value on the upper face of the i-th dice. The sum of this sequence is equal to s.Suddenly, Taisia's pet cat steals exactly one dice with maximum value a_i and calculates the sum of the values on the remaining n-1 dice, which is equal to r.You only know the number of dice n and the values of s, r. Restore a possible sequence a that fulfills the constraints.
Input: ['72 2 12 4 24 9 55 17 113 15 104 4 35 20 15'] Output:['1 1', '2 2 ', '1 2 2 4', '6 4 2 3 2', '5 5 5', '1 1 1 1', '1 4 5 5 5', '']
[ 2, 2, 3 ]
On March 14, the day of the number \pi is celebrated all over the world. This is a very important mathematical constant equal to the ratio of the circumference of a circle to its diameter.Polycarp was told at school that the number \pi is irrational, therefore it has an infinite number of digits in decimal notation. He wanted to prepare for the Day of the number \pi by memorizing this number as accurately as possible.Polycarp wrote out all the digits that he managed to remember. For example, if Polycarp remembered \pi as 3.1415, he wrote out 31415.Polycarp was in a hurry and could have made a mistake, so you decided to check how many first digits of the number \pi Polycarp actually remembers correctly.
Input: ['900034141592653141592653589793238462643383279314203141531415926535827182314159265358979323846264338327'] Output:['0', '1', '0', '0', '3', '5', '12', '0', '30', '']
[ 3 ]
Serval loves Brain Power and his brain power problem.Serval defines that a string T is powerful iff T can be obtained by concatenating some string T' multiple times. Formally speaking, T is powerful iff there exist a string T' and an integer k>=q 2 such that T=\underbrace{T'+T'+...+T'}_{k\text{ times}}For example, gogogo is powerful because it can be obtained by concatenating go three times, but power is not powerful.Serval has a string S consists of lowercase English letters. He is curious about the longest powerful subsequence of S, and he only needs you to find out the length of it. If all the non-empty subsequences of S is not powerful, the answer is considered to be 0.A string a is a subsequence of a string b if a can be obtained from b by the deletion of several (possibly, zero or all) characters.
Input: ['buaa', ''] Output:['2', '']
[ 0, 2 ]
Serval loves playing music games. He meets a problem when playing music games, and he leaves it for you to solve.You are given n positive integers s_1 < s_2 < ... < s_n. f(x) is defined as the number of i (1<=q i<=q n) that exist non-negative integers p_i, q_i such that: s_i=p_i<=ft\lfloor{s_n\over x}\right\rfloor + q_i<=ft\lceil{s_n\over x}\right\rceilFind out \sum_{x=1}^{s_n} x\cdot f(x) modulo 998\,244\,353.As a reminder, \lfloor x\rfloor denotes the maximal integer that is no greater than x, and \lceil x\rceil denotes the minimal integer that is no less than x.
Input: ['431 2 441 2 7 94344208 591000 4779956 540342951633 1661 1741 2134 2221'] Output:['26', '158', '758737625', '12334970', '']
[ 0, 3 ]
Serval has two n-bit binary integer numbers a and b. He wants to share those numbers with Toxel.Since Toxel likes the number b more, Serval decides to change a into b by some (possibly zero) operations. In an operation, Serval can choose any positive integer k between 1 and n, and change a into one of the following number: a\oplus(a\ll k) a\oplus(a\gg k) In other words, the operation moves every bit of a left or right by k positions, where the overflowed bits are removed, and the missing bits are padded with 0. The bitwise XOR of the shift result and the original a is assigned back to a.Serval does not have much time. He wants to perform no more than n operations to change a into b. Please help him to find out an operation sequence, or determine that it is impossible to change a into b in at most n operations. You do not need to minimize the number of operations. In this problem, x\oplus y denotes the bitwise XOR operation of x and y. a\ll k and a\gg k denote the logical left shift and logical right shift.
Input: ['3500111110001113001000'] Output:['2', '3 -2', '0', '-1']
[ 0 ]
Toxel likes arrays. Before traveling to the Paldea region, Serval gave him an array a as a gift. This array has n pairwise distinct elements.In order to get more arrays, Toxel performed m operations with the initial array. In the i-th operation, he modified the p_{i}-th element of the (i-1)-th array to v_{i}, resulting in the i-th array (the initial array a is numbered as 0). During modifications, Toxel guaranteed that the elements of each array are still pairwise distinct after each operation.Finally, Toxel got m+1 arrays and denoted them as A_{0}=a, A_{1},...,A_{m}. For each pair (i,j) (0<= i<j<= m), Toxel defines its value as the number of distinct elements of the concatenation of A_{i} and A_{j}. Now Toxel wonders, what is the sum of the values of all pairs? Please help him to calculate the answer.
Input: ['33 21 2 31 42 51 111 110 104 6 9 12 16 20 2 10 19 71 35 42 172 186 117 18 175 55 52 2'] Output:['13', '1', '705', '']
[ 3 ]
Serval has a string s that only consists of 0 and 1 of length n. The i-th character of s is denoted as s_i, where 1<=q i<=q n.Serval can perform the following operation called Inversion Magic on the string s: Choose an segment [l, r] (1<=q l<=q r<=q n). For l<=q i<=q r, change s_i into 1 if s_i is 0, and change s_i into 0 if s_i is 1. For example, let s be 010100 and the segment [2,5] is chosen. The string s will be 001010 after performing the Inversion Magic.Serval wants to make s a palindrome after performing Inversion Magic exactly once. Help him to determine whether it is possible.A string is a palindrome iff it reads the same backwards as forwards. For example, 010010 is a palindrome but 10111 is not.
Input: ['34100151001070111011'] Output:['Yes', 'Yes', 'No', '']
[ 0 ]
Mocha likes arrays, and Serval gave her an array consisting of positive integers as a gift.Mocha thinks that for an array of positive integers a, it is good iff the greatest common divisor of all the elements in a is no more than its length. And for an array of at least 2 positive integers, it is beautiful iff all of its prefixes whose length is no less than 2 are good. For example: [3,6] is not good, because \gcd(3,6)=3 is greater than its length 2. [1,2,4] is both good and beautiful, because all of its prefixes whose length is no less than 2, which are [1,2] and [1,2,4], are both good. [3,6,1] is good but not beautiful, because [3,6] is not good. Now Mocha gives you the gift array a of n positive integers, and she wants to know whether array a could become beautiful by reordering the elements in a. It is allowed to keep the array a unchanged.
Input: ['623 631 2 433 6 1315 35 21435 10 35 1451261 227821 143 4171 1941'] Output:['No', 'Yes', 'Yes', 'No', 'Yes', 'Yes', '']
[ 0, 3 ]
You are given a tree of n vertices. The vertices are numbered from 1 to n.You will need to assign a weight to each edge. Let the weight of the i-th edge be a_i (1 <=q i <=q n-1). The weight of each edge should be an integer between 0 and 2^{30}-1, inclusive.You are given q conditions. Each condition consists of three integers u, v, and x. This means that the bitwise XOR of all edges on the shortest path from u to v should be x.Find out if there exist a_1, a_2, ..., a_{n-1} that satisfy the given conditions. If yes, print a solution such that a_1 \oplus a_2 \oplus ... \oplus a_{n-1} is the smallest. Here, \oplus denotes the bitwise XOR operation.If there are multiple solutions such that a_1 \oplus a_2 \oplus ... \oplus a_{n-1} is the smallest, print any.
Input: ['4 4', '1 2', '2 3', '3 4', '1 4 3', '2 4 2', '1 3 1', '2 3 1', ''] Output:['No']
[ 2 ]
We play a game with n dots on a number line.The initial coordinate of the i-th dot is x_i. These coordinates are distinct. Every dot starts moving simultaneously with the same constant speed.Each dot moves in the direction of the closest dot (different from itself) until it meets another dot. In the case of a tie, it goes to the left. Two dots meet if they are in the same coordinate, after that, they stop moving.After enough time, every dot stops moving. The result of a game is the number of distinct coordinates where the dots stop.Because this game is too easy, calculate the sum of results when we play the game for every subset of the given n dots that has at least two dots. As the result can be very large, print the sum modulo 10^9+7.
Input: ['4', '1 2 4 6', ''] Output:['11']
[ 0, 3, 4 ]
You are given an integer n. Pair the integers 1 to 2n (i.e. each integer should be in exactly one pair) so that each sum of matched pairs is consecutive and distinct. Formally, let (a_i, b_i) be the pairs that you matched. \{a_1, b_1, a_2, b_2, ..., a_n, b_n\} should be a permutation of \{1, 2, ..., 2n\}. Let the sorted list of \{a_1+b_1, a_2+b_2, ..., a_n+b_n\} be s_1 < s_2 < ... < s_n. We must have s_{i+1}-s_i = 1 for 1 <= i <= n - 1.
Input: ['4', '1', '2', '3', '4', ''] Output:['Yes', '1 2', 'No', 'Yes', '1 6', '3 5', '4 2', 'No', '']
[ 2, 3 ]
The sum of digits of a non-negative integer a is the result of summing up its digits together when written in the decimal system. For example, the sum of digits of 123 is 6 and the sum of digits of 10 is 1. In a formal way, the sum of digits of \displaystyle a=\sum_{i=0}^{\infty} a_i \cdot 10^i, where 0 <=q a_i <=q 9, is defined as \displaystyle\sum_{i=0}^{\infty}{a_i}.Given an integer n, find two non-negative integers x and y which satisfy the following conditions. x+y=n, and the sum of digits of x and the sum of digits of y differ by at most 1. It can be shown that such x and y always exist.
Input: ['5', '1', '161', '67', '1206', '19', ''] Output:['1 0', '67 94', '60 7', '1138 68', '14 5']
[ 2, 3 ]
You are given a sequence a_1, a_2, ..., a_n. Each element of a is 1 or 2.Find out if an integer k exists so that the following conditions are met. 1 <=q k <=q n-1, and a_1 \cdot a_2 \cdot ... \cdot a_k = a_{k+1} \cdot a_{k+2} \cdot ... \cdot a_n. If there exist multiple k that satisfy the given condition, print the smallest.
Input: ['362 2 1 2 1 231 2 141 1 1 1'] Output:['2', '-1', '1', '']
[ 0, 3 ]
You are participating in a Codeforces Round with n problems. You spend exactly one minute to solve each problem, the time it takes to submit a problem can be ignored. You can only solve at most one problem at any time. The contest starts at time 0, so you can make your first submission at any time t >= 1 minutes. Whenever you submit a problem, it is always accepted.The scoring of the i-th problem can be represented by three integers k_i, b_i, and a_i. If you solve it at time t minutes, you get \max(b_i - k_i \cdot t,a_i) points.Your task is to choose an order to solve all these n problems to get the maximum possible score. You can assume the contest is long enough to solve all problems.
Input: ['4410000 1000000000 200610000 1000000000 99992 999991010 10101000000000 1000000000 99999999961 8 19 29 42 14 34 13 12 19 510 12 584 10 14 19 81 14 34 15 62 9 61 11 102 19 124 19 14105 12 75 39 122 39 113 23 155 30 113 17 135 29 143 17 113 36 183 9 8'] Output:['3999961003', '53', '78', '180', '']
[ 4 ]
An edge-weighted tree of n nodes is given with each edge colored in some color. Each node of this tree can be blocked or unblocked, all nodes are unblocked initially.A simple path is a path in a graph that does not have repeating nodes. The length of a path is defined as the sum of weights of all edges on the path.A path is good when it is a simple path consisting of edges of the same color c, all edges of color c are on this path, and every node on the path is unblocked.You need to operate 2 kinds of queries: block a node, unblock a node. After each query, print the maximum length among all good paths. If there are no good paths, print 0.
Input: ['5 4', '4 1 3 4', '5 2 4 4', '3 1 3 2', '1 2 5 1', '0 4', '0 3', '0 2', '1 3', ''] Output:['5', '5', '0', '3', '']
[ 0 ]
A permutation scientist is studying a self-transforming permutation a consisting of n elements a_1,a_2,...,a_n.A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2] are permutations, while [1, 1], [4, 3, 1] are not.The permutation transforms day by day. On each day, each element x becomes a_x, that is, a_x becomes a_{a_x}. Specifically: on the first day, the permutation becomes b, where b_x = a_{a_x}; on the second day, the permutation becomes c, where c_x = b_{b_x}; ... For example, consider permutation a = [2,3,1]. On the first day, it becomes [3,1,2]. On the second day, it becomes [2,3,1].You're given the permutation a' on the k-th day.Define \sigma(x) = a_x, and define f(x) as the minimal positive integer m such that \sigma^m(x) = x, where \sigma^m(x) denotes \underbrace{\sigma(\sigma(... \sigma}_{m \text{ times}}(x) ...)).For example, if a = [2,3,1], then \sigma(1) = 2, \sigma^2(1) = \sigma(\sigma(1)) = \sigma(2) = 3, \sigma^3(1) = \sigma(\sigma(\sigma(1))) = \sigma(3) = 1, so f(1) = 3. And if a=[4,2,1,3], \sigma(2) = 2 so f(2) = 1; \sigma(3) = 1, \sigma^2(3) = 4, \sigma^3(3) = 3 so f(3) = 3.Find the initial permutation a such that \sum\limits^n_{i=1} \dfrac{1}{f(i)} is minimum possible.
Input: ['105 31 2 3 4 57 21 2 3 4 5 6 78 9981 2 3 4 5 6 7 86 16 3 5 4 1 24 84 2 1 39 11 5 4 8 7 6 3 2 95 99999992 3 4 5 17 978432204 6 1 2 7 5 33 10000000002 1 312 38 9 10 1 5 3 11 4 7 6 12 2'] Output:['YES', '2 3 4 1 5', 'YES', '6 2 5 7 1 3 4', 'YES', '2 3 4 5 6 7 8 1', 'YES', '3 1 6 4 2 5', 'YES', '4 2 1 3', 'NO', 'YES', '3 4 5 1 2', 'YES', '2 5 4 6 3 7 1', 'NO', 'YES', '3 7 8 6 5 1 12 10 11 4 2 9', '']
[ 3 ]
You are given an array of exactly n numbers [1,2,3,...,n] along with integers k and x.Partition the array in exactly k non-empty disjoint subsequences such that the bitwise XOR of all numbers in each subsequence is x, and each number is in exactly one subsequence. Notice that there are no constraints on the length of each subsequence.A sequence a is a subsequence of a sequence b if a can be obtained from b by the deletion of several (possibly, zero or all) elements.For example, for n = 15, k = 6, x = 7, the following scheme is valid: [6,10,11], 6 \oplus 10 \oplus 11 = 7, [5,12,14], 5 \oplus 12 \oplus 14 = 7, [3,9,13], 3 \oplus 9 \oplus 13 = 7, [1,2,4], 1 \oplus 2 \oplus 4 = 7, [8,15], 8 \oplus 15 = 7, [7], 7 = 7, where \oplus represents the bitwise XOR operation.The following scheme is invalid, since 8, 15 do not appear: [6,10,11], 6 \oplus 10 \oplus 11 = 7, [5,12,14], 5 \oplus 12 \oplus 14 = 7, [3,9,13], 3 \oplus 9 \oplus 13 = 7, [1,2,4], 1 \oplus 2 \oplus 4 = 7, [7], 7 = 7. The following scheme is invalid, since 3 appears twice, and 1, 2 do not appear: [6,10,11], 6 \oplus 10 \oplus 11 = 7, [5,12,14], 5 \oplus 12 \oplus 14 = 7, [3,9,13], 3 \oplus 9 \oplus 13 = 7, [3,4], 3 \oplus 4 = 7, [8,15], 8 \oplus 15 = 7, [7], 7 = 7.
Input: ['715 6 711 4 55 3 24 1 46 1 711 5 511 6 5'] Output:['YES', '3 6 10 11', '3 5 12 14', '3 3 9 13', '3 1 2 4', '2 8 15', '1 7', 'YES', '2 1 4', '2 2 7', '2 3 6', '5 5 8 9 10 11', 'NO', 'YES', '4 1 2 3 4', 'YES', '6 1 2 3 4 5 6', 'NO', 'NO']
[ 2, 3 ]
RSJ has a sequence a of n integers a_1,a_2, ..., a_n and an integer s. For each of a_2,a_3, ..., a_{n-1}, he chose a pair of non-negative integers x_i and y_i such that x_i+y_i=a_i and (x_i-s) \cdot (y_i-s) >=q 0.Now he is interested in the value F = a_1 \cdot x_2+y_2 \cdot x_3+y_3 \cdot x_4 + ... + y_{n - 2} \cdot x_{n-1}+y_{n-1} \cdot a_n.Please help him find the minimum possible value F he can get by choosing x_i and y_i optimally. It can be shown that there is always at least one valid way to choose them.
Input: ['105 02 0 1 3 45 15 3 4 3 57 27 6 5 4 3 2 15 11 2 3 4 55 21 2 3 4 54 00 1 1 15 54 3 5 6 44 10 2 1 03 99999200000 200000 2000006 81397976 129785 12984 78561 173685 15480'] Output:['0', '18', '32', '11', '14', '0', '16', '0', '40000000000', '2700826806', '']
[ 2, 3 ]
Given an integer n.Consider all pairs of integer arrays a and p of the same length such that n = \prod a_i^{p_i} (i.e. a_1^{p_1}\cdot a_2^{p_2}\cdot...) (a_i>1;p_i>0) and a_i is the product of some (possibly one) distinct prime numbers.For example, for n = 28 = 2^2\cdot 7^1 = 4^1 \cdot 7^1 the array pair a = [2, 7], p = [2, 1] is correct, but the pair of arrays a = [4, 7], p = [1, 1] is not, because 4=2^2 is a product of non-distinct prime numbers.Your task is to find the maximum value of \sum a_i \cdot p_i (i.e. a_1\cdot p_1 + a_2\cdot p_2 + ...) over all possible pairs of arrays a and p. Note that you do not need to minimize or maximize the length of the arrays.
Input: ['71001086413005619210000000002999999018'] Output:['20', '10', '22', '118', '90', '2', '333333009', '']
[ 2, 3 ]
You are given an integer n.Find any pair of integers (x,y) (1<=q x,y<=q n) such that x^y\cdot y+y^x\cdot x = n.
Input: ['537423125020732790'] Output:['-1', '-1', '2 3', '5 5', '3 13', '']
[ 3 ]
A cake assembly line in a bakery was once again optimized, and now n cakes are made at a time! In the last step, each of the n cakes should be covered with chocolate.Consider a side view on the conveyor belt, let it be a number line. The i-th cake occupies the segment [a_i - w, a_i + w] on this line, each pair of these segments does not have common points. Above the conveyor, there are n dispensers, and when a common button is pressed, chocolate from the i-th dispenser will cover the conveyor segment [b_i - h, b_i + h]. Each pair of these segments also does not have common points. Cakes and dispensers corresponding to the first example. The calibration of this conveyor belt part has not yet been performed, so you are to make it. Determine if it's possible to shift the conveyor so that each cake has some chocolate on it, and there is no chocolate outside the cakes. You can assume that the conveyour is long enough, so the cakes never fall. Also note that the button can only be pressed once. In the first example we can shift the cakes as shown in the picture.
Input: ['43 10 565 95 16540 65 1455 2 11 6 11 16 214 9 14 19 243 3 213 22 295 16 254 4 127 36 127 13635 50 141 144'] Output:['YES', 'YES', 'NO', 'YES', '']
[ 0 ]
Alice and Bob are playing an infinite game consisting of sets. Each set consists of rounds. In each round, one of the players wins. The first player to win two rounds in a set wins this set. Thus, a set always ends with the score of 2:0 or 2:1 in favor of one of the players.Let's call a game scenario a finite string s consisting of characters 'a' and 'b'. Consider an infinite string formed with repetitions of string s: sss ... Suppose that Alice and Bob play rounds according to this infinite string, left to right. If a character of the string sss ... is 'a', then Alice wins the round; if it's 'b', Bob wins the round. As soon as one of the players wins two rounds, the set ends in their favor, and a new set starts from the next round.Let's define a_i as the number of sets won by Alice among the first i sets while playing according to the given scenario. Let's also define r as the limit of ratio \frac{a_i}{i} as i \rightarrow \infty. If r > \frac{1}{2}, we'll say that scenario s is winning for Alice. If r = \frac{1}{2}, we'll say that scenario s is tied. If r < \frac{1}{2}, we'll say that scenario s is winning for Bob.You are given a string s consisting of characters 'a', 'b', and '?'. Consider all possible ways of replacing every '?' with 'a' or 'b' to obtain a string consisting only of characters 'a' and 'b'. Count how many of them result in a scenario winning for Alice, how many result in a tied scenario, and how many result in a scenario winning for Bob. Print these three numbers modulo 998\,244\,353.
Input: ['??', ''] Output:['1', '2', '1', '']
[ 0 ]
This is the hard version of the problem. In this version, you need to find the answer for every prefix of the monster array.In a computer game, you are fighting against n monsters. Monster number i has a_i health points, all a_i are integers. A monster is alive while it has at least 1 health point.You can cast spells of two types: Deal 1 damage to any single alive monster of your choice. Deal 1 damage to all alive monsters. If at least one monster dies (ends up with 0 health points) as a result of this action, then repeat it (and keep repeating while at least one monster dies every time). Dealing 1 damage to a monster reduces its health by 1.Spells of type 1 can be cast any number of times, while a spell of type 2 can be cast at most once during the game.For every k = 1, 2, ..., n, answer the following question. Suppose that only the first k monsters, with numbers 1, 2, ..., k, are present in the game. What is the smallest number of times you need to cast spells of type 1 to kill all k monsters?
Input: ['233 1 264 1 5 4 1 1'] Output:['2 1 0', '3 2 4 4 4 4', '']
[ 2 ]
This is the easy version of the problem. In this version, you only need to find the answer once. In this version, hacks are not allowed.In a computer game, you are fighting against n monsters. Monster number i has a_i health points, all a_i are integers. A monster is alive while it has at least 1 health point.You can cast spells of two types: Deal 1 damage to any single alive monster of your choice. Deal 1 damage to all alive monsters. If at least one monster dies (ends up with 0 health points) as a result of this action, then repeat it (and keep repeating while at least one monster dies every time). Dealing 1 damage to a monster reduces its health by 1.Spells of type 1 can be cast any number of times, while a spell of type 2 can be cast at most once during the game.What is the smallest number of times you need to cast spells of type 1 to kill all monsters?
Input: ['233 1 264 1 5 4 1 1'] Output:['0', '4', '']
[ 0, 2 ]
Monocarp and Polycarp are playing a computer game. This game features n bosses for the playing to kill, numbered from 1 to n.They will fight each boss the following way: Monocarp makes k attempts to kill the boss; Polycarp makes k attempts to kill the boss; Monocarp makes k attempts to kill the boss; Polycarp makes k attempts to kill the boss; ... Monocarp kills the i-th boss on his a_i-th attempt. Polycarp kills the i-th boss on his b_i-th attempt. After one of them kills the i-th boss, they move on to the (i+1)-st boss. The attempt counters reset for both of them. Once one of them kills the n-th boss, the game ends.Find all values of k from 1 to n such that Monocarp kills all bosses.
Input: ['331 1 12 3 111141 4 3 23 3 4 1'] Output:['3', '1 2 3 ', '1', '1 ', '2', '2 4 ', '']
[ 0, 3 ]
You are given an array a consisting of n integers.You have to perform the sequence of n-2 operations on this array: during the first operation, you either add a_2 to a_1 and subtract a_2 from a_3, or add a_2 to a_3 and subtract a_2 from a_1; during the second operation, you either add a_3 to a_2 and subtract a_3 from a_4, or add a_3 to a_4 and subtract a_3 from a_2; ... during the last operation, you either add a_{n-1} to a_{n-2} and subtract a_{n-1} from a_n, or add a_{n-1} to a_n and subtract a_{n-1} from a_{n-2}. So, during the i-th operation, you add the value of a_{i+1} to one of its neighbors, and subtract it from the other neighbor.For example, if you have the array [1, 2, 3, 4, 5], one of the possible sequences of operations is: subtract 2 from a_3 and add it to a_1, so the array becomes [3, 2, 1, 4, 5]; subtract 1 from a_2 and add it to a_4, so the array becomes [3, 1, 1, 5, 5]; subtract 5 from a_3 and add it to a_5, so the array becomes [3, 1, -4, 5, 10]. So, the resulting array is [3, 1, -4, 5, 10].An array is reachable if it can be obtained by performing the aforementioned sequence of operations on a. You have to calculate the number of reachable arrays, and print it modulo 998244353.
Input: ['4', '1 1 1 1', ''] Output:['3', '']
[ 0 ]
You are participating in Yet Another Tournament. There are n + 1 participants: you and n other opponents, numbered from 1 to n.Each two participants will play against each other exactly once. If the opponent i plays against the opponent j, he wins if and only if i > j.When the opponent i plays against you, everything becomes a little bit complicated. In order to get a win against opponent i, you need to prepare for the match for at least a_i minutes β€” otherwise, you lose to that opponent.You have m minutes in total to prepare for matches, but you can prepare for only one match at one moment. In other words, if you want to win against opponents p_1, p_2, ..., p_k, you need to spend a_{p_1} + a_{p_2} + ... + a_{p_k} minutes for preparation β€” and if this number is greater than m, you cannot achieve a win against all of these opponents at the same time.The final place of each contestant is equal to the number of contestants with strictly more wins + 1. For example, if 3 contestants have 5 wins each, 1 contestant has 3 wins and 2 contestants have 1 win each, then the first 3 participants will get the 1-st place, the fourth one gets the 4-th place and two last ones get the 5-th place.Calculate the minimum possible place (lower is better) you can achieve if you can't prepare for the matches more than m minutes in total.
Input: ['54 401100 100 200 13 21 2 35 01 1 1 1 14 00 1 1 14 41 2 2 1'] Output:['1', '2', '6', '4', '1', '']
[ 2, 4 ]
For a square matrix of integers of size n * n, let's define its beauty as follows: for each pair of side-adjacent elements x and y, write out the number |x-y|, and then find the number of different numbers among them.For example, for the matrix \begin{pmatrix} 1 & 3\\ 4 & 2 \end{pmatrix} the numbers we consider are |1-3|=2, |1-4|=3, |3-2|=1 and |4-2|=2; there are 3 different numbers among them (2, 3 and 1), which means that its beauty is equal to 3.You are given an integer n. You have to find a matrix of size n * n, where each integer from 1 to n^2 occurs exactly once, such that its beauty is the maximum possible among all such matrices.
Input: ['223'] Output:['1 3', '4 2', '1 3 4', '9 2 7', '5 8 6', '']
[ 3 ]
An array a is called ugly if it contains at least one element which is equal to the sum of all elements before it. If the array is not ugly, it is beautiful.For example: the array [6, 3, 9, 6] is ugly: the element 9 is equal to 6 + 3; the array [5, 5, 7] is ugly: the element 5 (the second one) is equal to 5; the array [8, 4, 10, 14] is beautiful: 8!=0, 4!=8, 10!=8 + 4, 14!=8 + 4 + 10, so there is no element which is equal to the sum of all elements before it. You are given an array a such that 1 <= a_1 <= a_2 <= ... <= a_n <= 100. You have to reorder the elements of a in such a way that the resulting array is beautiful. Note that you are not allowed to insert new elements or erase existing ones, you can only change the order of elements of a. You are allowed to keep the array a unchanged, if it is beautiful.
Input: ['443 3 6 6210 1051 2 3 4 531 4 4'] Output:['YES', '3 6 3 6', 'NO', 'YES', '2 4 1 5 3', 'YES', '1 4 4', '']
[ 3 ]
Vika likes playing with bracket sequences. Today she wants to create a new bracket sequence using the following algorithm. Initially, Vika's sequence is an empty string, and then she will repeat the following actions n times: Choose a place in the current bracket sequence to insert new brackets uniformly at random. If the length of the current sequence is k, then there are k+1 such places: before the first bracket, between the first and the second brackets, ..., after the k-th bracket. In particular, there is one such place in an empty bracket sequence. Choose string "()" with probability p or string ")(" with probability 1 - p and insert it into the chosen place. The length of the bracket sequence will increase by 2. A bracket sequence is called regular if it is possible to obtain a correct arithmetic expression by inserting characters '+' and '1' into it. For example, sequences "(())()", "()", and "(()(()))" are regular, while ")(", "(()", and "(()))(" are not.Vika wants to know the probability that her bracket sequence will be a regular one at the end. Help her and find this probability modulo 998\,244\,353 (see Output section).
Input: ['1 7500', ''] Output:['249561089', '']
[ 3 ]
You have a rectangular grid of height 2 and width 10^9 consisting of unit cells. There are n rectangles placed on this grid, and the borders of these rectangles pass along cell borders. The i-th rectangle covers all cells in rows from u_i to d_i inclusive and columns from l_i to r_i inclusive (1 <= u_i <= d_i <= 2; 1 <= l_i <= r_i <= 10^9). The initial rectangles can intersect, be nested, and coincide arbitrarily.You should either remove each rectangle, or replace it with any of its non-empty subrectangles. In the latter case, the new subrectangle must lie inside the initial rectangle, and its borders must still pass along cell borders. In particular, it is allowed for the subrectangle to be equal to the initial rectangle.After that replacement, no two (non-removed) rectangles are allowed to have common cells, and the total area covered with the new rectangles must be as large as possible. Illustration for the first test case. The initial rectangles are given at the top, the new rectangles are given at the bottom. Rectangle number 4 is removed.
Input: ['851 2 2 42 4 2 81 4 2 71 2 1 21 9 1 1021 1 1 101 5 1 1521 1 1 101 1 1 1051 3 1 71 3 1 81 1 1 41 2 1 71 10 1 1121 1 2 101 5 1 821 5 2 101 2 1 721 5 2 102 2 2 1552 6 2 71 4 2 51 5 1 91 7 2 101 2 1 6'] Output:['15', '1 2 2 4', '2 5 2 8', '1 5 1 7', '0 0 0 0', '1 9 1 10', '15', '1 1 1 10', '1 11 1 15', '10', '1 1 1 10', '0 0 0 0', '10', '0 0 0 0', '1 8 1 8', '1 1 1 4', '1 5 1 7', '1 10 1 11', '20', '1 1 2 10', '0 0 0 0', '15', '1 5 2 10', '1 2 1 4', '20', '1 5 1 10', '2 2 2 15', '16', '2 6 2 6', '2 4 2 5', '0 0 0 0', '1 7 2 10', '1 2 1 6', '']
[ 0, 2, 4 ]
You are given a set a_1, a_2, ..., a_n of distinct positive integers.We define the squareness of an integer x as the number of perfect squares among the numbers a_1 + x, a_2 + x, ..., a_n + x.Find the maximum squareness among all integers x between 0 and 10^{18}, inclusive.Perfect squares are integers of the form t^2, where t is a non-negative integer. The smallest perfect squares are 0, 1, 4, 9, 16, ....
Input: ['451 2 3 4 551 6 13 22 97110052 5 10 17 26'] Output:['2', '5', '1', '2', '']
[ 0, 3 ]
Let's call a string balanced if all characters that are present in it appear the same number of times. For example, "coder", "appall", and "ttttttt" are balanced, while "wowwow" and "codeforces" are not.You are given a string s of length n consisting of lowercase English letters. Find a balanced string t of the same length n consisting of lowercase English letters that is different from the string s in as few positions as possible. In other words, the number of indices i such that s_i!=t_i should be as small as possible.
Input: ['45hello10codeforces5eevee6appall'] Output:['1', 'helno', '2', 'codefofced', '1', 'eeeee', '0', 'appall', '']
[ 0, 2 ]
A company of n people is planning a visit to the cinema. Every person can either go to the cinema or not. That depends on how many other people will go. Specifically, every person i said: "I want to go to the cinema if and only if at least a_i other people will go, not counting myself". That means that person i will become sad if: they go to the cinema, and strictly less than a_i other people go; or they don't go to the cinema, and at least a_i other people go. In how many ways can a set of people going to the cinema be chosen so that nobody becomes sad?
Input: ['421 170 1 2 3 4 5 686 0 3 3 6 7 2 753 0 0 3 3'] Output:['2', '1', '3', '2', '']
[ 0, 2 ]
Vika's house has a room in a shape of a rectangular parallelepiped (also known as a rectangular cuboid). Its floor is a rectangle of size w * d, and the ceiling is right above at the constant height of h. Let's introduce a coordinate system on the floor so that its corners are at points (0, 0), (w, 0), (w, d), and (0, d).A laptop is standing on the floor at point (a, b). A projector is hanging on the ceiling right above point (f, g). Vika wants to connect the laptop and the projector with a cable in such a way that the cable always goes along the walls, ceiling, or floor (i. e. does not go inside the cuboid). Additionally, the cable should always run parallel to one of the cuboid's edges (i. e. it can not go diagonally).What is the minimum length of a cable that can connect the laptop to the projector? Illustration for the first test case. One of the optimal ways to put the cable is shown in green.
Input: ['555 20 2923 10 18 320 10 51 5 2 515 15 47 13 10 102 1000 21 1 1 99910 4 107 1 2 1'] Output:['47', '8', '14', '1002', '17', '']
[ 3 ]
Today is an important day for chef Tonio β€” an auditor has arrived in his hometown of Morioh. He has also arrived at Tonio's restaurant and ordered dessert. Tonio has not been prepared for this turn of events.As you know, dessert is a string of lowercase English letters. Tonio remembered the rule of desserts β€” a string s of length n. Any dessert t is delicious if the number of occurrences of t in s as a substring is divisible by the length of t.Now Tonio wants to know the number of delicious substrings of s. If the substring occurs several times in the string s, then all occurrences must be taken into account.
Input: ['7', 'abacaba', ''] Output:['11', '']
[ 3, 4 ]
One day Kira found n friends from Morioh and decided to gather them around a table to have a peaceful conversation. The height of friend i is equal to a_i. It so happened that the height of each of the friends is unique.Unfortunately, there were only 3 chairs in Kira's house, and obviously, it will not be possible to seat all friends! So, Kira has to invite only 3 of his friends.But everything is not so simple! If the heights of the lowest and the tallest of the invited friends are not coprime, then the friends will play tricks on each other, which will greatly anger Kira.Kira became interested, how many ways are there to choose 3 of his friends so that they don't play tricks? Two ways are considered different if there is a friend invited in one way, but not in the other.Formally, if Kira invites friends i, j, and k, then the following should be true: \gcd(\min(a_i, a_j, a_k), \max(a_i, a_j, a_k)) = 1, where \gcd(x, y) denotes the greatest common divisor (GCD) of the numbers x and y.Kira is not very strong in computer science, so he asks you to count the number of ways to invide friends.
Input: ['3', '1 2 3', ''] Output:['1', '']
[ 0 ]
Josuke received a huge undirected weighted complete^ graph G as a gift from his grandfather. The graph contains 10^{18} vertices. The peculiarity of the gift is that the weight of the edge between the different vertices u and v is equal to \gcd(u, v)^\ddagger. Josuke decided to experiment and make a new graph G'. To do this, he chooses two integers l <= r and deletes all vertices except such vertices v that l <= v <= r, and also deletes all the edges except between the remaining vertices.Now Josuke is wondering how many different weights are there in G'. Since their count turned out to be huge, he asks for your help.^ A complete graph is a simple undirected graph in which every pair of distinct vertices is adjacent.^\ddagger \gcd(x, y) denotes the greatest common divisor (GCD) of the numbers x and y.
Input: ['72 416 242 61 103 32562 2568125 100090'] Output:['2', '6', '3', '5', '0', '5', '50045', '']
[ 0, 3, 4 ]
This is an interactive problem.Kira has a hidden positive integer n, and Hayato needs to guess it.Initially, Kira gives Hayato the value \mathrm{cnt} β€” the number of unit bits in the binary notation of n. To guess n, Hayato can only do operations of one kind: choose an integer x and subtract it from n. Note that after each operation, the number n changes. Kira doesn't like bad requests, so if Hayato tries to subtract a number x greater than n, he will lose to Kira. After each operation, Kira gives Hayato the updated value \mathrm{cnt} β€” the number of unit bits in the binary notation of the updated value of n.Kira doesn't have much patience, so Hayato must guess the original value of n after no more than 30 operations.Since Hayato is in elementary school, he asks for your help. Write a program that guesses the number n. Kira is an honest person, so he chooses the initial number n before all operations and does not change it afterward.
Input: ['3', '', '1', '', '0', '', '1', '', '1', '', '0', '', '2', '', '1', '', '0'] Output:['- 1', '', '! 1', '', '- 1', '', '- 1', '', '! 2', '', '- 2', '', '- 1', '', '! 3']
[ 4 ]
While at Kira's house, Josuke saw a piece of paper on the table with a task written on it.The task sounded as follows. There is an array a of length n. On this array, do the following: select an integer k > 1; split the array into k subsegments ^; calculate the sum in each of k subsegments and write these sums to another array b (where the sum of the subsegment (l, r) is {\sum_{j = l}^{r}a_j}); the final score of such a split will be \gcd(b_1, b_2, ..., b_k)^\ddagger. The task is to find such a partition that the score is maximum possible. Josuke is interested in this task but is not strong in computer science. Help him to find the maximum possible score.^ A division of an array into k subsegments is k pairs of numbers (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that l_i <= r_i and for every 1 <= j <= k - 1 l_{j + 1} = r_j + 1, also l_1 = 1 and r_k = n. These pairs represent the subsegments.^\ddagger \gcd(b_1, b_2, ..., b_k) stands for the greatest common divisor (GCD) of the array b.
Input: ['642 2 1 321 231 4 561 2 1 1 1 31012 30 37 88 12 78 89 17 2 1267 7 7 7 7 7'] Output:['4', '1', '5', '3', '1', '21', '']
[ 0, 2, 3 ]
Today Hayato came home from school with homework.In the assignment, Hayato was given an array a of length n. The task was to find 3 numbers in this array whose sum is odd. At school, he claimed that there are such 3 numbers, but Hayato was not sure, so he asked you for help.Answer if there are such three numbers, and if so, output indices i, j, and k such that a_i + a_j + a_k is odd.The odd numbers are integers that are not divisible by 2: 1, 3, 5, and so on.
Input: ['631 1 141 1 2 231 2 351 4 5 1 242 6 2 455 6 3 2 1'] Output:['YES', '1 2 3', 'YES', '3 4 1', 'NO', 'YES', '1 3 4', 'NO', 'YES', '1 3 5', '']
[ 2 ]
Iron and Werewolf are participating in a chess Olympiad, so they want to practice team building. They gathered n players, where n is a power of 2, and they will play sports. Iron and Werewolf are among those n people.One of the sports is tug of war. For each 1<=q i <=q n, the i-th player has strength s_i. Elimination rounds will be held until only one player remains β€” we call that player the absolute winner.In each round: Assume that m>1 players are still in the game, where m is a power of 2. The m players are split into two teams of equal sizes (i. e., with m/2 players in each team). The strength of a team is the sum of the strengths of its players. If the teams have equal strengths, Iron chooses who wins; otherwise, the stronger team wins. Every player in the losing team is eliminated, so m/2 players remain. Iron already knows each player's strength and is wondering who can become the absolute winner and who can't if he may choose how the teams will be formed in each round, as well as the winning team in case of equal strengths.
Input: ['4', '60 32 59 87', ''] Output:['1001', '']
[ 0 ]
This is an interactive problem.Anya has gathered n chess experts numbered from 1 to n for which the following properties hold: For any pair of players one of the players wins every game against the other (and no draws ever occur); Transitivity does not necessarily hold β€” it might happen that A always beats B, B always beats C and C always beats A. Anya does not know, for each pair, who is the player who beats the other.To organize a tournament, Anya hosts n-1 games. In each game, she chooses two players. One of them wins and stays, while the other one is disqualified. After all the games are hosted only one player will remain. A player is said to be a candidate master if they can win a tournament (notice that the winner of a tournament may depend on the players selected by Anya in the n-1 games).Since Anya is a curious girl, she is interested in finding the candidate masters. Unfortunately, she does not have much time. To speed up the process, she will organize up to 2n simuls (short for "simultaneous exhibition", in which one player plays against many).In one simul, Anya chooses exactly one player who will play against some (at least one) of the other players. The chosen player wins all games they would win in a regular game, and the same holds for losses. After the simul finishes, Anya is only told the total number of games won by the chosen player (but not which ones). Nobody is disqualified during a simul.Can you help Anya host simuls and determine the candidate masters?The winning players in each pair could be changed between the simuls, but only in a way that preserves the results of all previous simuls. These changes may depend on your queries.
Input: ['3', '', '1', '', '1', '', '1', ''] Output:['', '? 1 010', '', '? 2 001', '', '? 3 100', '', '! 111', '']
[ 2 ]
Boris thinks that chess is a tedious game. So he left his tournament early and went to a barber shop as his hair was a bit messy.His current hair can be described by an array a_1,a_2,..., a_n, where a_i is the height of the hair standing at position i. His desired haircut can be described by an array b_1,b_2,..., b_n in a similar fashion.The barber has m razors. Each has its own size and can be used at most once. In one operation, he chooses a razor and cuts a segment of Boris's hair. More formally, an operation is: Choose any razor which hasn't been used before, let its size be x; Choose a segment [l,r] (1<=q l <=q r <=q n); Set a_i := \min (a_i,x) for each l<=q i <=q r; Notice that some razors might have equal sizes β€” the barber can choose some size x only as many times as the number of razors with size x. He may perform as many operations as he wants, as long as any razor is used at most once and a_i = b_i for each 1 <=q i <=q n at the end. He does not have to use all razors.Can you determine whether the barber can give Boris his desired haircut?
Input: ['733 3 32 1 221 263 4 4 6 3 43 1 2 3 2 333 2 3101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10101 2 3 4 5 6 7 8 9 1031 1 11 1 2124 2 4 3 1 5 6 3 5 6 2 1137 9 4 5 3 3 3 6 8 10 3 2 55 3 1 5 3 2 2 5 8 5 1 1 581 5 3 5 4 2 3 1137 9 4 5 3 3 3 6 8 10 3 2 55 3 1 5 3 2 2 5 8 5 1 1 571 5 3 4 2 3 1319747843 2736467 9385783972039844 2039844 203984412039844'] Output:['YES', 'NO', 'YES', 'NO', 'YES', 'NO', 'YES', '']
[ 2 ]
Baltic, a famous chess player who is also a mathematician, has an array a_1,a_2, ..., a_n, and he can perform the following operation several (possibly 0) times: Choose some index i (1 <=q i <=q n); multiply a_i with -1, that is, set a_i := -a_i. Baltic's favorite number is m, and he wants a_1 + a_2 + \cdots + a_m to be the smallest of all non-empty prefix sums. More formally, for each k = 1,2,..., n it should hold that a_1 + a_2 + \cdots + a_k >=q a_1 + a_2 + \cdots + a_m.Please note that multiple smallest prefix sums may exist and that it is only required that a_1 + a_2 + \cdots + a_m is one of them.Help Baltic find the minimum number of operations required to make a_1 + a_2 + \cdots + a_m the least of all prefix sums. It can be shown that a valid sequence of operations always exists.
Input: ['64 3-1 -2 -3 -44 31 2 3 41 115 5-2 3 -5 1 -205 2-2 3 -5 -5 -2010 4345875723 -48 384678321 -375635768 -35867853 -35863586 -358683842 -81725678 38576 -357865873'] Output:['1', '1', '0', '0', '3', '4', '']
[ 2 ]
MKnez wants to construct an array s_1,s_2, ... , s_n satisfying the following conditions: Each element is an integer number different from 0; For each pair of adjacent elements their sum is equal to the sum of the whole array. More formally, s_i \neq 0 must hold for each 1 <=q i <=q n. Moreover, it must hold that s_1 + s_2 + \cdots + s_n = s_i + s_{i+1} for each 1 <=q i < n.Help MKnez to construct an array with these properties or determine that it does not exist.
Input: ['2', '2', '3', ''] Output:['YES', '9 5', 'NO', '']
[ 3 ]
Thalia is a Legendary Grandmaster in chess. She has n trophies in a line numbered from 1 to n (from left to right) and a lamp standing next to each of them (the lamps are numbered as the trophies).A lamp can be directed either to the left or to the right, and it illuminates all trophies in that direction (but not the one it is next to). More formally, Thalia has a string s consisting only of characters 'L' and 'R' which represents the lamps' current directions. The lamp i illuminates: trophies 1,2,..., i-1 if s_i is 'L'; trophies i+1,i+2,..., n if s_i is 'R'. She can perform the following operation at most once: Choose an index i (1 <=q i < n); Swap the lamps i and i+1 (without changing their directions). That is, swap s_i with s_{i+1}. Thalia asked you to illuminate all her trophies (make each trophy illuminated by at least one lamp), or to tell her that it is impossible to do so. If it is possible, you can choose to perform an operation or to do nothing. Notice that lamps cannot change direction, it is only allowed to swap adjacent ones.
Input: ['6', '2', 'LL', '2', 'LR', '2', 'RL', '2', 'RR', '7', 'LLRLLLR', '7', 'RRLRRRL', ''] Output:['-1', '1', '0', '-1', '3', '6', '']
[ 2 ]
You are given a rooted tree consisting of n vertices numbered from 1 to n. Vertex 1 is the root of the tree. Each vertex has an integer value. The value of i-th vertex is a_i. You can do the following operation at most k times. Choose a vertex v that has not been chosen before and an integer x such that x is a common divisor of the values of all vertices of the subtree of v. Multiply by x the value of each vertex in the subtree of v. What is the maximum possible value of the root node 1 after at most k operations? Formally, you have to maximize the value of a_1.A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root. The subtree of a node u is the set of all nodes y such that the simple path from y to the root passes through u. Note that u is in the subtree of u.
Input: ['25 224 12 24 6 121 21 32 42 55 324 12 24 6 121 21 32 42 5'] Output:['288', '576', '']
[ 3 ]
Recently, a tree has fallen on Bob's head from the sky. The tree has n nodes. Each node u of the tree has an integer number a_u written on it. But the tree has no fixed root, as it has fallen from the sky.Bob is currently studying the tree. To add some twist, Alice proposes a game. First, Bob chooses some node r to be the root of the tree. After that, Alice chooses a node v and tells him. Bob then can pick one or more nodes from the subtree of v. His score will be the bitwise XOR of all the values written on the nodes picked by him. Bob has to find the maximum score he can achieve for the given r and v.As Bob is not a good problem-solver, he asks you to help him find the answer. Can you help him? You need to find the answers for several combinations of r and v for the same tree.Recall that a tree is a connected undirected graph without cycles. The subtree of a node u is the set of all nodes y such that the simple path from y to the root passes through u. Note that u is in the subtree of u.
Input: ['3612 12 8 25 6 11 51 22 62 32 434 23 51 223 81 242 22 11 21 133 8 71 22 322 22 1'] Output:['15', '6', '29', '11', '3', '8', '11', '15', '3', '']
[ 3 ]
You are given two binary strings a and b of length n. In each move, the string a is modified in the following way. An index i (1 <=q i <=q n) is chosen uniformly at random. The character a_i will be flipped. That is, if a_i is 0, it becomes 1, and if a_i is 1, it becomes 0. What is the expected number of moves required to make both strings equal for the first time?A binary string is a string, in which the character is either \tt{0} or \tt{1}.
Input: ['41012000041000111050100110111'] Output:['1', '0', '665496254', '665496277', '']
[ 3 ]
You have a string a and a string b. Both of the strings have length n. There are at most 10 different characters in the string a. You also have a set Q. Initially, the set Q is empty. You can apply the following operation on the string a any number of times: Choose an index i (1<=q i <=q n) and a lowercase English letter c. Add a_i to the set Q and then replace a_i with c. For example, Let the string a be "\tt{abecca}". We can do the following operations: In the first operation, if you choose i = 3 and c = \tt{x}, the character a_3 = \tt{e} will be added to the set Q. So, the set Q will be \{\tt{e}\}, and the string a will be "\tt{ab\underline{x}cca}". In the second operation, if you choose i = 6 and c = \tt{s}, the character a_6 = \tt{a} will be added to the set Q. So, the set Q will be \{\tt{e}, \tt{a}\}, and the string a will be "\tt{abxcc\underline{s}}". You can apply any number of operations on a, but in the end, the set Q should contain at most k different characters. Under this constraint, you have to maximize the number of integer pairs (l, r) (1<=q l<=q r <=q n) such that a[l,r] = b[l,r]. Here, s[l,r] means the substring of string s starting at index l (inclusively) and ending at index r (inclusively).
Input: ['63 1abcabd3 0abcabd3 1xbbxcd4 1abcdaxcb3 10abcabd10 3lkwhbahuqaqoiujoncjb'] Output:['6', '3', '6', '6', '6', '11', '']
[ 0 ]
You are given a permutation p of length n, an array of m distinct integers a_1, a_2, ..., a_m (1 <= a_i <= n), and an integer d.Let \mathrm{pos}(x) be the index of x in the permutation p. The array a is not good if \mathrm{pos}(a_{i}) < \mathrm{pos}(a_{i + 1}) <= \mathrm{pos}(a_{i}) + d for all 1 <= i < m. For example, with the permutation p = [4, 2, 1, 3, 6, 5] and d = 2: a = [2, 3, 6] is a not good array. a = [2, 6, 5] is good because \mathrm{pos}(a_1) = 2, \mathrm{pos}(a_2) = 5, so the condition \mathrm{pos}(a_2) <= \mathrm{pos}(a_1) + d is not satisfied. a = [1, 6, 3] is good because \mathrm{pos}(a_2) = 5, \mathrm{pos}(a_3) = 4, so the condition \mathrm{pos}(a_2) < \mathrm{pos}(a_3) is not satisfied. In one move, you can swap two adjacent elements of the permutation p. What is the minimum number of moves needed such that the array a becomes good? It can be shown that there always exists a sequence of moves so that the array a becomes good.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: ['54 2 21 2 3 41 35 2 45 4 3 2 15 25 3 33 4 1 5 23 1 22 2 11 22 16 2 41 2 3 4 5 62 5'] Output:['1', '3', '2', '0', '2', '']
[ 2, 3 ]