question_text
stringlengths
2
3.82k
input_outputs
stringlengths
23
941
algo_tags
sequence
This is the hard version of the problem. The only difference between the simple and hard versions is that in this version u can take any possible value.As is known, Omsk is the capital of Berland. Like any capital, Omsk has a well-developed metro system. The Omsk metro consists of a certain number of stations connected by tunnels, and between any two stations there is exactly one path that passes through each of the tunnels no more than once. In other words, the metro is a tree.To develop the metro and attract residents, the following system is used in Omsk. Each station has its own weight x \in \{-1, 1\}. If the station has a weight of -1, then when the station is visited by an Omsk resident, a fee of 1 burle is charged. If the weight of the station is 1, then the Omsk resident is rewarded with 1 burle.Omsk Metro currently has only one station with number 1 and weight x = 1. Every day, one of the following events occurs: A new station with weight x is added to the station with number v_i, and it is assigned a number that is one greater than the number of existing stations. Alex, who lives in Omsk, wonders: is there a subsegment (possibly empty) of the path between vertices u and v such that, by traveling along it, exactly k burles can be earned (if k < 0, this means that k burles will have to be spent on travel). In other words, Alex is interested in whether there is such a subsegment of the path that the sum of the weights of the vertices in it is equal to k. Note that the subsegment can be empty, and then the sum is equal to 0. You are a friend of Alex, so your task is to answer Alex's questions.Subsegment — continuous sequence of elements.
Input: ['18+ 1 -1? 1 1 2? 1 2 1+ 1 1? 1 3 -1? 1 1 1? 1 3 2? 1 1 0'] Output:['NO', 'YES', 'NO', 'YES', 'YES', 'YES', '']
[ 3 ]
This is the simple version of the problem. The only difference between the simple and hard versions is that in this version u = 1.As is known, Omsk is the capital of Berland. Like any capital, Omsk has a well-developed metro system. The Omsk metro consists of a certain number of stations connected by tunnels, and between any two stations there is exactly one path that passes through each of the tunnels no more than once. In other words, the metro is a tree.To develop the metro and attract residents, the following system is used in Omsk. Each station has its own weight x \in \{-1, 1\}. If the station has a weight of -1, then when the station is visited by an Omsk resident, a fee of 1 burle is charged. If the weight of the station is 1, then the Omsk resident is rewarded with 1 burle.Omsk Metro currently has only one station with number 1 and weight x = 1. Every day, one of the following events occurs: A new station with weight x is added to the station with number v_i, and it is assigned a number that is one greater than the number of existing stations. Alex, who lives in Omsk, wonders: is there a subsegment (possibly empty) of the path between vertices u and v such that, by traveling along it, exactly k burles can be earned (if k < 0, this means that k burles will have to be spent on travel). In other words, Alex is interested in whether there is such a subsegment of the path that the sum of the weights of the vertices in it is equal to k. Note that the subsegment can be empty, and then the sum is equal to 0. You are a friend of Alex, so your task is to answer Alex's questions.Subsegment — continuous sequence of elements.
Input: ['18+ 1 -1? 1 1 2? 1 2 1+ 1 1? 1 3 -1? 1 1 1? 1 3 2? 1 1 0'] Output:['NO', 'YES', 'NO', 'YES', 'YES', 'YES', '']
[ 2, 3 ]
You are given an array a consisting of n zeros. You are also given a set of m not necessarily different segments. Each segment is defined by two numbers l_i and r_i (1 <= l_i <= r_i <= n) and represents a subarray a_{l_i}, a_{l_i+1}, ..., a_{r_i} of the array a.Let's call the segment l_i, r_i beautiful if the number of ones on this segment is strictly greater than the number of zeros. For example, if a = [1, 0, 1, 0, 1], then the segment [1, 5] is beautiful (the number of ones is 3, the number of zeros is 2), but the segment [3, 4] is not is beautiful (the number of ones is 1, the number of zeros is 1).You also have q changes. For each change you are given the number 1 <= x <= n, which means that you must assign an element a_x the value 1.You have to find the first change after which at least one of m given segments becomes beautiful, or report that none of them is beautiful after processing all q changes.
Input: ['65 51 24 51 51 32 45531244 21 14 42235 21 51 5421345 21 51 35412355 51 51 51 51 51 431433 22 21 33231'] Output:['3', '-1', '3', '3', '3', '1', '']
[ 0, 4 ]
Timofey has an apple tree growing in his garden; it is a rooted tree of n vertices with the root in vertex 1 (the vertices are numbered from 1 to n). A tree is a connected graph without loops and multiple edges.This tree is very unusual — it grows with its root upwards. However, it's quite normal for programmer's trees.The apple tree is quite young, so only two apples will grow on it. Apples will grow in certain vertices (these vertices may be the same). After the apples grow, Timofey starts shaking the apple tree until the apples fall. Each time Timofey shakes the apple tree, the following happens to each of the apples:Let the apple now be at vertex u. If a vertex u has a child, the apple moves to it (if there are several such vertices, the apple can move to any of them). Otherwise, the apple falls from the tree. It can be shown that after a finite time, both apples will fall from the tree.Timofey has q assumptions in which vertices apples can grow. He assumes that apples can grow in vertices x and y, and wants to know the number of pairs of vertices (a, b) from which apples can fall from the tree, where a — the vertex from which an apple from vertex x will fall, b — the vertex from which an apple from vertex y will fall. Help him do this.
Input: ['251 23 45 33 243 45 14 41 331 21 331 12 33 1'] Output:['2', '2', '1', '4', '4', '1', '2', '']
[ 3 ]
Vanya really likes math. One day when he was solving another math problem, he came up with an interesting tree. This tree is built as follows.Initially, the tree has only one vertex with the number 1 — the root of the tree. Then, Vanya adds two children to it, assigning them consecutive numbers — 2 and 3, respectively. After that, he will add children to the vertices in increasing order of their numbers, starting from 2, assigning their children the minimum unused indices. As a result, Vanya will have an infinite tree with the root in the vertex 1, where each vertex will have exactly two children, and the vertex numbers will be arranged sequentially by layers. Part of Vanya's tree. Vanya wondered what the sum of the vertex numbers on the path from the vertex with number 1 to the vertex with number n in such a tree is equal to. Since Vanya doesn't like counting, he asked you to help him find this sum.
Input: ['63103711000000000000000015'] Output:['4', '18', '71', '1', '19999999999999980', '26', '']
[ 3 ]
Today Alex was brought array a_1, a_2, ..., a_n of length n. He can apply as many operations as he wants (including zero operations) to change the array elements.In 1 operation Alex can choose any l and r such that 1 <=q l <=q r <=q n, and multiply all elements of the array from l to r inclusive by -1. In other words, Alex can replace the subarray [a_l, a_{l + 1}, ..., a_r] by [-a_l, -a_{l + 1}, ..., -a_r] in 1 operation.For example, let n = 5, the array is [1, -2, 0, 3, -1], l = 2 and r = 4, then after the operation the array will be [1, 2, 0, -3, -1].Alex is late for school, so you should help him find the maximum possible sum of numbers in the array, which can be obtained by making any number of operations, as well as the minimum number of operations that must be done for this.
Input: ['56-1 7 -4 -2 5 -88-1 0 0 -2 1 0 -3 052 -1 0 -3 -750 -17 0 1 04-1 0 -2 -1'] Output:['27 3', '7 2', '13 1', '18 1', '4 1', '']
[ 2, 3 ]
Sasha found an array a consisting of n integers and asked you to paint elements.You have to paint each element of the array. You can use as many colors as you want, but each element should be painted into exactly one color, and for each color, there should be at least one element of that color.The cost of one color is the value of \max(S) - \min(S), where S is the sequence of elements of that color. The cost of the whole coloring is the sum of costs over all colors.For example, suppose you have an array a = [\color{red}{1}, \color{red}{5}, \color{blue}{6}, \color{blue}{3}, \color{red}{4}], and you painted its elements into two colors as follows: elements on positions 1, 2 and 5 have color 1; elements on positions 3 and 4 have color 2. Then: the cost of the color 1 is \max([1, 5, 4]) - \min([1, 5, 4]) = 5 - 1 = 4; the cost of the color 2 is \max([6, 3]) - \min([6, 3]) = 6 - 3 = 3; the total cost of the coloring is 7. For the given array a, you have to calculate the maximum possible cost of the coloring.
Input: ['651 5 6 3 41541 6 3 961 13 9 3 7 242 2 2 254 5 2 2 3'] Output:['7', '0', '11', '23', '0', '5', '']
[ 2 ]
bright, sunny and innocent......Tenzing has a beautiful necklace. The necklace consists of n pearls numbered from 1 to n with a string connecting pearls i and (i \text{ mod } n)+1 for all 1 <=q i <=q n.One day Tenzing wants to cut the necklace into several parts by cutting some strings. But for each connected part of the necklace, there should not be more than k pearls. The time needed to cut each string may not be the same. Tenzing needs to spend a_i minutes cutting the string between pearls i and (i \text{ mod } n)+1.Tenzing wants to know the minimum time in minutes to cut the necklace such that each connected part will not have more than k pearls.
Input: ['45 21 1 1 1 15 21 2 3 4 56 34 2 5 1 3 310 32 5 6 5 2 1 7 9 7 2'] Output:['3', '7', '5', '15', '']
[ 2 ]
There are n uniform random real variables between 0 and 1, inclusive, which are denoted as x_1, x_2, ..., x_n.Tenzing has m conditions. Each condition has the form of x_i+x_j<= 1 or x_i+x_j>= 1.Tenzing wants to know the probability that all the conditions are satisfied, modulo 998~244~353.Formally, let M = 998~244~353. It can be shown that the answer can be expressed as an irreducible fraction \frac{p}{q}, where p and q are integers and q \not \equiv 0 \pmod{M}. Output the integer equal to p \cdot q^{-1} \bmod M. In other words, output the integer x that 0 <= x < M and x \cdot q \equiv p \pmod{M}.
Input: ['3 2', '0 1 2', '1 3 3', ''] Output:['748683265', '']
[ 3 ]
Yet another random problem.Tenzing has an array a of length n and an integer v.Tenzing will perform the following operation m times: Choose an integer i such that 1 <=q i <=q n uniformly at random. For all j such that i <=q j <=q n, set a_j := a_j + v. Tenzing wants to know the expected value of \prod_{i=1}^n a_i after performing the m operations, modulo 10^9+7.Formally, let M = 10^9+7. It can be shown that the answer can be expressed as an irreducible fraction \frac{p}{q}, where p and q are integers and q \not \equiv 0 \pmod{M}. Output the integer equal to p \cdot q^{-1} \bmod M. In other words, output the integer x that 0 <= x < M and x \cdot q \equiv p \pmod{M}.
Input: ['2 2 5', '2 2', ''] Output:['84', '']
[ 3 ]
Tenzing has an undirected tree of n vertices.Define the value of a tree with black and white vertices in the following way. The value of an edge is the absolute difference between the number of black nodes in the two components of the tree after deleting the edge. The value of the tree is the sum of values over all edges.For all k such that 0 <=q k <=q n, Tenzing wants to know the maximum value of the tree when k vertices are painted black and n-k vertices are painted white.
Input: ['4', '1 2', '3 2', '2 4', ''] Output:['0 3 4 5 6 ', '']
[ 2 ]
There are n pairwise-distinct points and a line x+y=k on a two-dimensional plane. The i-th point is at (x_i,y_i). All points have non-negative coordinates and are strictly below the line. Alternatively, 0 <=q x_i,y_i, x_i+y_i < k.Tenzing wants to erase all the points. He can perform the following two operations: Draw triangle: Tenzing will choose two non-negative integers a, b that satisfy a+b<k, then all points inside the triangle formed by lines x=a, y=b and x+y=k will be erased. It can be shown that this triangle is an isosceles right triangle. Let the side lengths of the triangle be l, l and \sqrt 2 l respectively. Then, the cost of this operation is l \cdot A.The blue area of the following picture describes the triangle with a=1,b=1 with cost =1\cdot A. Erase a specific point: Tenzing will choose an integer i that satisfies 1 <=q i <=q n and erase the point i. The cost of this operation is c_i.Help Tenzing find the minimum cost to erase all of the points.
Input: ['4 6 1', '1 2 1', '2 1 1', '1 1 1', '3 2 6', ''] Output:['4', '']
[ 2 ]
Tell a story about me and my animal friends.Tenzing has n animal friends. He numbers them from 1 to n.One day Tenzing wants to play with his animal friends. To do so, Tenzing will host several games.In one game, he will choose a set S which is a subset of \{1,2,3,...,n\} and choose an integer t. Then, he will play the game with the animals in S for t minutes.But there are some restrictions: Tenzing loves friend 1 very much, so 1 must be an element of S. Tenzing doesn't like friend n, so n must not be an element of S. There are m additional restrictions. The i-th special restriction is described by integers u_i, v_i and y_i, suppose x is the total time that exactly one of u_i and v_i is playing with Tenzing. Tenzing must ensure that x is less or equal to y_i. Otherwise, there will be unhappiness. Tenzing wants to know the maximum total time that he can play with his animal friends. Please find out the maximum total time that Tenzing can play with his animal friends and a way to organize the games that achieves this maximum total time, or report that he can play with his animal friends for an infinite amount of time. Also, Tenzing does not want to host so many games, so he will host at most n^2 games.
Input: ['5 4', '1 3 2', '1 4 2', '2 3 1', '2 5 1', ''] Output:['4 4', '10000 1', '10010 1', '10100 1', '11110 1', '']
[ 2 ]
Tenzing received 3n books from his fans. The books are arranged in 3 stacks with n books in each stack. Each book has a non-negative integer difficulty rating.Tenzing wants to read some (possibly zero) books. At first, his knowledge is 0.To read the books, Tenzing will choose a non-empty stack, read the book on the top of the stack, and then discard the book. If Tenzing's knowledge is currently u, then his knowledge will become u|v after reading a book with difficulty rating v. Here | denotes the bitwise OR operation. Note that Tenzing can stop reading books whenever he wants.Tenzing's favourite number is x. Can you help Tenzing check if it is possible for his knowledge to become x?
Input: ['35 71 2 3 4 55 4 3 2 11 3 5 7 95 23 2 3 4 55 4 3 2 13 3 5 7 93 01 2 33 2 12 2 2'] Output:['Yes', 'No', 'Yes', '']
[ 2, 3 ]
Tsondu always runs first! ! !Tsondu and Tenzing are playing a card game. Tsondu has n monsters with ability values a_1, a_2, ..., a_n while Tenzing has m monsters with ability values b_1, b_2, ..., b_m.Tsondu and Tenzing take turns making moves, with Tsondu going first. In each move, the current player chooses two monsters: one on their side and one on the other side. Then, these monsters will fight each other. Suppose the ability values for the chosen monsters are x and y respectively, then the ability values of the monsters will become x-y and y-x respectively. If the ability value of any monster is smaller than or equal to 0, the monster dies.The game ends when at least one player has no monsters left alive. The winner is the player with at least one monster left alive. If both players have no monsters left alive, the game ends in a draw.Find the result of the game when both players play optimally.
Input: ['61 391 2 32 31 21 1 13 21 2 31 13 31 1 12 2 210 101 2 3 3 2 2 1 1 2 23 3 3 3 2 1 1 1 1 110 101 2 3 4 5 6 7 8 9 106 7 8 9 10 11 1 1 1 1'] Output:['Tsondu', 'Draw', 'Tsondu', 'Tenzing', 'Draw', 'Draw', '']
[ 3 ]
There is a square matrix, consisting of n rows and n columns of cells, both numbered from 1 to n. The cells are colored white or black. Cells from 1 to a_i are black, and cells from a_i+1 to n are white, in the i-th column.You want to place m integers in the matrix, from 1 to m. There are two rules: each cell should contain at most one integer; black cells should not contain integers. The beauty of the matrix is the number of such j that j+1 is written in the same row, in the next column as j (in the neighbouring cell to the right).What's the maximum possible beauty of the matrix?
Input: ['630 0 0942 0 3 1542 0 3 1642 0 3 110100 2 2 1 5 10 3 4 1 120110'] Output:['6', '3', '4', '4', '16', '0', '']
[ 2, 3 ]
Two segments [l_1, r_1] and [l_2, r_2] intersect if there exists at least one x such that l_1 <= x <= r_1 and l_2 <= x <= r_2.An array of segments [[l_1, r_1], [l_2, r_2], ..., [l_k, r_k]] is called beautiful if k is even, and is possible to split the elements of this array into \frac{k}{2} pairs in such a way that: every element of the array belongs to exactly one of the pairs; segments in each pair intersect with each other; segments in different pairs do not intersect. For example, the array [[2, 4], [9, 12], [2, 4], [7, 7], [10, 13], [6, 8]] is beautiful, since it is possible to form 3 pairs as follows: the first element of the array (segment [2, 4]) and the third element of the array (segment [2, 4]); the second element of the array (segment [9, 12]) and the fifth element of the array (segment [10, 13]); the fourth element of the array (segment [7, 7]) and the sixth element of the array (segment [6, 8]). As you can see, the segments in each pair intersect, and no segments from different pairs intersect.You are given an array of n segments [[l_1, r_1], [l_2, r_2], ..., [l_n, r_n]]. You have to remove the minimum possible number of elements from this array so that the resulting array is beautiful.
Input: ['372 49 122 47 74 810 136 852 22 80 101 25 641 12 23 34 4'] Output:['1', '3', '4', '']
[ 2 ]
No, not "random" numbers.Ranom digits are denoted by uppercase Latin letters from A to E. Moreover, the value of the letter A is 1, B is 10, C is 100, D is 1000, E is 10000.A Ranom number is a sequence of Ranom digits. The value of the Ranom number is calculated as follows: the values of all digits are summed up, but some digits are taken with negative signs: a digit is taken with negative sign if there is a digit with a strictly greater value to the right of it (not necessarily immediately after it); otherwise, that digit is taken with a positive sign.For example, the value of the Ranom number DAAABDCA is 1000 - 1 - 1 - 1 - 10 + 1000 + 100 + 1 = 2088.You are given a Ranom number. You can change no more than one digit in it. Calculate the maximum possible value of the resulting number.
Input: ['4DAAABDCAABABCDEEDCBADDDDAAADDABECD'] Output:['11088', '10010', '31000', '15886', '']
[ 0, 2, 3 ]
The only difference between easy and hard versions is the maximum number of queries. In this version, you are allowed to ask at most 1000 queries.This is an interactive problem.You are playing a game. The circle is divided into n sectors, sectors are numbered from 1 to n in some order. You are in the adjacent room and do not know either the number of sectors or their numbers. There is also an arrow that initially points to some sector. Initially, the host tells you the number of the sector to which the arrow points. After that, you can ask the host to move the arrow k sectors counterclockwise or clockwise at most 1000 times. And each time you are told the number of the sector to which the arrow points.Your task is to determine the integer n — the number of sectors in at most 1000 queries.It is guaranteed that 1 <= n <= 10^6.
Input: ['1', '', '5', '', '6', '', '7', '', '2', '', '10', '', '9', '', '8', '', '4', '', '3', '', '1'] Output:['+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '! 10']
[ 3 ]
The only difference between easy and hard versions is the maximum number of queries. In this version, you are allowed to ask at most 2023 queries.This is an interactive problem.You are playing a game. The circle is divided into n sectors, sectors are numbered from 1 to n in some order. You are in the adjacent room and do not know either the number of sectors or their numbers. There is also an arrow that initially points to some sector. Initially, the host tells you the number of the sector to which the arrow points. After that, you can ask the host to move the arrow k sectors counterclockwise or clockwise at most 2023 times. And each time you are told the number of the sector to which the arrow points.Your task is to determine the integer n — the number of sectors in at most 2023 queries.It is guaranteed that 1 <= n <= 10^6.
Input: ['1', '', '5', '', '6', '', '7', '', '2', '', '10', '', '9', '', '8', '', '4', '', '3', '', '1'] Output:['+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '+ 1', '', '! 10']
[ 3 ]
Tema is playing a very interesting computer game.During the next mission, Tema's character found himself on an unfamiliar planet. Unlike Earth, this planet is flat and can be represented as an n * m rectangle.Tema's character is located at the point with coordinates (0, 0). In order to successfully complete the mission, he needs to reach the point with coordinates (n, m) alive.Let the character of the computer game be located at the coordinate (i, j). Every second, starting from the first, Tema can: either use vertical hyperjump technology, after which his character will end up at coordinate (i + 1, j) at the end of the second; or use horizontal hyperjump technology, after which his character will end up at coordinate (i, j + 1) at the end of the second; or Tema can choose not to make a hyperjump, in which case his character will not move during this second; The aliens that inhabit this planet are very dangerous and hostile. Therefore, they will shoot from their railguns r times.Each shot completely penetrates one coordinate vertically or horizontally. If the character is in the line of its impact at the time of the shot (at the end of the second), he dies.Since Tema looked at the game's source code, he knows complete information about each shot — the time, the penetrated coordinate, and the direction of the shot.What is the minimum time for the character to reach the desired point? If he is doomed to die and cannot reach the point with coordinates (n, m), output -1.
Input: ['51 341 2 02 2 13 2 24 1 13 362 1 02 1 12 1 22 2 02 2 12 2 22 137 1 22 1 17 2 12 259 1 23 2 05 1 24 2 27 1 04 676 1 212 1 34 1 017 2 31 2 616 2 63 2 4'] Output:['5', '-1', '3', '6', '10', '']
[ 0 ]
In a small town, there is a workshop specializing in woodwork. Since the town is small, only three carvers work there.Soon, a wooden toy festival is planned in the town. The workshop employees want to prepare for it.They know that n people will come to the workshop with a request to make a wooden toy. People are different and may want different toys. For simplicity, let's denote the pattern of the toy that the i-th person wants as a_i (1 <= a_i <= 10^9).Each of the carvers can choose an integer pattern x (1 <= x <= 10^9) in advance, different carvers can choose different patterns. x is the integer. During the preparation for the festival, the carvers will perfectly work out the technique of making the toy of the chosen pattern, which will allow them to cut it out of wood instantly. To make a toy of pattern y for a carver who has chosen pattern x, it will take |x - y| time, because the more the toy resembles the one he can make instantly, the faster the carver will cope with the work.On the day of the festival, when the next person comes to the workshop with a request to make a wooden toy, the carvers can choose who will take on the job. At the same time, the carvers are very skilled people and can work on orders for different people simultaneously.Since people don't like to wait, the carvers want to choose patterns for preparation in such a way that the maximum waiting time over all people is as small as possible.Output the best maximum waiting time that the carvers can achieve.
Input: ['561 7 7 9 9 965 4 2 1 30 60914 19 37 59 1 4 4 98 731263 10 1 17 15 11'] Output:['0', '2', '13', '0', '1', '']
[ 2, 4 ]
Dima Vatrushin is a math teacher at school. He was sent on vacation for n days for his good work. Dima has long dreamed of going to a ski resort, so he wants to allocate several consecutive days and go skiing. Since the vacation requires careful preparation, he will only go for at least k days.You are given an array a containing the weather forecast at the resort. That is, on the i-th day, the temperature will be a_i degrees.Dima was born in Siberia, so he can go on vacation only if the temperature does not rise above q degrees throughout the vacation.Unfortunately, Dima was so absorbed in abstract algebra that he forgot how to count. He asks you to help him and count the number of ways to choose vacation dates at the resort.
Input: ['73 1 15-5 0 -105 3 -338 12 9 0 54 3 1212 12 10 154 1 -50 -1 2 55 5 03 -1 4 -5 -31 1 556 1 30 3 -2 5 -4 -4'] Output:['6', '0', '1', '0', '0', '1', '9', '']
[ 3 ]
Once upon a time, Toma found himself in a binary cafe. It is a very popular and unusual place.The cafe offers visitors k different delicious desserts. The desserts are numbered from 0 to k-1. The cost of the i-th dessert is 2^i coins, because it is a binary cafe! Toma is willing to spend no more than n coins on tasting desserts. At the same time, he is not interested in buying any dessert more than once, because one is enough to evaluate the taste.In how many different ways can he buy several desserts (possibly zero) for tasting?
Input: ['51 22 12 210 2179 100'] Output:['2', '2', '3', '4', '180', '']
[ 3 ]
This is an interactive problem.Consider the following game for two players: Initially, an array of integers a_1, a_2, ..., a_n of length n is written on blackboard. Game consists of rounds. On each round, the following happens: The first player selects any i such that a_i \gt 0. If there is no such i, the first player loses the game (the second player wins) and game ends. The second player selects any j \neq i such that a_j \gt 0. If there is no such j, the second player loses the game (the first player wins) and game ends. Let d = \min(a_i, a_j). The values of a_i and a_j are simultaneously decreased by d and the next round starts. It can be shown that game always ends after the finite number of rounds.You have to select which player you will play for (first or second) and win the game.
Input: ['4', '10 4 6 3', '', '', '3', '', '1', '', '0'] Output:['', '', 'First', '1', '', '2', '', '4']
[ 2 ]
You have n lamps, numbered by integers from 1 to n. Each lamp i has two integer parameters a_i and b_i.At each moment each lamp is in one of three states: it may be turned on, turned off, or broken.Initially all lamps are turned off. In one operation you can select one lamp that is turned off and turn it on (you can't turn on broken lamps). You receive b_i points for turning lamp i on. The following happens after each performed operation: Let's denote the number of lamps that are turned on as x (broken lamps do not count). All lamps i such that a_i <= x simultaneously break, whether they were turned on or off. Please note that broken lamps never count as turned on and that after a turned on lamp breaks, you still keep points received for turning it on.You can perform an arbitrary number of operations.Find the maximum number of points you can get.
Input: ['442 21 61 101 1353 43 12 53 23 361 23 41 43 43 52 311 1'] Output:['15', '14', '20', '1', '']
[ 2 ]
You are given two integers n and k.An array a_1, a_2, ..., a_n of length n, consisting of zeroes and ones is good if for all integers i from 1 to n both of the following conditions are satisfied: at least \lceil \frac{i}{k} \rceil of the first i elements of a are equal to 1, at least \lceil \frac{i}{k} \rceil of the last i elements of a are equal to 1. Here, \lceil \frac{i}{k} \rceil denotes the result of division of i by k, rounded up. For example, \lceil \frac{6}{3} \rceil = 2, \lceil \frac{11}{5} \rceil = \lceil 2.2 \rceil = 3 and \lceil \frac{7}{4} \rceil = \lceil 1.75 \rceil = 2.Find the minimum possible number of ones in a good array.
Input: ['73 25 29 37 110 49 58 8'] Output:['2', '3', '4', '7', '4', '3', '2', '']
[ 2, 3 ]
This is an interactive problem.There is an n by n grid of conveyor belts, in positions (1, 1) through (n, n) of a coordinate plane. Every other square in the plane is empty. Each conveyor belt can be configured to move boxes up ('^'), down ('v'), left ('<'), or right ('>'). If a box moves onto an empty square, it stops moving.However, one of the n^2 belts is stuck, and will always move boxes in the same direction, no matter how it is configured. Your goal is to perform a series of tests to determine which conveyor belt is stuck, and the direction in which it sends items.To achieve this, you can perform up to 25 tests. In each test, you assign a direction to all n^2 belts, place a box on top of one of them, and then turn all of the conveyors on. One possible result of a query with n=4. In this case, the box starts at (2, 2). If there were no stuck conveyor, it would end up at (5, 4), but because of the stuck '>' conveyor at (3, 3), it enters an infinite loop.The conveyors move the box around too quickly for you to see, so the only information you receive from a test is whether the box eventually stopped moving, and if so, the coordinates of its final position.
Input: ['3', '', '', '', '', '-1 -1', '', '', '', '', '0 2'] Output:['', '? 2 2', '>><', '>>v', '^<<', '', '? 1 1', '>><', '>>v', '^<<', '', '! 1 2 ^']
[ 4 ]
You are given an array a of n integers, where all elements a_i lie in the range [1, k]. How many different arrays b of m integers, where all elements b_i lie in the range [1, k], contain a as a subsequence? Two arrays are considered different if they differ in at least one position.A sequence x is a subsequence of a sequence y if x can be obtained from y by the deletion of several (possibly, zero or all) elements.Since the answer may be large, print it modulo 10^9 + 7.
Input: ['71 1000000 113 4 31 2 25 7 81 2 3 4 16 6 1818 2 2 5 2 161 10 218 10 12345671 1 2 1 2 2 2 15 1000000000 1000000000525785549 816356460 108064697 194447117 725595511'] Output:['1', '9', '1079', '1', '1023', '906241579', '232432822', '']
[ 3 ]
There is a string s of length n consisting of the characters '(' and ')'. You are walking on this string. You start by standing on top of the first character of s, and you want to make a sequence of moves such that you end on the n-th character. In one step, you can move one space to the left (if you are not standing on the first character), or one space to the right (if you are not standing on the last character). You may not stay in the same place, however you may visit any character, including the first and last character, any number of times.At each point in time, you write down the character you are currently standing on. We say the string is walkable if there exists some sequence of moves that take you from the first character to the last character, such that the string you write down is a regular bracket sequence.A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.One possible valid walk on s=\mathtt{(())()))}. The red dot indicates your current position, and the red string indicates the string you have written down. Note that the red string is a regular bracket sequence at the end of the process.You are given q queries. Each query flips the value of a character from '(' to ')' or vice versa. After each query, determine whether the string is walkable.Queries are cumulative, so the effects of each query carry on to future queries.
Input: ['10 9', '(())()()))', '9', '7', '2', '6', '3', '6', '7', '4', '8', ''] Output:['YES', 'YES', 'NO', 'NO', 'YES', 'NO', 'YES', 'NO', 'NO', '']
[ 2 ]
You are given integers n and m. Fill an n by m grid with the integers 1 through n\cdot m, in such a way that for any two adjacent cells in the grid, the absolute difference of the values in those cells is not a prime number. Two cells in the grid are considered adjacent if they share a side. It can be shown that under the given constraints, there is always a solution.
Input: ['34 45 76 4'] Output:['16 7 1 9', '12 8 2 3', '13 4 10 11', '14 5 6 15', '', '29 23 17 9 5 6 2', '33 27 21 15 11 7 1', '32 31 25 19 20 16 10', '26 30 24 18 14 8 4', '35 34 28 22 13 12 3', '', ' 2 3 7 11', ' 8 9 1 10', '17 13 5 4', '18 14 6 12', '19 23 15 21', '20 24 16 22', '']
[ 3 ]
You are given a permutation p of size n. You want to minimize the number of subarrays of p that are permutations. In order to do so, you must perform the following operation exactly once: Select integers i, j, where 1 <= i, j <= n, then Swap p_i and p_j. For example, if p = [5, 1, 4, 2, 3] and we choose i = 2, j = 3, the resulting array will be [5, 4, 1, 2, 3]. If instead we choose i = j = 5, the resulting array will be [5, 1, 4, 2, 3].Which choice of i and j will minimize the number of subarrays that are permutations?A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).An array a is a subarray of an array b if a can be obtained from b by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input: ['831 2 331 3 251 3 2 5 464 5 6 1 2 398 7 6 3 2 1 4 5 9107 10 5 1 9 8 3 2 6 4108 5 10 9 2 1 3 4 6 7102 3 5 7 10 1 8 6 4 9'] Output:['2 3', '1 1', '5 2', '1 4', '9 5', '8 8', '6 10', '5 4', '']
[ 3 ]
Two integers were written on a blackboard. After that, the following step was carried out n-2 times: Select any two integers on the board, and write the absolute value of their difference on the board. After this process was complete, the list of n integers was shuffled. You are given the final list. Recover one of the initial two numbers. You do not need to recover the other one.You are guaranteed that the input can be generated using the above process.
Input: ['939 2 7315 -4 114-9 1 11 -1053 0 0 0 378 16 8 0 8 16 840 0 0 01027 1 24 28 2 -1 26 25 28 276600000000 800000000 0 -200000000 1000000000 80000000030 -1000000000 1000000000'] Output:['9', '11', '-9', '3', '8', '0', '-1', '600000000', '0', '']
[ 3 ]
Berland Intercollegiate Contest has just finished. Monocarp and Polycarp, as the jury, are going to conduct an editorial. Unfortunately, the time is limited, since they have to finish before the closing ceremony.There were n problems in the contest. The problems are numbered from 1 to n. The editorial for the i-th problem takes a_i minutes. Monocarp and Polycarp are going to conduct an editorial for exactly k of the problems.The editorial goes as follows. They have a full problemset of n problems before them, in order. They remove n - k problems without changing the order of the remaining k problems. Then, Monocarp takes some prefix of these k problems (possibly, an empty one or all problems). Polycarp takes the remaining suffix of them. After that, they go to different rooms and conduct editorials for their problems in parallel. So, the editorial takes as much time as the longer of these two does.Please, help Monocarp and Polycarp to choose the problems and the split in such a way that the editorial finishes as early as possible. Print the duration of the editorial.
Input: ['65 41 10 1 1 15 31 20 5 15 35 31 20 3 15 510 610 8 20 14 3 8 6 4 16 1110 59 9 2 13 15 19 4 9 13 121 11'] Output:['2', '6', '5', '21', '18', '1', '']
[ 2, 4 ]
A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example: the bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); the bracket sequences ")(", "(" and ")" are not. A bracket sequence is called beautiful if one of the following conditions is satisfied: it is a regular bracket sequence; if the order of the characters in this sequence is reversed, it becomes a regular bracket sequence. For example, the bracket sequences "()()", "(())", ")))(((", "))()((" are beautiful.You are given a bracket sequence s. You have to color it in such a way that: every bracket is colored into one color; for every color, there is at least one bracket colored into that color; for every color, if you write down the sequence of brackets having that color in the order they appear, you will get a beautiful bracket sequence. Color the given bracket sequence s into the minimum number of colors according to these constraints, or report that it is impossible.
Input: ['48((())))(4(())4))((3(()'] Output:['2', '2 2 2 1 2 2 2 1', '1', '1 1 1 1', '1', '1 1 1 1', '-1', '']
[ 2 ]
You are given a string s consisting of the characters 0, 1 and/or ?. Let's call it a pattern.Let's say that the binary string (a string where each character is either 0 or 1) matches the pattern if you can replace each character ? with 0 or 1 (for each character, the choice is independent) so that the strings become equal. For example, 0010 matches ?01?, but 010 doesn't match 1??, ??, or ????.Let's define the cost of the binary string as the minimum number of operations of the form "reverse an arbitrary contiguous substring of the string" required to sort the string in non-descending order.You have to find a binary string with the minimum possible cost among those that match the given pattern. If there are multiple answers, print any of them.
Input: ['4??01?101001??10?0?1?10?10'] Output:['00011', '10100', '111101', '011110010', '']
[ 2 ]
You are given a string s of length n, where each character is either < or >.An array a consisting of n+1 elements is compatible with the string s if, for every i from 1 to n, the character s_i represents the result of comparing a_i and a_{i+1}, i. e.: s_i is < if and only if a_i < a_{i+1}; s_i is > if and only if a_i > a_{i+1}. For example, the array [1, 2, 5, 4, 2] is compatible with the string <<>>. There are other arrays with are compatible with that string, for example, [13, 37, 42, 37, 13].The cost of the array is the number of different elements in it. For example, the cost of [1, 2, 5, 4, 2] is 4; the cost of [13, 37, 42, 37, 13] is 3.You have to calculate the minimum cost among all arrays which are compatible with the given string s.
Input: ['44<<>>4>><<5>>>>>7<><><><'] Output:['3', '3', '6', '2', '']
[ 2 ]
You are given two integers x and k. Grasshopper starts in a point 0 on an OX axis. In one move, it can jump some integer distance, that is not divisible by k, to the left or to the right.What's the smallest number of moves it takes the grasshopper to reach point x? What are these moves? If there are multiple answers, print any of them.
Input: ['310 210 33 4'] Output:['2', '7 3', '1', '10', '1', '3', '']
[ 3 ]
In many, many years, far, far away, there will be a launch of the first flight to Mars. To celebrate the success, n astrophysicists working on the project will be given bonuses of a total value of k gold coins.You have to distribute the money among the astrophysicists, and to make it easier, you have to assign bonuses in silver coins. Each gold coin is worth g silver coins, so you have to distribute all k \cdot g silver coins among n people.Unfortunately, the company has some financial troubles right now. Therefore, instead of paying the number of silver coins written on the bonus, they decided to round this amount to the nearest integer number of gold coins.The rounding procedure is as follows. If an astrophysicist has bonus equal to x silver coins, and we denote r = x \bmod g, then: If r >=q \lceil \frac{g}{2} \rceil, the astrophysicist receives x + (g - r) silver coins; Otherwise, an astrophysicists receives x - r silver coins. Note that due to rounding, the total sum of actually paid money is not, in general, equal to k \cdot g silver coins. The operation a \bmod b denotes the remainder of the division of a by b. Sum of values before rounding has to be equal to k \cdot g silver coins, but some workers can be assigned 0 silver coins.You aim to distribute the bonuses so that the company saves as many silver coins due to rounding as possible. Please note that there is always a distribution in which the company spends no more than k \cdot g silver coins.
Input: ['53 3 1002 1 1491 2 1336 16 673 8 22'] Output:['100', '0', '26', '72', '176', '']
[ 2, 3 ]
The rebels have been crushed in the most recent battle with the imperial forces, but there is a ray of new hope.Meanwhile, on one of the conquered planets, Luke was getting ready for an illegal street race (which should come as no surprise, given his family history). Luke arrived at the finish line with 88 miles per hour on his speedometer. After getting out of the car, he was greeted by a new reality. It turns out that the battle has not happened yet and will start in exactly k hours.The rebels have placed a single battleship on each of the n planets. m unidirectional wormholes connect the planets. Traversing each wormhole takes exactly one hour. Generals of the Imperium have planned the battle precisely, but their troops cannot dynamically adapt to changing circumstances. Because of this, it is enough for the rebels to move some ships around before the battle to confuse the enemy, secure victory and change the galaxy's fate.Owing to numerous strategical considerations, which we now omit, the rebels would like to choose two ships that will switch places so that both of them will be on the move for the whole time (exactly k hours). In other words, rebels look for two planets, x and y, such that paths of length k exist from x to y and from y to x.Because of the limited fuel supply, choosing one ship would also be acceptable. This ship should fly through the wormholes for k hours and then return to its initial planet.How many ways are there to choose the ships for completing the mission?
Input: ['7 8 346', '1 2', '1 3', '2 4', '3 4', '4 5', '5 1', '6 7', '7 6', ''] Output:['5', '']
[ 3 ]
Famous worldwide astrophysicist Mleil waGrasse Tysok recently read about the existence of twin galaxy clusters. Before he shares this knowledge with the broader audience in his podcast called S.tarT-ok, he wants to prove their presence on his own. Mleil is aware that the vastness of the universe is astounding (almost as astounding as his observation skills) and decides to try his luck and find some new pair of twin clusters.To do so, he used his TLEscope to observe a part of the night sky that was not yet examined by humanity in which there are exactly 2^{k + 1} galaxies in a row. i-th of them consist of exactly 0 <= g_i < 4^k stars.A galaxy cluster is any non-empty contiguous segment of galaxies. Moreover, its' trait is said to be equal to the bitwise XOR of all values g_i within this range.Two galaxy clusters are considered twins if and only if they have the same traits and their corresponding segments are disjoint.Write a program that, for many scenarios, will read a description of a night sky part observed by Mleil and outputs a location of two intervals belonging to some twin clusters pair, or a single value -1 if no such pair exists.
Input: ['424 15 0 7 11 8 3 210 1 2 300 0315 63 57 39 61 25 42 61 50 41 27 41 56 23 17 27'] Output:['2 4 6 6', '2 2 3 4', '1 1 2 2', '1 1 4 10', '']
[ 0, 3 ]
n people indexed with integers from 1 to n came to take part in a lottery. Each received a ticket with an integer from 0 to m.In a lottery, one integer called target is drawn uniformly from 0 to m. k tickets (or less, if there are not enough participants) with the closest numbers to the target are declared the winners. In case of a draw, a ticket belonging to the person with a smaller index is declared a winner.Bytek decided to take part in the lottery. He knows the values on the tickets of all previous participants. He can pick whatever value he wants on his ticket, but unfortunately, as he is the last one to receive it, he is indexed with an integer n + 1. Bytek wants to win the lottery. Thus, he wants to know what he should pick to maximize the chance of winning. He wants to know the smallest integer in case there are many such integers. Your task is to find it and calculate his chance of winning.
Input: ['3 6 2', '1 4 5', ''] Output:['4 2', '']
[ 0, 2, 3, 4 ]
Consider all equalities of form a + b = c, where a has A digits, b has B digits, and c has C digits. All the numbers are positive integers and are written without leading zeroes. Find the k-th lexicographically smallest equality when written as a string like above or determine that it does not exist.For example, the first three equalities satisfying A = 1, B = 1, C = 2 are 1 + 9 = 10, 2 + 8 = 10, 2 + 9 = 11. An equality s is lexicographically smaller than an equality t with the same lengths of the numbers if and only if the following holds: in the first position where s and t differ, the equality s has a smaller digit than the corresponding digit in t.
Input: ['71 1 1 92 2 3 12 2 1 11 5 6 421 6 6 100000005 5 6 30315688156 6 6 1000000000000'] Output:['2 + 1 = 3', '10 + 90 = 100', '-1', '9 + 99996 = 100005', '-1', '78506 + 28543 = 107049', '-1', '']
[ 0, 3 ]
Recently, Polycarp was given an unusual typewriter as a gift! Unfortunately, the typewriter was defective and had a rather strange design.The typewriter consists of n cells numbered from left to right from 1 to n, and a carriage that moves over them. The typewriter cells contain n distinct integers from 1 to n, and each cell i initially contains the integer p_i. Before all actions, the carriage is at cell number 1 and there is nothing in its buffer storage. The cell on which the carriage is located is called the current cell. The carriage can perform five types of operations: Take the integer from the current cell, if it is not empty, and put it in the carriage buffer, if it is empty (this buffer can contain no more than one integer). Put the integer from the carriage buffer, if it is not empty, into the current cell, if it is empty. Swap the number in the carriage buffer with the number in the current cell, if both the buffer and the cell contain integers. Move the carriage from the current cell i to cell i + 1 (if i < n), while the integer in the buffer is preserved. Reset the carriage, i.e. move it to cell number 1, while the integer in the buffer is preserved. Polycarp was very interested in this typewriter, so he asks you to help him understand it and will ask you q queries of three types: Perform a cyclic shift of the sequence p to the left by k_j. Perform a cyclic shift of the sequence p to the right by k_j. Reverse the sequence p. Before and after each query, Polycarp wants to know what minimum number of carriage resets is needed for the current sequence in order to distribute the numbers to their cells (so that the number i ends up in cell number i).Note that Polycarp only wants to know the minimum number of carriage resets required to arrange the numbers in their places, but he does not actually distribute them.Help Polycarp find the answers to his queries!
Input: ['3', '2 3 1', '0', ''] Output:['1', '']
[ 0, 3 ]
You are given an array a of length n. A positive integer x is called good if it is impossible to find a subsegment of the array such that the least common multiple of all its elements is equal to x.You need to find the smallest good integer.A subsegment of the array a is a set of elements a_l, a_{l + 1}, ..., a_r for some 1 <= l <= r <= n. We will denote such subsegment as [l, r].
Input: ['631 2 351 2 3 4 522 311000000000121 8 4 2 3 5 7 2 9 10 11 13127 2 5 4 2 1 1 2 3 11 8 9'] Output:['4', '7', '1', '1', '16', '13', '']
[ 3, 4 ]
Zinaida Viktorovna has n students in her history class. The homework for today included m topics, but the students had little time to prepare, so i-th student learned only topics from l_i to r_i inclusive.At the beginning of the lesson, each student holds their hand at 0. The teacher wants to ask some topics. It goes like this: The teacher asks the topic k. If the student has learned topic k, then he raises his hand by 1, otherwise he lower it by 1. Each topic Zinaida Viktorovna can ask no more than one time.Find the maximum difference of the heights of the highest and the lowest hand that can be in class after the survey.Note that the student's hand can go below 0.
Input: ['64 82 64 82 71 53 31 32 32 23 51 51 51 53 51 13 35 54 71 71 33 34 52 41 32 4'] Output:['6', '4', '0', '2', '12', '2', '']
[ 0, 2 ]
Alice and Bob are playing a game. They have two strings S and T of the same length n consisting of lowercase latin letters. Players take turns alternately, with Alice going first.On her turn, Alice chooses an integer i from 1 to n, one of the strings S or T, and any lowercase latin letter c, and replaces the i-th symbol in the chosen string with the character c.On his turn, Bob chooses one of the strings S or T, and reverses it. More formally, Bob makes the replacement S := \operatorname{rev}(S) or T := \operatorname{rev}(T), where \operatorname{rev}(P) = P_n P_{n-1} ... P_1.The game lasts until the strings S and T are equal. As soon as the strings become equal, the game ends instantly.Define the duration of the game as the total number of moves made by both players during the game. For example, if Alice made 2 moves in total, and Bob made 1 move, then the duration of this game is 3.Alice's goal is to minimize the duration of the game, and Bob's goal is to maximize the duration of the game.What will be the duration of the game, if both players play optimally? It can be shown that the game will end in a finite number of turns.
Input: ['75abcdeabxde5helloolleo2abcd7aaaaaaaabbbbba1qq6yoyoyooyoyoy8abcdefghhguedfbh'] Output:['1', '2', '3', '9', '0', '2', '6', '']
[ 2, 3 ]
Fedya is playing a new game called "The Legend of Link", in which one of the character's abilities is to combine two materials into one weapon. Each material has its own strength, which can be represented by a positive integer x. The strength of the resulting weapon is determined as the sum of the absolute differences of the digits in the decimal representation of the integers at each position.Formally, let the first material have strength X = \overline{x_{1}x_{2} ... x_{n}}, and the second material have strength Y = \overline{y_{1}y_{2} ... y_{n}}. Then the strength of the weapon is calculated as |x_{1} - y_{1}| + |x_{2} - y_{2}| + ... + |x_{n} - y_{n}|. If the integers have different lengths, then the shorter integer is padded with leading zeros.Fedya has an unlimited supply of materials with all possible strengths from L to R, inclusive. Help him find the maximum possible strength of the weapon he can obtain.An integer C = \overline{c_{1}c_{2} ... c_{k}} is defined as an integer obtained by sequentially writing the digits c_1, c_2, ..., c_k from left to right, i.e. 10^{k-1} \cdot c_1 + 10^{k-2} \cdot c_2 + ... + c_k.
Input: ['653 57179 23913 37132228 13222854943329752812629795 5515758193968886336688 1914'] Output:['4', '19', '11', '0', '163', '28', '']
[ 2, 3 ]
Given an array a of length n, which elements are equal to -1 and 1. Let's call the array a good if the following conditions are held at the same time: a_1 + a_2 + ... + a_n >= 0; a_1 \cdot a_2 \cdot ... \cdot a_n = 1. In one operation, you can select an arbitrary element of the array a_i and change its value to the opposite. In other words, if a_i = -1, you can assign the value to a_i := 1, and if a_i = 1, then assign the value to a_i := -1.Determine the minimum number of operations you need to perform to make the array a good. It can be shown that this is always possible.
Input: ['74-1 -1 1 -15-1 -1 -1 1 14-1 1 -1 13-1 -1 -151 1 1 1 11-12-1 -1'] Output:['1', '1', '0', '3', '0', '1', '2', '']
[ 2, 3 ]
Ksyusha has a pet chinchilla, a tree on n vertices and huge scissors. A tree is a connected graph without cycles. During a boring physics lesson Ksyusha thought about how to entertain her pet.Chinchillas like to play with branches. A branch is a tree of 3 vertices. The branch looks like this. A cut is the removal of some (not yet cut) edge in the tree. Ksyusha has plenty of free time, so she can afford to make enough cuts so that the tree splits into branches. In other words, after several (possibly zero) cuts, each vertex must belong to exactly one branch.Help Ksyusha choose the edges to be cut or tell that it is impossible.
Input: ['491 24 37 95 44 63 28 71 761 21 34 31 56 161 23 23 44 56 551 35 35 23 4'] Output:['2', '2 8 ', '-1', '1', '3 ', '-1', '']
[ 2 ]
Ira loves Spanish flamenco dance very much. She decided to start her own dance studio and found n students, ith of whom has level a_i.Ira can choose several of her students and set a dance with them. So she can set a huge number of dances, but she is only interested in magnificent dances. The dance is called magnificent if the following is true: exactly m students participate in the dance; levels of all dancers are pairwise distinct; levels of every two dancers have an absolute difference strictly less than m. For example, if m = 3 and a = [4, 2, 2, 3, 6], the following dances are magnificent (students participating in the dance are highlighted in red): [\color{red}{4}, 2, \color{red}{2}, \color{red}{3}, 6], [\color{red}{4}, \color{red}{2}, 2, \color{red}{3}, 6]. At the same time dances [\color{red}{4}, 2, 2, \color{red}{3}, 6], [4, \color{red}{2}, \color{red}{2}, \color{red}{3}, 6], [\color{red}{4}, 2, 2, \color{red}{3}, \color{red}{6}] are not magnificent.In the dance [\color{red}{4}, 2, 2, \color{red}{3}, 6] only 2 students participate, although m = 3.The dance [4, \color{red}{2}, \color{red}{2}, \color{red}{3}, 6] involves students with levels 2 and 2, although levels of all dancers must be pairwise distinct.In the dance [\color{red}{4}, 2, 2, \color{red}{3}, \color{red}{6}] students with levels 3 and 6 participate, but |3 - 6| = 3, although m = 3.Help Ira count the number of magnificent dances that she can set. Since this number can be very large, count it modulo 10^9 + 7. Two dances are considered different if the sets of students participating in them are different.
Input: ['97 48 10 10 9 6 11 75 34 2 2 3 68 21 5 2 2 3 1 3 33 33 3 35 13 4 3 10 712 35 2 1 1 4 3 5 5 5 2 7 51 113 21 2 32 21 2'] Output:['5', '2', '10', '0', '5', '11', '1', '2', '1', '']
[ 3 ]
You are given a permutation p of length n.A permutation 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, while \{1,2,2\} is not (since 2 appears twice), and \{1,3,4\} is also not a permutation (as n=3, but the array contains 4).To the permutation p, you need to apply the following operation exactly once: First you choose a segment [l, r] (1 <= l <= r <= n, a segment is a continuous sequence of numbers \{p_l, p_{l+1}, ..., p_{r-1}, p_r\}) and reverse it. Reversing a segment means swapping pairs of numbers (p_l, p_r), (p_{l+1}, p_{r-1}), ..., (p_{l + i}, p_{r - i}) (where l + i <= r - i). Then you swap the prefix and suffix: [r+1, n] and [1, l - 1] (note that these segments may be empty). For example, given n = 5, p = \{2, \color{blue}{3}, \color{blue}{1}, 5, 4\}, if you choose the segment [l = 2, r = 3], after reversing the segment p = \{\color{green}{2}, \color{blue}{1}, \color{blue}{3}, \color{green}{5}, \color{green}{4}\}, then you swap the segments [4, 5] and [1, 1]. Thus, p = \{\color{green}{5}, \color{green}{4}, 1, 3, \color{green}{2}\}. It can be shown that this is the maximum possible result for the given permutation.You need to output the lexicographically maximum permutation that can be obtained by applying the operation described exactly once.A permutation a is lexicographically greater than permutation b if there exists an i (1 <= i <= n) such that a_j = b_j for 1 <= j < i and a_i > b_i.
Input: ['952 3 1 5 494 1 6 7 2 8 5 3 944 3 2 122 163 2 4 1 5 673 2 1 5 7 6 41010 2 5 6 1 9 3 8 4 744 2 1 311'] Output:['5 4 1 3 2 ', '9 4 1 6 7 2 8 5 3 ', '3 2 1 4 ', '1 2 ', '6 5 3 2 4 1 ', '7 6 4 5 3 2 1 ', '9 3 8 4 7 1 10 2 5 6 ', '3 4 2 1 ', '1 ', '']
[ 0, 2 ]
Vlad was given an array a of n positive integers. Now he wants to build a beautiful array b of length n from it.Vlad considers an array beautiful if all the numbers in it are positive and have the same parity. That is, all numbers in the beautiful array are greater than zero and are either all even or all odd.To build the array b, Vlad can assign each b_i either the value a_i or a_i - a_j, where any j from 1 to n can be chosen.To avoid trying to do the impossible, Vlad asks you to determine whether it is possible to build a beautiful array b of length n using his array a.
Input: ['752 6 8 4 351 4 7 6 942 6 4 1075 29 13 9 10000001 11 352 1 2 4 252 4 5 4 342 5 5 4'] Output:['NO', 'YES', 'YES', 'YES', 'YES', 'NO', 'NO', '']
[ 2, 3 ]
You are given an array a containing the weather forecast for Berlandia for the last n days. That is, a_i — is the estimated air temperature on day i (1 <= i <= n).You are also given an array b — the air temperature that was actually present on each of the days. However, all the values in array b are mixed up. Determine which day was which temperature, if you know that the weather never differs from the forecast by more than k degrees. In other words, if on day i the real air temperature was c, then the equality |a_i - c| <= k is always true.For example, let an array a = [1, 3, 5, 3, 9] of length n = 5 and k = 2 be given and an array b = [2, 5, 11, 2, 4]. Then, so that the value of b_i corresponds to the air temperature on day i, we can rearrange the elements of the array b so: [2, 2, 5, 4, 11]. Indeed: On the 1st day, |a_1 - b_1| = |1 - 2| = 1, 1 <= 2 = k is satisfied; On the 2nd day |a_2 - b_2| = |3 - 2| = 1, 1 <= 2 = k is satisfied; On the 3rd day, |a_3 - b_3| = |5 - 5| = 0, 0 <= 2 = k is satisfied; On the 4th day, |a_4 - b_4| = |3 - 4| = 1, 1 <= 2 = k is satisfied; On the 5th day, |a_5 - b_5| = |9 - 11| = 2, 2 <= 2 = k is satisfied.
Input: ['35 21 3 5 3 92 5 11 2 46 1-1 3 -2 0 -5 -1-4 0 -1 4 0 03 37 7 79 4 8'] Output:['2 2 5 4 11', '0 4 -1 0 -4 0', '8 4 9']
[ 2 ]
Polycarp plays a computer game in a post-apocalyptic setting. The zombies have taken over the world, and Polycarp with a small team of survivors is defending against hordes trying to invade their base. The zombies are invading for x minutes starting from minute 0. There are n entrances to the base, and every minute one zombie attempts to enter through every entrance.The survivors can defend the entrances against the zombies. There are two options: manually — shoot the zombies coming through a certain entrance; automatically — set up an electric fence on a certain entrance to fry the zombies. If an entrance is defended either or both ways during some minute, no zombie goes through.Every entrance is defended by a single dedicated survivor. The i-th entrance is defended manually from minute l_i until minute r_i, non-inclusive — [l_i, r_i).There are k generators that can be used to defend the entrances automatically. Every entrance should be connected to exactly one generator, but a generator can be connected to multiple entrances (or even none of them). Each generator will work for exactly m consecutive minutes. Polycarp can choose when to power on each generator independently of each other, the m minute long interval should be fully inside the [0, x) time interval.Polycarp is a weird gamer. He wants the game to be as difficult as possible for him. So he wants to connect each entrance to a generator and choose the time for each generator in such a way that as many zombies as possible enter the base. Please, help him to achieve that!
Input: ['3 3 10 3', '0 2', '1 7', '4 7', ''] Output:['18', '']
[ 4 ]
Recall that the binomial coefficient \binom{x}{y} is calculated as follows (x and y are non-negative integers): if x < y, then \binom{x}{y} = 0; otherwise, \binom{x}{y} = \frac{x!}{y! \cdot (x-y)!}. You are given an array a_1, a_2, ..., a_n and an integer k. You have to calculate a new array b_1, b_2, ..., b_n, where b_1 = (\binom{1}{k} \cdot a_1) \bmod 998244353; b_2 = (\binom{2}{k} \cdot a_1 + \binom{1}{k} \cdot a_2) \bmod 998244353; b_3 = (\binom{3}{k} \cdot a_1 + \binom{2}{k} \cdot a_2 + \binom{1}{k} \cdot a_3) \bmod 998244353, and so on. Formally, b_i = (\sum\limits_{j=1}^{i} \binom{i - j + 1}{k} \cdot a_j) \bmod 998244353.Note that the array is given in a modified way, and you have to output it in a modified way as well.
Input: ['5 8 2 3 100 2', ''] Output:['1283', '']
[ 0 ]
The only difference between easy and hard versions is the maximum values of n and q.You are given an array, consisting of n integers. Initially, all elements are red.You can apply the following operation to the array multiple times. During the i-th operation, you select an element of the array; then: if the element is red, it increases by i and becomes blue; if the element is blue, it decreases by i and becomes red. The operations are numbered from 1, i. e. during the first operation some element is changed by 1 and so on.You are asked q queries of the following form: given an integer k, what can the largest minimum in the array be if you apply exactly k operations to it? Note that the operations don't affect the array between queries, all queries are asked on the initial array a.
Input: ['4 10', '5 2 8 4', '1 2 3 4 5 6 7 8 9 10', ''] Output:['3 4 5 6 7 8 8 10 8 12', '']
[ 2, 3, 4 ]
The only difference between easy and hard versions is the maximum values of n and q.You are given an array, consisting of n integers. Initially, all elements are red.You can apply the following operation to the array multiple times. During the i-th operation, you select an element of the array; then: if the element is red, it increases by i and becomes blue; if the element is blue, it decreases by i and becomes red. The operations are numbered from 1, i. e. during the first operation some element is changed by 1 and so on.You are asked q queries of the following form: given an integer k, what can the largest minimum in the array be if you apply exactly k operations to it? Note that the operations don't affect the array between queries, all queries are asked on the initial array a.
Input: ['4 10', '5 2 8 4', '1 2 3 4 5 6 7 8 9 10', ''] Output:['3 4 5 6 7 8 8 10 8 12', '']
[ 2, 3, 4 ]
For an array of integers [a_1, a_2, ..., a_n], let's call the value |a_1-a_2|+|a_2-a_3|+\cdots+|a_{n-1}-a_n| the contrast of the array. Note that the contrast of an array of size 1 is equal to 0.You are given an array of integers a. Your task is to build an array of b in such a way that all the following conditions are met: b is not empty, i.e there is at least one element; b is a subsequence of a, i.e b can be produced by deleting some elements from a (maybe zero); the contrast of b is equal to the contrast of a. What is the minimum possible size of the array b?
Input: ['451 3 3 3 724 241 1 1 175 4 2 1 0 0 4'] Output:['2', '2', '1', '3', '']
[ 2 ]
You are given an array a_1, a_2, ..., a_n, where all elements are different.You have to perform exactly k operations with it. During each operation, you do exactly one of the following two actions (you choose which to do yourself): find two minimum elements in the array, and delete them; find the maximum element in the array, and delete it. You have to calculate the maximum possible sum of elements in the resulting array.
Input: ['65 12 5 1 10 65 22 5 1 10 63 11 2 36 115 22 12 10 13 116 215 22 12 10 13 115 1999999996 999999999 999999997 999999998 999999995'] Output:['21', '11', '3', '62', '46', '3999999986', '']
[ 0 ]
You are given two arrays a and b both of length n.You will merge^ these arrays forming another array c of length 2 \cdot n. You have to find the maximum length of a subarray consisting of equal values across all arrays c that could be obtained.^ A merge of two arrays results in an array c composed by successively taking the first element of either array (as long as that array is nonempty) and removing it. After this step, the element is appended to the back of c. We repeat this operation as long as we can (i.e. at least one array is nonempty).
Input: ['412231 2 34 5 621 22 151 2 2 2 22 1 1 1 1'] Output:['2', '1', '2', '5', '']
[ 2 ]
On a permutation p of length n, we define a bully swap as follows: Let i be the index of the largest element p_i such that p_i \neq i. Let j be the index of the smallest element p_j such that i < j. Swap p_i and p_j. We define f(p) as the number of bully swaps we need to perform until p becomes sorted. Note that if p is the identity permutation, f(p)=0.You are given n and a permutation p of length n. You need to process the following q updates.In each update, you are given two integers x and y. You will swap p_x and p_y and then find the value of f(p).Note that the updates are persistent. Changes made to the permutation p will apply when processing future updates.
Input: ['8 5', '6 2 1 5 3 4 7 8', '1 8', '2 3', '4 7', '7 8', '3 6', ''] Output:['5', '6', '9', '8', '7', '']
[ 3 ]
You are given a tree with n nodes. For each node, you either color it in 0 or 1.The value of a path (u,v) is equal to the MEX^ of the colors of the nodes from the shortest path between u and v.The value of a coloring is equal to the sum of values of all paths (u,v) such that 1 <=q u <=q v <=q n.What is the maximum possible value of any coloring of the tree? The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of [2,2,1] is 0, because 0 does not belong to the array. The MEX of [3,1,0,1] is 2, because 0 and 1 belong to the array, but 2 does not. The MEX of [0,3,1,2] is 4 because 0, 1, 2, and 3 belong to the array, but 4 does not.
Input: ['431 22 341 21 31 4101 21 33 43 51 65 72 86 96 101'] Output:['8', '15', '96', '1', '']
[ 0 ]
You are given an integer n and k intervals. The i-th interval is [l_i,r_i] where 1 <=q l_i <=q r_i <=q n.Let us call a regular bracket sequence^{,\ddagger} of length n hyperregular if for each i such that 1 <=q i <=q k, the substring \overline{s_{l_i} s_{l_{i}+1} ... s_{r_i}} is also a regular bracket sequence.Your task is to count the number of hyperregular bracket sequences. Since this number can be really large, you are only required to find it modulo 998\,244\,353.^ A bracket sequence is a string containing only the characters "(" and ")".^\ddagger A bracket sequence is called regular if one can turn it into a valid math expression by adding characters + and 1. For example, sequences (())(), (), (()(())) and the empty string are regular, while )(, ((), and (()))( are not.
Input: ['76 05 08 11 310 23 46 91000 3100 701200 801300 90128 51 123 2011 144 918 194 31 41 41 4'] Output:['5', '0', '0', '4', '839415253', '140', '2', '']
[ 2, 3 ]
You are given two arrays a and b, both of length n.Your task is to count the number of pairs of integers (i,j) such that 1 <=q i < j <=q n and a_i \cdot a_j = b_i+b_j.
Input: ['332 3 23 3 184 2 8 2 1 2 7 53 5 8 8 1 1 6 584 4 8 8 8 8 8 88 8 8 8 8 8 8 8'] Output:['2', '7', '1', '']
[ 0, 3 ]
Sadly, the problem setter couldn't think of an interesting story, thus he just asks you to solve the following problem.Given an array a consisting of n positive integers, count the number of non-empty subsequences for which the bitwise \mathsf{AND} of the elements in the subsequence has exactly k set bits in its binary representation. The answer may be large, so output it modulo 10^9+7.Recall that the subsequence of an array a is a sequence that can be obtained from a by removing some (possibly, zero) elements. For example, [1, 2, 3], [3], [1, 3] are subsequences of [1, 2, 3], but [3, 2] and [4, 5, 6] are not.Note that \mathsf{AND} represents the bitwise AND operation.
Input: ['65 11 1 1 1 14 00 1 2 35 15 5 7 4 21 2312 00 2 0 2 0 2 0 2 0 2 0 210 663 0 63 5 5 63 63 4 12 13'] Output:['31', '10', '10', '1', '4032', '15', '']
[ 3 ]
In a carnival game, there is a huge pyramid of cans with 2023 rows, numbered in a regular pattern as shown. If can 9^2 is hit initially, then all cans colored red in the picture above would fall. You throw a ball at the pyramid, and it hits a single can with number n^2. This causes all cans that are stacked on top of this can to fall (that is, can n^2 falls, then the cans directly above n^2 fall, then the cans directly above those cans, and so on). For example, the picture above shows the cans that would fall if can 9^2 is hit.What is the sum of the numbers on all cans that fall? Recall that n^2 = n * n.
Input: ['1091234561014341000000'] Output:['156', '1', '5', '10', '21', '39', '46', '146', '63145186', '58116199242129511', '']
[ 3 ]
A snowflake graph is generated from two integers x and y, both greater than 1, as follows: Start with one central vertex. Connect x new vertices to this central vertex. Connect y new vertices to each of these x vertices. For example, below is a snowflake graph for x=5 and y=3. The snowflake graph above has a central vertex 15, then x=5 vertices attached to it (3, 6, 7, 8, and 20), and then y=3 vertices attached to each of those. Given a snowflake graph, determine the values of x and y.
Input: ['321 2021 205 2013 201 311 310 34 819 814 89 712 717 718 616 62 66 157 158 1520 153 157 61 21 32 42 53 63 79 89 33 66 22 15 22 74 33 8'] Output:['5 3', '2 2', '2 3', '']
[ 3 ]
Initially you have a single pile with n gold nuggets. In an operation you can do the following: Take any pile and split it into two piles, so that one of the resulting piles has exactly twice as many gold nuggets as the other. (All piles should have an integer number of nuggets.) One possible move is to take a pile of size 6 and split it into piles of sizes 2 and 4, which is valid since 4 is twice as large as 2. Can you make a pile with exactly m gold nuggets using zero or more operations?
Input: ['116 49 44 218 2727 427 227 101 13 15 1746001 2984004'] Output:['YES', 'YES', 'NO', 'NO', 'YES', 'YES', 'NO', 'YES', 'YES', 'NO', 'NO', '']
[ 0 ]
Victor wants to become "Mr. Perfectly Fine". For that, he needs to acquire a certain set of skills. More precisely, he has 2 skills he needs to acquire. Victor has n books. Reading book i takes him m_i minutes and will give him some (possibly none) of the required two skills, represented by a binary string of length 2.What is the minimum amount of time required so that Victor acquires all of the two skills?
Input: ['642 003 104 014 0053 013 015 012 109 1015 1139 118 017 1064 016 017 018 009 011 0048 009 109 118 11'] Output:['7', '5', '5', '9', '-1', '8', '']
[ 2 ]
You are given an unsorted permutation p_1, p_2, ..., p_n. To sort the permutation, you choose a constant k (k >= 1) and do some operations on the permutation. In one operation, you can choose two integers i, j (1 <= j < i <= n) such that i - j = k, then swap p_i and p_j.What is the maximum value of k that you can choose to sort the given permutation?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).An unsorted permutation p is a permutation such that there is at least one position i that satisfies p_i!=i.
Input: ['733 1 243 4 1 274 2 6 7 5 3 191 6 7 4 9 2 3 8 561 5 3 4 2 6103 10 5 2 9 6 7 8 1 4111 11 6 4 8 3 7 5 9 10 2'] Output:['1', '2', '3', '4', '3', '2', '3', '']
[ 3 ]
You are given a positive integer n. Please find an array a_1, a_2, ..., a_n that is perfect.A perfect array a_1, a_2, ..., a_n satisfies the following criteria: 1 <= a_i <= 1000 for all 1 <= i <= n. a_i is divisible by i for all 1 <= i <= n. a_1 + a_2 + ... + a_n is divisible by n.
Input: ['71234567'] Output:['1', '2 4', '1 2 3', '2 8 6 4', '3 4 9 4 5', '1 10 18 8 5 36', '3 6 21 24 10 6 14', '']
[ 3 ]
You are given a permutation a_1,a_2,...,a_n of the first n positive integers. A subarray [l,r] is called copium if we can rearrange it so that it becomes a sequence of consecutive integers, or more formally, if \max(a_l,a_{l+1},...,a_r)-\min(a_l,a_{l+1},...,a_r)=r-l For each k in the range [0,n], print out the maximum number of copium subarrays of a over all ways of rearranging the last n-k elements of a.
Input: ['555 2 1 4 342 1 4 31187 5 8 1 4 2 6 3101 4 5 3 7 8 9 2 10 6'] Output:['15 15 11 10 9 9 ', '10 8 8 7 7 ', '1 1 ', '36 30 25 19 15 13 12 9 9 ', '55 55 41 35 35 25 22 22 19 17 17 ', '']
[ 2 ]
There is a country consisting of n cities and n - 1 bidirectional roads connecting them such that we can travel between any two cities using these roads. In other words, these cities and roads form a tree.There are m bus routes connecting the cities together. A bus route between city x and city y allows you to travel between any two cities in the simple path between x and y with this route.Determine if for every pair of cities u and v, you can travel from u to v using at most two bus routes.
Input: ['45 21 22 33 42 51 45 25 11 22 33 42 51 52 01 26 31 22 33 44 55 61 32 54 6'] Output:['YES', 'NO', '1 3', 'NO', '1 2', 'NO', '1 6']
[ 2, 4 ]
You are given a tree (an undirected connected acyclic graph) which initially only contains vertex 1. There will be several queries to the given tree. In the i-th query, vertex i + 1 will appear and be connected to vertex p_i (1 <= p_i <= i). After each query, please find out the least number of operations required to make the current tree has two centroids. In one operation, you can add one vertex and one edge to the tree such that it remains a tree.A vertex is called a centroid if its removal splits the tree into subtrees with at most \lfloor \frac{n}{2} \rfloor vertices each, with n as the number of vertices of the tree. For example, the centroid of the following tree is 3 because the biggest subtree after removing the centroid has 2 vertices. In the next tree, vertex 1 and 2 are both centroids.
Input: ['52131 141 2 371 2 3 2 5 2101 2 2 4 5 5 7 8 9'] Output:['0', '0 1', '0 1 0', '0 1 0 1 2 3', '0 1 2 1 0 1 0 1 2']
[ 2 ]
A substring is a continuous and non-empty segment of letters from a given string, without any reorders.An even palindrome is a string that reads the same backward as forward and has an even length. For example, strings "zz", "abba", "abccba" are even palindromes, but strings "codeforces", "reality", "aba", "c" are not.A beautiful string is an even palindrome or a string that can be partitioned into some smaller even palindromes.You are given a string s, consisting of n lowercase Latin letters. Count the number of beautiful substrings of s.
Input: ['66abaaba1a2aa6abcdef12accabccbacca6abbaaa'] Output:['3', '0', '1', '0', '14', '6', '']
[ 0, 4 ]
The only difference between this problem and the easy version is the constraints on t and n.You are given an array a, consisting of n distinct integers a_1, a_2, ..., a_n. Define the beauty of an array p_1, p_2, ... p_k as the minimum amount of time needed to sort this array using an arbitrary number of range-sort operations. In each range-sort operation, you will do the following: Choose two integers l and r (1 <= l < r <= k). Sort the subarray p_l, p_{l + 1}, ..., p_r in r - l seconds. Please calculate the sum of beauty over all subarrays of array a.A subarray of an array is defined as a sequence of consecutive elements of the array.
Input: ['526 433 10 644 8 7 259 8 2 4 6122 6 13 3 15 5 10 8 16 9 11 18'] Output:['1', '2', '8', '16', '232', '']
[ 2, 4 ]
The only difference between this problem and the hard version is the constraints on t and n.You are given an array a, consisting of n distinct integers a_1, a_2, ..., a_n. Define the beauty of an array p_1, p_2, ... p_k as the minimum amount of time needed to sort this array using an arbitrary number of range-sort operations. In each range-sort operation, you will do the following: Choose two integers l and r (1 <= l < r <= k). Sort the subarray p_l, p_{l + 1}, ..., p_r in r - l seconds. Please calculate the sum of beauty over all subarrays of array a.A subarray of an array is defined as a sequence of consecutive elements of the array.
Input: ['526 433 10 644 8 7 259 8 2 4 6122 6 13 3 15 5 10 8 16 9 11 18'] Output:['1', '2', '8', '16', '232', '']
[ 2, 4 ]
You are given two arrays a and b each consisting of n integers. All elements of a are pairwise distinct.Find the number of ways to reorder a such that a_i > b_i for all 1 <= i <= n, modulo 10^9 + 7.Two ways of reordering are considered different if the resulting arrays are different.
Input: ['569 6 8 4 5 24 1 5 6 3 134 3 23 4 912132 3 41 3 3122 3 7 10 23 28 29 50 69 135 420 10001 1 2 3 5 8 13 21 34 55 89 144'] Output:['32', '0', '1', '0', '13824', '']
[ 3 ]
This is an interactive problem.There are n distinct hidden points with real coordinates on a two-dimensional Euclidean plane. In one query, you can ask some line ax + by + c = 0 and get the projections of all n points to this line in some order. The given projections are not exact, please read the interaction section for more clarity.Using the minimum number of queries, guess all n points and output them in some order. Here minimality means the minimum number of queries required to solve any possible test case with n points.The hidden points are fixed in advance and do not change throughout the interaction. In other words, the interactor is not adaptive.A projection of point A to line ax + by + c = 0 is the point on the line closest to A.
Input: ['1', '2', '', '1 1 2.5 1', '', '1.500000001 1.500000000 2 2', '', ''] Output:['', '', '? 0 1 -1', '', '? 0.2 -0.2 0', '', '! 1 3 2.5 0.500000001']
[ 3 ]
A fashion tour consists of m identical runway shows in different cities. There are n models willing to participate in the tour, numbered from 1 to n. People in different cities have different views on the fashion industry, so they rate each model differently. In particular, people in city i rate model j with rating r_{i, j}.You are to choose some number of k models, and their order, let the chosen models have indices j_1, j_2, ..., j_k in the chosen order. In each city, these k models will walk the runway one after another in this order. To make the show exciting, in each city, the ratings of models should be strictly increasing in the order of their performance. More formally, for any city i and index t (2 <=q t <=q k), the ratings must satisfy r_{i,j_{t - 1}} < r_{i,j_t}. After all, the fashion industry is all about money, so choosing model j to participate in the tour profits you p_j money. Compute the maximum total profit you can make by choosing the models and their order while satisfying all the requirements.
Input: ['3 5', '10 10 10 10 10', '1 2 3 4 5', '1 5 2 3 4', '2 3 4 5 1', ''] Output:['30', '']
[ 0 ]
There is a street with n sights, with sight number i being i miles from the beginning of the street. Sight number i has beauty b_i. You want to start your morning jog l miles and end it r miles from the beginning of the street. By the time you run, you will see sights you run by (including sights at l and r miles from the start). You are interested in the 3 most beautiful sights along your jog, but every mile you run, you get more and more tired.So choose l and r, such that there are at least 3 sights you run by, and the sum of beauties of the 3 most beautiful sights minus the distance in miles you have to run is maximized. More formally, choose l and r, such that b_{i_1} + b_{i_2} + b_{i_3} - (r - l) is maximum possible, where i_1, i_2, i_3 are the indices of the three maximum elements in range [l, r].
Input: ['455 1 4 2 341 1 1 169 8 7 6 5 47100000000 1 100000000 1 100000000 1 100000000'] Output:['8', '1', '22', '299999996', '']
[ 0, 2 ]
Because to take away a man's freedom of choice, even his freedom to make the wrong choice, is to manipulate him as though he were a puppet and not a person.— Madeleine L'EngleThere are n programmers choosing their favorite algorithm amongst m different choice options. Before the first round, all m options are available. In each round, every programmer makes a vote for one of the remaining algorithms. After the round, only the algorithms with the maximum number of votes remain. The voting process ends when there is only one option left. Determine whether the voting process can continue indefinitely or no matter how people vote, they will eventually choose a single option after some finite amount of rounds?
Input: ['53 24 25 31000000 10000001 1000000'] Output:['YES', 'NO', 'YES', 'NO', 'YES', '']
[ 2, 3 ]
You have an array a of n non-negative integers. Let's define f(a, x) = [a_1 \bmod x, a_2 \bmod x, ..., a_n \bmod x] for some positive integer x. Find the biggest x, such that f(a, x) is a palindrome.Here, a \bmod x is the remainder of the integer division of a by x.An array is a palindrome if it reads the same backward as forward. More formally, an array a of length n is a palindrome if for every i (1 <=q i <=q n) a_i = a_{n - i + 1}.
Input: ['421 283 0 1 2 0 3 2 1103100 1 1000000000'] Output:['1', '2', '0', '999999900', '']
[ 3 ]
There is a group of n people. Some of them might be liars, who always tell lies. Other people always tell the truth. The i-th person says "There are at least l_i liars amongst us". Determine if what people are saying is contradictory, or if it is possible. If it is possible, output the number of liars in the group. If there are multiple possible answers, output any one of them.
Input: ['721 222 220 0111055 5 3 3 565 3 6 6 3 5'] Output:['1', '-1', '0', '-1', '0', '3', '4', '']
[ 0, 2 ]
LuoTianyi gave an array b of n \cdot m integers. She asks you to construct a table a of size n * m, filled with these n \cdot m numbers, and each element of the array must be used exactly once. Also she asked you to maximize the following value: \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}<=ft(\max\limits_{1 <= x <= i, 1 <= y <= j}a_{x,y}-\min\limits_{1 <= x <= i, 1 <= y <= j}a_{x,y}\right) This means that we consider n \cdot m subtables with the upper left corner in (1,1) and the bottom right corner in (i, j) (1 <= i <= n, 1 <= j <= m), for each such subtable calculate the difference of the maximum and minimum elements in it, then sum up all these differences. You should maximize the resulting sum.Help her find the maximal possible value, you don't need to reconstruct the table itself.
Input: ['52 21 3 1 42 2-1 -1 -1 -12 37 8 9 -3 10 83 24 8 -3 0 -7 14 3-32030 59554 16854 -85927 68060 -64460 -79547 90932 85063 82703 -12001 38762'] Output:['9', '0', '64', '71', '1933711', '']
[ 2, 3 ]
LuoTianyi gives you a palindrome string s, and she wants you to find out the length of the longest non-empty subsequence^{\ddagger} of s which is not a palindrome string. If there is no such subsequence, output -1 instead. A palindrome is a string that reads the same backward as forward. For example, strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not.^{\ddagger} A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from b. For example, strings "a", "aaa", "bab" are subsequences of string "abaab", but strings "codeforces", "bbb", "h" are not.
Input: ['4abacabaaaacodeforcesecrofedoclol'] Output:['6', '-1', '18', '2', '']
[ 2 ]
LuoTianyi gives you a tree with values in its vertices, and the root of the tree is vertex 1.In one operation, you can change the value in one vertex to any non-negative integer.Now you need to find the minimum number of operations you need to perform to make each path from the root to leaf has a bitwise XOR value of zero.A leaf in a rooted tree is a vertex that has exactly one neighbor and is not a root.
Input: ['6', '3 5 7 5 8 4', '1 2', '1 3', '1 4', '3 5', '4 6', ''] Output:['3']
[ 2 ]
This is the hard version of the problem. The only difference is that in this version k<= n. You can make hacks only if both versions of the problem are solved. Chtholly and the floating islands. LuoTianyi now lives in a world with n floating islands. The floating islands are connected by n-1 undirected air routes, and any two of them can reach each other by passing the routes. That means, the n floating islands form a tree.One day, LuoTianyi wants to meet her friends: Chtholly, Nephren, William, .... Totally, she wants to meet k people. She doesn't know the exact positions of them, but she knows that they are in pairwise distinct islands. She define an island is good if and only if the sum of the distances from it to the islands with k people is the minimal among all the n islands.Now, LuoTianyi wants to know that, if the k people are randomly set in k distinct of the n islands, then what is the expect number of the good islands? You just need to tell her the expect number modulo 10^9+7.The distance between two islands is the minimum number of air routes you need to take to get from one island to the other.
Input: ['4 2', '1 2', '2 3', '3 4', ''] Output:['666666674', '']
[ 3 ]
This is the easy version of the problem. The only difference is that in this version k<=\min(n,3). You can make hacks only if both versions of the problem are solved. Chtholly and the floating islands. LuoTianyi now lives in a world with n floating islands. The floating islands are connected by n-1 undirected air routes, and any two of them can reach each other by passing the routes. That means, the n floating islands form a tree.One day, LuoTianyi wants to meet her friends: Chtholly, Nephren, William, .... Totally, she wants to meet k people. She doesn't know the exact positions of them, but she knows that they are in pairwise distinct islands. She define an island is good if and only if the sum of the distances from it to the islands with k people is the minimal among all the n islands.Now, LuoTianyi wants to know that, if the k people are randomly set in k distinct of the n islands, then what is the expect number of the good islands? You just need to tell her the expect number modulo 10^9+7.The distance between two islands is the minimum number of air routes you need to take to get from one island to the other.
Input: ['4 2', '1 2', '2 3', '3 4', ''] Output:['666666674', '']
[ 3 ]
There are n people taking part in a show about VOCALOID. They will sit in the row of seats, numbered 1 to m from left to right.The n people come and sit in order. Each person occupies a seat in one of three ways: Sit in the seat next to the left of the leftmost person who is already sitting, or if seat 1 is taken, then leave the show. If there is no one currently sitting, sit in seat m. Sit in the seat next to the right of the rightmost person who is already sitting, or if seat m is taken, then leave the show. If there is no one currently sitting, sit in seat 1. Sit in the seat numbered x_i. If this seat is taken, then leave the show. Now you want to know what is the maximum number of people that can take a seat, if you can let people into the show in any order?
Input: ['103 105 5 54 61 -2 -2 15 7-1 -1 4 -2 -26 75 -2 -2 -2 -2 -26 6-1 1 4 5 -1 46 8-1 -1 -1 3 -1 -26 75 -1 -2 -2 -2 -23 1-2 -2 12 55 -21 2-1'] Output:['1', '3', '5', '6', '5', '5', '5', '1', '2', '1', '']
[ 2 ]
You are given a tree consisting of n vertices and n - 1 edges, and each vertex v has a counter c(v) assigned to it.Initially, there is a chip placed at vertex s and all counters, except c(s), are set to 0; c(s) is set to 1.Your goal is to place the chip at vertex t. You can achieve it by a series of moves. Suppose right now the chip is placed at the vertex v. In one move, you do the following: choose one of neighbors to of vertex v uniformly at random (to is neighbor of v if and only if there is an edge \{v, to\} in the tree); move the chip to vertex to and increase c(to) by 1; You'll repeat the move above until you reach the vertex t.For each vertex v calculate the expected value of c(v) modulo 998\,244\,353.
Input: ['3 1 3', '1 2', '2 3', ''] Output:['2 2 1 ', '']
[ 3 ]
Alice and Bob are playing a game on a graph. They have an undirected graph without self-loops and multiple edges. All vertices of the graph have degree equal to 2. The graph may consist of several components. Note that if such graph has n vertices, it will have exactly n edges.Alice and Bob take turn. Alice goes first. In each turn, the player can choose k (l <= k <= r; l < r) vertices that form a connected subgraph and erase these vertices from the graph, including all incident edges.The player who can't make a step loses.For example, suppose they are playing on the given graph with given l = 2 and r = 3: A valid vertex set for Alice to choose at the first move is one of the following: \{1, 2\} \{1, 3\} \{2, 3\} \{4, 5\} \{4, 6\} \{5, 6\} \{1, 2, 3\} \{4, 5, 6\} Suppose, Alice chooses subgraph \{4, 6\}.Then a valid vertex set for Bob to choose at the first move is one of the following: \{1, 2\} \{1, 3\} \{2, 3\} \{1, 2, 3\} Suppose, Bob chooses subgraph \{1, 2, 3\}.Alice can't make a move, so she loses.You are given a graph of size n and integers l and r. Who will win if both Alice and Bob play optimally.
Input: ['6 2 3', '1 2', '2 3', '3 1', '4 5', '5 6', '6 4', ''] Output:['Bob', '']
[ 0, 3 ]
A palindrome is a string that reads the same backwards as forwards. For example, the string abcba is palindrome, while the string abca is not.Let p(t) be the number of unique palindromic substrings of string t, i. e. the number of substrings t[l ... r] that are palindromes themselves. Even if some substring occurs in t several times, it's counted exactly once. (The whole string t is also counted as a substring of t).For example, string t = abcbbcabcb has p(t) = 6 unique palindromic substrings: a, b, c, bb, bcb and cbbc.Now, let's define p(s, m) = p(t) where t = s[1 ... m]. In other words, p(s, m) is the number of palindromic substrings in the prefix of s of length m. For example, p(abcbbcabcb, 5) = p(abcbb) = 5.You are given an integer n and k "conditions" (k <= 20). Let's say that string s, consisting of n lowercase Latin letters, is good if all k conditions are satisfied at the same time. A condition is a pair (x_i, c_i) and have the following meaning: p(s, x_i) = c_i, i. e. a prefix of s of length x_i contains exactly c_i unique palindromic substrings. Find a good string s or report that such s doesn't exist.Look in Notes if you need further clarifications.
Input: ['710 25 105 63 1334 23 43 34 23 43 44 14510 34 6 104 5 810 44 6 7 104 5 7 8'] Output:['YES', 'abcbbcabcb', 'YES', 'foo', 'YES', 'ayda', 'YES', 'wada', 'NO', 'YES', 'abcbcacbab', 'NO', '']
[ 3 ]
A prime number is an integer greater than 1, which has exactly two divisors. For example, 7 is a prime, since it has two divisors \{1, 7\}. A composite number is an integer greater than 1, which has more than two different divisors.Note that the integer 1 is neither prime nor composite.Let's look at some composite number v. It has several divisors: some divisors are prime, others are composite themselves. If the number of prime divisors of v is less or equal to the number of composite divisors, let's name v as strongly composite.For example, number 12 has 6 divisors: \{1, 2, 3, 4, 6, 12\}, two divisors 2 and 3 are prime, while three divisors 4, 6 and 12 are composite. So, 12 is strongly composite. Other examples of strongly composite numbers are 4, 8, 9, 16 and so on.On the other side, divisors of 15 are \{1, 3, 5, 15\}: 3 and 5 are prime, 15 is composite. So, 15 is not a strongly composite. Other examples are: 2, 3, 5, 6, 7, 10 and so on.You are given n integers a_1, a_2, ..., a_n (a_i > 1). You have to build an array b_1, b_2, ..., b_k such that following conditions are satisfied: Product of all elements of array a is equal to product of all elements of array b: a_1 \cdot a_2 \cdot ... \cdot a_n = b_1 \cdot b_2 \cdot ... \cdot b_k; All elements of array b are integers greater than 1 and strongly composite; The size k of array b is the maximum possible. Find the size k of array b, or report, that there is no array b satisfying the conditions.
Input: ['823 633 4 522 333 10 14225 301108093 3 3 5 5 5 7 7 72012 15 2 2 2 2 2 3 3 3 17 21 21 21 30 6 6 33 31 39'] Output:['1', '1', '0', '2', '2', '3', '4', '15', '']
[ 2, 3 ]
Let's define a permutation of length n as an array p of length n, which contains every number from 1 to n exactly once.You are given a permutation p_1, p_2, ..., p_n and a number k. You need to sort this permutation in the ascending order. In order to do it, you can repeat the following operation any number of times (possibly, zero): pick two elements of the permutation p_i and p_j such that |i - j| = k, and swap them. Unfortunately, some permutations can't be sorted with some fixed numbers k. For example, it's impossible to sort [2, 4, 3, 1] with k = 2.That's why, before starting the sorting, you can make at most one preliminary exchange: choose any pair p_i and p_j and swap them. Your task is to: check whether is it possible to sort the permutation without any preliminary exchanges, if it's not, check, whether is it possible to sort the permutation using exactly one preliminary exchange. For example, if k = 2 and permutation is [2, 4, 3, 1], then you can make a preliminary exchange of p_1 and p_4, which will produce permutation [1, 4, 3, 2], which is possible to sort with given k.
Input: ['64 13 1 2 44 23 4 1 24 23 1 4 210 34 5 9 1 8 6 10 2 3 710 34 6 9 1 8 5 10 2 3 710 34 6 9 1 8 5 10 3 2 7'] Output:['0', '0', '1', '0', '1', '-1', '']
[ 0, 3 ]
Consider an array a_1, a_2, ..., a_n consisting of numbers 1 and -1. Define A-characteristic of this array as a number of pairs of indices 1 <= i < j <= n, such that a_i \cdot a_j = 1.Find any array a with given length n with A-characteristic equal to the given value k.
Input: ['72 02 13 13 23 35 45 5'] Output:['YES', '1 -1 ', 'YES', '1 1 ', 'YES', '1 -1 1 ', 'NO', 'YES', '1 1 1 ', 'YES', '-1 1 -1 1 1 ', 'NO']
[ 3 ]
This is the hard version of the problem. The only difference is that in this version, a_i <= 10^9.For a given sequence of n integers a, a triple (i, j, k) is called magic if: 1 <= i, j, k <= n. i, j, k are pairwise distinct. there exists a positive integer b such that a_i \cdot b = a_j and a_j \cdot b = a_k. Kolya received a sequence of integers a as a gift and now wants to count the number of magic triples for it. Help him with this task!Note that there are no constraints on the order of integers i, j and k.
Input: ['751 7 7 2 736 2 1891 2 3 4 5 6 7 8 941000 993 986 17971 10 100 1000 10000 100000 100000081 1 2 2 4 4 8 891 1 1 2 2 2 4 4 4'] Output:['6', '1', '3', '0', '9', '16', '45', '']
[ 0, 3 ]
This is the easy version of the problem. The only difference is that in this version, a_i <= 10^6.For a given sequence of n integers a, a triple (i, j, k) is called magic if: 1 <= i, j, k <= n. i, j, k are pairwise distinct. there exists a positive integer b such that a_i \cdot b = a_j and a_j \cdot b = a_k. Kolya received a sequence of integers a as a gift and now wants to count the number of magic triples for it. Help him with this task!Note that there are no constraints on the order of integers i, j and k.
Input: ['751 7 7 2 736 2 1891 2 3 4 5 6 7 8 941000 993 986 17971 10 100 1000 10000 100000 100000081 1 2 2 4 4 8 891 1 1 2 2 2 4 4 4'] Output:['6', '1', '3', '0', '9', '16', '45', '']
[ 0, 3 ]
Two friends, Alisa and Yuki, planted a tree with n vertices in their garden. A tree is an undirected graph without cycles, loops, or multiple edges. Each edge in this tree has a length of k. Initially, vertex 1 is the root of the tree.Alisa and Yuki are growing the tree not just for fun, they want to sell it. The cost of the tree is defined as the maximum distance from the root to a vertex among all vertices of the tree. The distance between two vertices u and v is the sum of the lengths of the edges on the path from u to v.The girls took a course in gardening, so they know how to modify the tree. Alisa and Yuki can spend c coins to shift the root of the tree to one of the neighbors of the current root. This operation can be performed any number of times (possibly zero). Note that the structure of the tree is left unchanged; the only change is which vertex is the root.The friends want to sell the tree with the maximum profit. The profit is defined as the difference between the cost of the tree and the total cost of operations. The profit is cost of the tree minus the total cost of operations.Help the girls and find the maximum profit they can get by applying operations to the tree any number of times (possibly zero).
Input: ['43 2 32 13 15 4 12 14 25 43 46 5 34 16 12 65 13 210 6 41 31 99 77 66 49 22 88 55 10'] Output:['2', '12', '17', '32', '']
[ 0 ]
README.md exists but content is empty.
Downloads last month
37