question_text
stringlengths 2
3.82k
| input_outputs
stringlengths 23
941
| algo_tags
sequence |
---|---|---|
You are given a set of 2n+1 integer points on a Cartesian plane. Points are numbered from 0 to 2n inclusive. Let P_i be the i-th point. The x-coordinate of the point P_i equals i. The y-coordinate of the point P_i equals zero (initially). Thus, initially P_i=(i,0).The given points are vertices of a plot of a piecewise function. The j-th piece of the function is the segment P_{j}P_{j + 1}.In one move you can increase the y-coordinate of any point with odd x-coordinate (i.e. such points are P_1, P_3, ..., P_{2n-1}) by 1. Note that the corresponding segments also change.For example, the following plot shows a function for n=3 (i.e. number of points is 2\cdot3+1=7) in which we increased the y-coordinate of the point P_1 three times and y-coordinate of the point P_5 one time: Let the area of the plot be the area below this plot and above the coordinate axis OX. For example, the area of the plot on the picture above is 4 (the light blue area on the picture above is the area of the plot drawn on it).Let the height of the plot be the maximum y-coordinate among all initial points in the plot (i.e. points P_0, P_1, ..., P_{2n}). The height of the plot on the picture above is 3.Your problem is to say which minimum possible height can have the plot consisting of 2n+1 vertices and having an area equal to k. Note that it is unnecessary to minimize the number of moves.It is easy to see that any answer which can be obtained by performing moves described above always exists and is an integer number not exceeding 10^{18}. | Input: ['4 3'] Output:['1'] | [
3
] |
Little C loves number «3» very much. He loves all things about it.Now he is interested in the following problem:There are two arrays of 2^n intergers a_0,a_1,...,a_{2^n-1} and b_0,b_1,...,b_{2^n-1}.The task is for each i (0 <=q i <=q 2^n-1), to calculate c_i=\sum a_j \cdot b_k (j|k=i and j\&k=0, where "|" denotes bitwise or operation and "\&" denotes bitwise and operation).It's amazing that it can be proved that there are exactly 3^n triples (i,j,k), such that j|k=i, j\&k=0 and 0 <=q i,j,k <=q 2^n-1. So Little C wants to solve this excellent problem (because it's well related to 3) excellently.Help him calculate all c_i. Little C loves 3 very much, so he only want to know each c_i \& 3. | Input: ['11111'] Output:['12'] | [
3
] |
Little D is a friend of Little C who loves intervals very much instead of number "3".Now he has n intervals on the number axis, the i-th of which is [a_i,b_i].Only the n intervals can not satisfy him. He defines the value of an interval of intervals [l,r] (1 <=q l <=q r <=q n, l and r are both integers) as the total length of the union of intervals from the l-th to the r-th.He wants to select exactly k different intervals of intervals such that the sum of their values is maximal. Please help him calculate the maximal sum. | Input: ['2 11 32 4'] Output:['3'] | [
4
] |
Little C loves number «3» very much. He loves all things about it.Now he is playing a game on a chessboard of size n * m. The cell in the x-th row and in the y-th column is called (x,y). Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly 3. The Manhattan distance between two cells (x_i,y_i) and (x_j,y_j) is defined as |x_i-x_j|+|y_i-y_j|.He want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place. | Input: ['2 2'] Output:['0'] | [
0
] |
Alice has a computer that operates on w-bit integers. The computer has n registers for values. The current content of the registers is given as an array a_1, a_2, ..., a_n. The computer uses so-called "number gates" to manipulate this data. Each "number gate" takes two registers as inputs and calculates a function of the two values stored in those registers. Note that you can use the same register as both inputs.Each "number gate" is assembled from bit gates. There are six types of bit gates: AND, OR, XOR, NOT AND, NOT OR, and NOT XOR, denoted "A", "O", "X", "a", "o", "x", respectively. Each bit gate takes two bits as input. Its output given the input bits b_1, b_2 is given below: \begin{matrix} b_1 & b_2 & A & O & X & a & o & x \\ 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 \\ 0 & 1 & 0 & 1 & 1 & 1 & 0 & 0 \\ 1 & 0 & 0 & 1 & 1 & 1 & 0 & 0 \\ 1 & 1 & 1 & 1 & 0 & 0 & 0 & 1 \\ \end{matrix} To build a "number gate", one takes w bit gates and assembles them into an array. A "number gate" takes two w-bit integers x_1 and x_2 as input. The "number gate" splits the integers into w bits and feeds the i-th bit of each input to the i-th bit gate. After that, it assembles the resulting bits again to form an output word. For instance, for 4-bit computer we might have a "number gate" "AXoA" (AND, XOR, NOT OR, AND). For two inputs, 13 = 1101_2 and 10 = 1010_2, this returns 12 = 1100_2, as 1 and 1 is 1, 1 xor 0 is 1, not (0 or 1) is 0, and finally 1 and 0 is 0. You are given a description of m "number gates". For each gate, your goal is to report the number of register pairs for which the "number gate" outputs the number 0. In other words, find the number of ordered pairs (i,j) where 1 <=q i,j <=q n, such that w_k(a_i, a_j) = 0, where w_k is the function computed by the k-th "number gate". | Input: ['4 3 113 10 6AXoA'] Output:['3'] | [
0,
3
] |
Bob has a simple undirected connected graph (without self-loops and multiple edges). He wants to learn whether his graph is bipartite (that is, you can paint all vertices of the graph into two colors so that there is no edge connecting two vertices of the same color) or not. As he is not very good at programming, he asked Alice for help. He does not want to disclose his graph to Alice, but he agreed that Alice can ask him some questions about the graph.The only question that Alice can ask is the following: she sends s — a subset of vertices of the original graph. Bob answers with the number of edges that have both endpoints in s. Since he doesn't want Alice to learn too much about the graph, he allows her to ask no more than 20000 questions. Furthermore, he suspects that Alice might introduce false messages to their communication channel, so when Alice finally tells him whether the graph is bipartite or not, she also needs to provide a proof — either the partitions themselves or a cycle of odd length.Your task is to help Alice to construct the queries, find whether the graph is bipartite. | Input: ['4401110'] Output:['? 4 1 2 3 4? 21 2? 21 3? 21 4? 22 4? 23 4Y 21 2'] | [
4
] |
You are given n integers a_1, a_2, ..., a_n. Each of a_i has between 3 and 5 divisors. Consider a = \prod a_i — the product of all input integers. Find the number of divisors of a. As this number may be very large, print it modulo prime number 998244353. | Input: ['3915143'] Output:['32'] | [
3
] |
After a long day, Alice and Bob decided to play a little game. The game board consists of n cells in a straight line, numbered from 1 to n, where each cell contains a number a_i between 1 and n. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell i to cell j only if the following two conditions are satisfied: the number in the new cell j must be strictly larger than the number in the old cell i (i.e. a_j > a_i), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. |i-j|\bmod a_i = 0). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players. | Input: ['83 6 5 4 2 7 1 8'] Output:['BAAAABAB'] | [
0
] |
Alice has a lovely piece of cloth. It has the shape of a square with a side of length a centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length b centimeters (where b < a). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).Alice would like to know whether the area of her cloth expressed in square centimeters is prime. Could you help her to determine it? | Input: ['46 516 1361690850361 2477762263034 33'] Output:['YESNONOYES'] | [
3
] |
You have a set of n weights. You know that their masses are a_1, a_2, ..., a_n grams, but you don't know which of them has which mass. You can't distinguish the weights.However, your friend does know the mass of each weight. You can ask your friend to give you exactly k weights with the total mass m (both parameters k and m are chosen by you), and your friend will point to any valid subset of weights, if it is possible.You are allowed to make this query only once. Find the maximum possible number of weights you can reveal after this query. | Input: ['4', '1 4 2 2', ''] Output:['2', ''] | [
3
] |
You are given two positive integers a and b. There are two possible operations: multiply one of the numbers by some prime p; divide one of the numbers on its prime factor p. What is the minimum number of operations required to obtain two integers having the same number of divisors? You are given several such pairs, you need to find the answer for each of them. | Input: ['89 10100 17220 7017 194 1832 20100 32224 385'] Output:['13101011'] | [
0,
3
] |
You are given a matrix of size n * n filled with lowercase English letters. You can change no more than k letters in this matrix.Consider all paths from the upper left corner to the lower right corner that move from a cell to its neighboring cell to the right or down. Each path is associated with the string that is formed by all the letters in the cells the path visits. Thus, the length of each string is 2n - 1.Find the lexicographically smallest string that can be associated with a path after changing letters in at most k cells of the matrix.A string a is lexicographically smaller than a string b, if the first different letter in a and b is smaller in a. | Input: ['4 2abcdbcdebcadbcde'] Output:['aaabcde'] | [
2
] |
In a galaxy far, far away Lesha the student has just got to know that he has an exam in two days. As always, he hasn't attended any single class during the previous year, so he decided to spend the remaining time wisely.Lesha knows that today he can study for at most a hours, and he will have b hours to study tomorrow. Note that it is possible that on his planet there are more hours in a day than on Earth. Lesha knows that the quality of his knowledge will only depend on the number of lecture notes he will read. He has access to an infinite number of notes that are enumerated with positive integers, but he knows that he can read the first note in one hour, the second note in two hours and so on. In other words, Lesha can read the note with number k in k hours. Lesha can read the notes in arbitrary order, however, he can't start reading a note in the first day and finish its reading in the second day.Thus, the student has to fully read several lecture notes today, spending at most a hours in total, and fully read several lecture notes tomorrow, spending at most b hours in total. What is the maximum number of notes Lesha can read in the remaining time? Which notes should he read in the first day, and which — in the second? | Input: ['3 3'] Output:['13 22 1 '] | [
2
] |
You have a plate and you want to add some gilding to it. The plate is a rectangle that we split into w* h cells. There should be k gilded rings, the first one should go along the edge of the plate, the second one — 2 cells away from the edge and so on. Each ring has a width of 1 cell. Formally, the i-th of these rings should consist of all bordering cells on the inner rectangle of size (w - 4(i - 1))*(h - 4(i - 1)). The picture corresponds to the third example. Your task is to compute the number of cells to be gilded. | Input: ['3 3 1'] Output:['8'] | [
3
] |
There is an infinite board of square tiles. Initially all tiles are white.Vova has a red marker and a blue marker. Red marker can color a tiles. Blue marker can color b tiles. If some tile isn't white then you can't use marker of any color on it. Each marker must be drained completely, so at the end there should be exactly a red tiles and exactly b blue tiles across the board.Vova wants to color such a set of tiles that: they would form a rectangle, consisting of exactly a+b colored tiles; all tiles of at least one color would also form a rectangle. Here are some examples of correct colorings: Here are some examples of incorrect colorings: Among all correct colorings Vova wants to choose the one with the minimal perimeter. What is the minimal perimeter Vova can obtain?It is guaranteed that there exists at least one correct coloring. | Input: ['4 4'] Output:['12'] | [
0,
3,
4
] |
You are given an undirected tree consisting of n vertices. An undirected tree is a connected undirected graph with n - 1 edges.Your task is to add the minimum number of edges in such a way that the length of the shortest path from the vertex 1 to any other vertex is at most 2. Note that you are not allowed to add loops and multiple edges. | Input: ['71 22 32 44 54 65 7'] Output:['2'] | [
2
] |
You are given an array a, consisting of n positive integers.Let's call a concatenation of numbers x and y the number that is obtained by writing down numbers x and y one right after another without changing the order. For example, a concatenation of numbers 12 and 3456 is a number 123456.Count the number of ordered pairs of positions (i, j) (i \neq j) in array a such that the concatenation of a_i and a_j is divisible by k. | Input: ['6 1145 1 10 12 11 7'] Output:['7'] | [
3
] |
You are given n segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.The intersection of a sequence of segments is such a maximal set of points (not necesserily having integer coordinates) that each point lies within every segment from the sequence. If the resulting set isn't empty, then it always forms some continuous segment. The length of the intersection is the length of the resulting segment or 0 in case the intersection is an empty set.For example, the intersection of segments [1;5] and [3;10] is [3;5] (length 2), the intersection of segments [1;5] and [5;7] is [5;5] (length 0) and the intersection of segments [1;5] and [6;6] is an empty set (length 0).Your task is to remove exactly one segment from the given sequence in such a way that the intersection of the remaining (n - 1) segments has the maximal possible length. | Input: ['41 32 60 43 3'] Output:['1'] | [
2,
3
] |
You are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order.You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let a_{i_1}, a_{i_2}, ..., a_{i_p} be the difficulties of the selected problems in increasing order. Then for each j from 1 to p-1 a_{i_{j + 1}} <= a_{i_j} \cdot 2 should hold. It means that the contest consisting of only one problem is always valid.Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems. | Input: ['101 2 5 6 7 10 21 23 24 49'] Output:['4'] | [
2,
3
] |
We call an array b_1, b_2, ..., b_m good, if there exist two indices i < j such that b_i \cdot b_j is a perfect square.Given an array b_1, b_2, ..., b_m, in one action you can perform one of the following: multiply any element b_i by any prime p; divide any element b_i by prime p, if b_i is divisible by p. Let f(b_1, b_2, ..., b_m) be the minimum number of actions needed to make the array b good.You are given an array of n integers a_1, a_2, ..., a_n and q queries of the form l_i, r_i. For each query output f(a_{l_i}, a_{l_i + 1}, ..., a_{r_i}). | Input: ['10 1034 37 28 16 44 36 43 50 22 131 34 86 109 103 108 95 61 41 72 6'] Output:['2013011100'] | [
3
] |
Consider a set of points A, initially it is empty. There are three types of queries: Insert a point (x_i, y_i) to A. It is guaranteed that this point does not belong to A at this moment. Remove a point (x_i, y_i) from A. It is guaranteed that this point belongs to A at this moment. Given a point (x_i, y_i), calculate the minimum number of points required to add to A to make A symmetrical with respect to the line containing points (0, 0) and (x_i, y_i). Note that these points are not actually added to A, i.e. these queries are independent from each other. | Input: ['121 1 61 6 11 5 51 2 33 4 41 3 23 7 72 2 32 6 13 8 82 5 53 1 1'] Output:['1022'] | [
0
] |
Let's consider a simplified version of order book of some stock. The order book is a list of orders (offers) from people that want to buy or sell one unit of the stock, each order is described by direction (BUY or SELL) and price.At every moment of time, every SELL offer has higher price than every BUY offer. In this problem no two ever existed orders will have the same price.The lowest-price SELL order and the highest-price BUY order are called the best offers, marked with black frames on the picture below. The presented order book says that someone wants to sell the product at price 12 and it's the best SELL offer because the other two have higher prices. The best BUY offer has price 10. There are two possible actions in this orderbook: Somebody adds a new order of some direction with some price. Somebody accepts the best possible SELL or BUY offer (makes a deal). It's impossible to accept not the best SELL or BUY offer (to make a deal at worse price). After someone accepts the offer, it is removed from the orderbook forever.It is allowed to add new BUY order only with prices less than the best SELL offer (if you want to buy stock for higher price, then instead of adding an order you should accept the best SELL offer). Similarly, one couldn't add a new SELL order with price less or equal to the best BUY offer. For example, you can't add a new offer "SELL 20" if there is already an offer "BUY 20" or "BUY 25" — in this case you just accept the best BUY offer.You have a damaged order book log (in the beginning the are no orders in book). Every action has one of the two types: "ADD p" denotes adding a new order with price p and unknown direction. The order must not contradict with orders still not removed from the order book. "ACCEPT p" denotes accepting an existing best offer with price p and unknown direction.The directions of all actions are lost. Information from the log isn't always enough to determine these directions. Count the number of ways to correctly restore all ADD action directions so that all the described conditions are satisfied at any moment. Since the answer could be large, output it modulo 10^9 + 7. If it is impossible to correctly restore directions, then output 0. | Input: ['6ADD 1ACCEPT 1ADD 2ACCEPT 2ADD 3ACCEPT 3'] Output:['8'] | [
2
] |
Let s(x) be sum of digits in decimal representation of positive integer x. Given two integers n and m, find some positive integers a and b such that s(a) >= n, s(b) >= n, s(a + b) <= m. | Input: ['6 5'] Output:['6 7'] | [
3
] |
The campus has m rooms numbered from 0 to m - 1. Also the x-mouse lives in the campus. The x-mouse is not just a mouse: each second x-mouse moves from room i to the room i \cdot x \mod{m} (in fact, it teleports from one room to another since it doesn't visit any intermediate room). Starting position of the x-mouse is unknown.You are responsible to catch the x-mouse in the campus, so you are guessing about minimum possible number of traps (one trap in one room) you need to place. You are sure that if the x-mouse enters a trapped room, it immediately gets caught.And the only observation you made is \text{GCD} (x, m) = 1. | Input: ['4 3'] Output:['3'] | [
3
] |
Polycarp studies in Berland State University. Soon he will have to take his exam. He has to pass exactly n exams.For the each exam i there are known two days: a_i — day of the first opportunity to pass the exam, b_i — day of the second opportunity to pass the exam (a_i < b_i). Polycarp can pass at most one exam during each day. For each exam Polycarp chooses by himself which day he will pass this exam. He has to pass all the n exams.Polycarp wants to pass all the exams as soon as possible. Print the minimum index of day by which Polycarp can pass all the n exams, or print -1 if he cannot pass all the exams at all. | Input: ['21 51 7'] Output:['5'] | [
4
] |
You are given a square board, consisting of n rows and n columns. Each tile in it should be colored either white or black.Let's call some coloring beautiful if each pair of adjacent rows are either the same or different in every position. The same condition should be held for the columns as well.Let's call some coloring suitable if it is beautiful and there is no rectangle of the single color, consisting of at least k tiles.Your task is to count the number of suitable colorings of the board of the given size.Since the answer can be very large, print it modulo 998244353. | Input: ['1 1'] Output:['0'] | [
3
] |
You have n sticks of the given lengths.Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.Let S be the area of the rectangle and P be the perimeter of the rectangle. The chosen rectangle should have the value \frac{P^2}{S} minimal possible. The value is taken without any rounding.If there are multiple answers, print any of them.Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. | Input: ['347 2 2 782 8 1 4 8 2 1 555 5 5 5 5'] Output:['2 7 7 22 2 1 15 5 5 5'] | [
2
] |
You are given a chessboard of size n * n. It is filled with numbers from 1 to n^2 in the following way: the first \lceil \frac{n^2}{2} \rceil numbers from 1 to \lceil \frac{n^2}{2} \rceil are written in the cells with even sum of coordinates from left to right from top to bottom. The rest n^2 - \lceil \frac{n^2}{2} \rceil numbers from \lceil \frac{n^2}{2} \rceil + 1 to n^2 are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation \lceil\frac{x}{y}\rceil means division x by y rounded up.For example, the left board on the following picture is the chessboard which is given for n=4 and the right board is the chessboard which is given for n=5. You are given q queries. The i-th query is described as a pair x_i, y_i. The answer to the i-th query is the number written in the cell x_i, y_i (x_i is the row, y_i is the column). Rows and columns are numbered from 1 to n. | Input: ['4 51 14 44 33 22 4'] Output:['1816134'] | [
3
] |
There are n startups. Startups can be active or acquired. If a startup is acquired, then that means it has exactly one active startup that it is following. An active startup can have arbitrarily many acquired startups that are following it. An active startup cannot follow any other startup.The following steps happen until there is exactly one active startup. The following sequence of steps takes exactly 1 day. Two distinct active startups A, B, are chosen uniformly at random. A fair coin is flipped, and with equal probability, A acquires B or B acquires A (i.e. if A acquires B, then that means B's state changes from active to acquired, and its starts following A). When a startup changes from active to acquired, all of its previously acquired startups become active. For example, the following scenario can happen: Let's say A, B are active startups. C, D, E are acquired startups under A, and F, G are acquired startups under B: Active startups are shown in red. If A acquires B, then the state will be A, F, G are active startups. C, D, E, B are acquired startups under A. F and G have no acquired startups: If instead, B acquires A, then the state will be B, C, D, E are active startups. F, G, A are acquired startups under B. C, D, E have no acquired startups: You are given the initial state of the startups. For each startup, you are told if it is either acquired or active. If it is acquired, you are also given the index of the active startup that it is following.You're now wondering, what is the expected number of days needed for this process to finish with exactly one active startup at the end.It can be shown the expected number of days can be written as a rational number P/Q, where P and Q are co-prime integers, and Q \not= 0 \pmod{10^9+7}. Return the value of P \cdot Q^{-1} modulo 10^9+7. | Input: ['3-1 -1 -1'] Output:['3'] | [
3
] |
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset. To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed 1.Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found here. | Input: ['63 6 9 18 36 108'] Output:['Yes'] | [
0,
3
] |
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers.For a given list of pairs of integers (a_1, b_1), (a_2, b_2), ..., (a_n, b_n) their WCD is arbitrary integer greater than 1, such that it divides at least one element in each pair. WCD may not exist for some lists.For example, if the list looks like [(12, 15), (25, 18), (10, 24)], then their WCD can be equal to 2, 3, 5 or 6 (each of these numbers is strictly greater than 1 and divides at least one number in each pair).You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. | Input: ['317 1815 2412 15'] Output:['6'] | [
0,
2
] |
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You are given a regular bracket sequence s and an integer number k. Your task is to find a regular bracket sequence of length exactly k such that it is also a subsequence of s.It is guaranteed that such sequence always exists. | Input: ['6 4()(())'] Output:['()()'] | [
2
] |
Tanechka is shopping in the toy shop. There are exactly n toys in the shop for sale, the cost of the i-th toy is i burles. She wants to choose two toys in such a way that their total cost is k burles. How many ways to do that does she have?Each toy appears in the shop exactly once. Pairs (a, b) and (b, a) are considered equal. Pairs (a, b), where a=b, are not allowed. | Input: ['8 5'] Output:['2'] | [
3
] |
You are given two strings s and t. The string s consists of lowercase Latin letters and at most one wildcard character '*', the string t consists only of lowercase Latin letters. The length of the string s equals n, the length of the string t equals m.The wildcard character '*' in the string s (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of s can be replaced with anything. If it is possible to replace a wildcard character '*' in s to obtain a string t, then the string t matches the pattern s.For example, if s="aba*aba" then the following strings match it "abaaba", "abacaba" and "abazzzaba", but the following strings do not match: "ababa", "abcaaba", "codeforces", "aba1aba", "aba?aba".If the given string t matches the given string s, print "YES", otherwise print "NO". | Input: ['6 10code*scodeforces'] Output:['YES'] | [
0
] |
In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of n students doing yet another trick. Let's assume that all these students are numbered from 1 to n. The teacher came to student a and put a hole in his badge. The student, however, claimed that the main culprit is some other student p_a.After that, the teacher came to student p_a and made a hole in his badge as well. The student in reply said that the main culprit was student p_{p_a}.This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.You don't know the first student who was caught by the teacher. However, you know all the numbers p_i. Your task is to find out for every student a, who would be the student with two holes in the badge if the first caught student was a. | Input: ['32 3 2'] Output:['2 2 3 '] | [
0
] |
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations. | Input: ['3 6 2 3 31 2 1 31 4 3 41 2 2 3'] Output:['142'] | [
3
] |
There is a strange peculiarity: if you connect the cities of Rostov, Taganrog and Shakhty, peculiarly, you get a triangle«Unbelievable But True»Students from many different parts of Russia and abroad come to Summer Informatics School. You marked the hometowns of the SIS participants on a map.Now you decided to prepare an interesting infographic based on this map. The first thing you chose to do is to find three cities on this map, such that they form a triangle with area S. | Input: ['3 70 03 00 4'] Output:['No'] | [
4
] |
This is an interactive problem.Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions. | Input: ['822'] Output:['? 4? 8! 4'] | [
4
] |
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon.Elections are coming. You know the number of voters and the number of parties — n and m respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give i-th voter c_i bytecoins you can ask him to vote for any other party you choose.The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. | Input: ['1 21 100'] Output:['0'] | [
0,
2
] |
In "The Man in the High Castle" world, there are m different film endings. Abendsen owns a storage and a shelf. At first, he has n ordered films on the shelf. In the i-th month he will do: Empty the storage. Put k_i \cdot m films into the storage, k_i films for each ending. He will think about a question: if he puts all the films from the shelf into the storage, then randomly picks n films (from all the films in the storage) and rearranges them on the shelf, what is the probability that sequence of endings in [l_i, r_i] on the shelf will not be changed? Notice, he just thinks about this question, so the shelf will not actually be changed.Answer all Abendsen's questions.Let the probability be fraction P_i. Let's say that the total number of ways to take n films from the storage for i-th month is A_i, so P_i \cdot A_i is always an integer. Print for each month P_i \cdot A_i \pmod {998244353}.998244353 is a prime number and it is equal to 119 \cdot 2^{23} + 1.It is guaranteed that there will be only no more than 100 different k values. | Input: ['6 4 41 2 3 4 4 41 4 01 3 21 4 21 5 2'] Output:['626730121504860'] | [
0
] |
Notice: unusual memory limit!After the war, destroyed cities in the neutral zone were restored. And children went back to school.The war changed the world, as well as education. In those hard days, a new math concept was created.As we all know, logarithm function can be described as: \log(p_1^{a_1}p_2^{a_2}...p_k^{a_2}) = a_1 \log p_1 + a_2 \log p_2 + ... + a_k \log p_k Where p_1^{a_1}p_2^{a_2}...p_k^{a_2} is the prime factorization of a integer. A problem is that the function uses itself in the definition. That is why it is hard to calculate.So, the mathematicians from the neutral zone invented this: \text{exlog}_f(p_1^{a_1}p_2^{a_2}...p_k^{a_2}) = a_1 f(p_1) + a_2 f(p_2) + ... + a_k f(p_k) Notice that \text{exlog}_f(1) is always equal to 0.This concept for any function f was too hard for children. So teachers told them that f can only be a polynomial of degree no more than 3 in daily uses (i.e., f(x) = Ax^3+Bx^2+Cx+D)."Class is over! Don't forget to do your homework!" Here it is: \sum_{i=1}^n \text{exlog}_f(i) Help children to do their homework. Since the value can be very big, you need to find the answer modulo 2^{32}. | Input: ['12 0 0 1 0'] Output:['63'] | [
0,
3
] |
Childan is making up a legendary story and trying to sell his forgery — a necklace with a strong sense of "Wu" to the Kasouras. But Mr. Kasoura is challenging the truth of Childan's story. So he is going to ask a few questions about Childan's so-called "personal treasure" necklace.This "personal treasure" is a multiset S of m "01-strings".A "01-string" is a string that contains n characters "0" and "1". For example, if n=4, strings "0110", "0000", and "1110" are "01-strings", but "00110" (there are 5 characters, not 4) and "zero" (unallowed characters) are not.Note that the multiset S can contain equal elements.Frequently, Mr. Kasoura will provide a "01-string" t and ask Childan how many strings s are in the multiset S such that the "Wu" value of the pair (s, t) is not greater than k. Mrs. Kasoura and Mr. Kasoura think that if s_i = t_i (1<=q i<=q n) then the "Wu" value of the character pair equals to w_i, otherwise 0. The "Wu" value of the "01-string" pair is the sum of the "Wu" values of every character pair. Note that the length of every "01-string" is equal to n.For example, if w=[4, 5, 3, 6], "Wu" of ("1001", "1100") is 7 because these strings have equal characters only on the first and third positions, so w_1+w_3=4+3=7.You need to help Childan to answer Mr. Kasoura's queries. That is to find the number of strings in the multiset S such that the "Wu" value of the pair is not greater than k. | Input: ['2 4 540 200101101100 2000 4011 2011 4011 60'] Output:['24234'] | [
0
] |
Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number!The only thing Mrs. Smith remembered was that any permutation of n can be a secret phone number. Only those permutations that minimize secret value might be the phone of her husband.The sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once.The secret value of a phone number is defined as the sum of the length of the longest increasing subsequence (LIS) and length of the longest decreasing subsequence (LDS). A subsequence a_{i_1}, a_{i_2}, ..., a_{i_k} where 1<=q i_1 < i_2 < ... < i_k<=q n is called increasing if a_{i_1} < a_{i_2} < a_{i_3} < ... < a_{i_k}. If a_{i_1} > a_{i_2} > a_{i_3} > ... > a_{i_k}, a subsequence is called decreasing. An increasing/decreasing subsequence is called longest if it has maximum length among all increasing/decreasing subsequences.For example, if there is a permutation [6, 4, 1, 7, 2, 3, 5], LIS of this permutation will be [1, 2, 3, 5], so the length of LIS is equal to 4. LDS can be [6, 4, 1], [6, 4, 2], or [6, 4, 3], so the length of LDS is 3.Note, the lengths of LIS and LDS can be different.So please help Mrs. Smith to find a permutation that gives a minimum sum of lengths of LIS and LDS. | Input: ['4'] Output:['3 4 1 2'] | [
2
] |
Rudolf is on his way to the castle. Before getting into the castle, the security staff asked him a question:Given two binary numbers a and b of length n. How many different ways of swapping two digits in a (only in a, not b) so that bitwise OR of these two numbers will be changed? In other words, let c be the bitwise OR of a and b, you need to find the number of ways of swapping two bits in a so that bitwise OR will not be equal to c.Note that binary numbers can contain leading zeros so that length of each number is exactly n.Bitwise OR is a binary operation. A result is a binary number which contains a one in each digit if there is a one in at least one of the two numbers. For example, 01010_2 OR 10011_2 = 11011_2.Well, to your surprise, you are not Rudolf, and you don't need to help him... You are the security staff! Please find the number of ways of swapping two bits in a so that bitwise OR will be changed. | Input: ['50101111001'] Output:['4'] | [
3
] |
Since next season are coming, you'd like to form a team from two or three participants. There are n candidates, the i-th candidate has rank a_i. But you have weird requirements for your teammates: if you have rank v and have chosen the i-th and j-th candidate, then GCD(v, a_i) = X and LCM(v, a_j) = Y must be met.You are very experienced, so you can change your rank to any non-negative integer but X and Y are tied with your birthdate, so they are fixed.Now you want to know, how many are there pairs (i, j) such that there exists an integer v meeting the following constraints: GCD(v, a_i) = X and LCM(v, a_j) = Y. It's possible that i = j and you form a team of two.GCD is the greatest common divisor of two number, LCM — the least common multiple. | Input: ['12 2 21 2 3 4 5 6 7 8 9 10 11 12'] Output:['12'] | [
3
] |
There is a light source on the plane. This source is so small that it can be represented as point. The light source is moving from point (a, s_y) to the (b, s_y) (s_y < 0) with speed equal to 1 unit per second. The trajectory of this light source is a straight segment connecting these two points. There is also a fence on OX axis represented as n segments (l_i, r_i) (so the actual coordinates of endpoints of each segment are (l_i, 0) and (r_i, 0)). The point (x, y) is in the shade if segment connecting (x,y) and the current position of the light source intersects or touches with any segment of the fence. You are given q points. For each point calculate total time of this point being in the shade, while the light source is moving from (a, s_y) to the (b, s_y). | Input: ['-3 1 622 46 753 11 36 16 47 6'] Output:['5.0000000000000003.0000000000000000.0000000000000001.5000000000000002.000000000000000'] | [
4
] |
Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the matrix that the teacher has constructed!Vasya knows that the matrix consists of n rows and m columns. For each row, he knows the xor (bitwise excluding or) of the elements in this row. The sequence a1, a2, ..., an denotes the xor of elements in rows with indices 1, 2, ..., n, respectively. Similarly, for each column, he knows the xor of the elements in this column. The sequence b1, b2, ..., bm denotes the xor of elements in columns with indices 1, 2, ..., m, respectively.Help Vasya! Find a matrix satisfying the given constraints or tell him that there is no suitable matrix. | Input: ['2 32 95 3 13'] Output:['YES3 4 56 7 8'] | [
3
] |
You are given two strings s and t, both consisting only of lowercase Latin letters.The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.Each of the occurrences of string a in a string b is a position i (1 <= i <= |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. | Input: ['10 3 4codeforcesfor1 33 105 65 7'] Output:['0101'] | [
0
] |
You received a notebook which is called Death Note. This notebook has infinite number of pages. A rule is written on the last page (huh) of this notebook. It says: "You have to write names in this notebook during n consecutive days. During the i-th day you have to write exactly a_i names.". You got scared (of course you got scared, who wouldn't get scared if he just receive a notebook which is named Death Note with a some strange rule written in it?).Of course, you decided to follow this rule. When you calmed down, you came up with a strategy how you will write names in the notebook. You have calculated that each page of the notebook can contain exactly m names. You will start writing names from the first page. You will write names on the current page as long as the limit on the number of names on this page is not exceeded. When the current page is over, you turn the page. Note that you always turn the page when it ends, it doesn't matter if it is the last day or not. If after some day the current page still can hold at least one name, during the next day you will continue writing the names from the current page.Now you are interested in the following question: how many times will you turn the page during each day? You are interested in the number of pages you will turn each day from 1 to n. | Input: ['3 53 7 9'] Output:['0 2 1 '] | [
2,
3
] |
A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length 0 are not allowed).Let's consider empty cells are denoted by '.', then the following figures are stars: The leftmost figure is a star of size 1, the middle figure is a star of size 2 and the rightmost figure is a star of size 3. You are given a rectangular grid of size n * m consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from 1 to n, columns are numbered from 1 to m. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed n \cdot m. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most n \cdot m stars. | Input: ['6 8....*......**.....*****....**.......*...........'] Output:['33 4 13 5 23 5 1'] | [
2,
4
] |
A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length 0 are not allowed).Let's consider empty cells are denoted by '.', then the following figures are stars: The leftmost figure is a star of size 1, the middle figure is a star of size 2 and the rightmost figure is a star of size 3. You are given a rectangular grid of size n * m consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from 1 to n, columns are numbered from 1 to m. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed n \cdot m. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most n \cdot m stars. | Input: ['6 8....*......**.....*****....**.......*...........'] Output:['33 4 13 5 23 5 1'] | [
0,
2
] |
There are n houses in a row. They are numbered from 1 to n in order from left to right. Initially you are in the house 1.You have to perform k moves to other house. In one move you go from your current house to some other house. You can't stay where you are (i.e., in each move the new house differs from the current house). If you go from the house x to the house y, the total distance you walked increases by |x-y| units of distance, where |a| is the absolute value of a. It is possible to visit the same house multiple times (but you can't visit the same house in sequence).Your goal is to walk exactly s units of distance in total.If it is impossible, print "NO". Otherwise print "YES" and any of the ways to do that. Remember that you should do exactly k moves. | Input: ['10 2 15'] Output:['YES10 4 '] | [
2
] |
There is an array with n elements a1, a2, ..., an and the number x.In one operation you can select some i (1 ≤ i ≤ n) and replace element ai with ai & x, where & denotes the bitwise and operation.You want the array to have at least two equal elements after applying some operations (possibly, none). In other words, there should be at least two distinct indices i ≠ j such that ai = aj. Determine whether it is possible to achieve and, if possible, the minimal number of operations to apply. | Input: ['4 31 2 3 7'] Output:['1'] | [
2
] |
There is a beautiful garden of stones in Innopolis.Its most beautiful place is the n piles with stones numbered from 1 to n.EJOI participants have visited this place twice. When they first visited it, the number of stones in piles was x_1, x_2, ..., x_n, correspondingly. One of the participants wrote down this sequence in a notebook. They visited it again the following day, and the number of stones in piles was equal to y_1, y_2, ..., y_n. One of the participants also wrote it down in a notebook.It is well known that every member of the EJOI jury during the night either sits in the room 108 or comes to the place with stones. Each jury member who comes there either takes one stone for himself or moves one stone from one pile to another. We can assume that there is an unlimited number of jury members. No one except the jury goes to the place with stones at night.Participants want to know whether their notes can be correct or they are sure to have made a mistake. | Input: ['51 2 3 4 52 1 4 3 5'] Output:['Yes'] | [
3
] |
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 <= i_j <= n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 \to i_2 \to ... i_k \to i_1.For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. | Input: ['5 53 2 3 1 1'] Output:['151 4 2 3 5 '] | [
3
] |
Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes.Strictly speaking, it makes a photo of all points with coordinates (x, y), such that x_1 <=q x <=q x_2 and y_1 <=q y <=q y_2, where (x_1, y_1) and (x_2, y_2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero.After taking the photo, Pavel wrote down coordinates of n of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky.Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points.Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. | Input: ['44 1 3 2 3 2 1 3'] Output:['1'] | [
0,
3
] |
Natasha is planning an expedition to Mars for n people. One of the important tasks is to provide food for each participant.The warehouse has m daily food packages. Each package has some food type a_i.Each participant must eat exactly one food package each day. Due to extreme loads, each participant must eat the same food type throughout the expedition. Different participants may eat different (or the same) types of food.Formally, for each participant j Natasha should select his food type b_j and each day j-th participant will eat one food package of type b_j. The values b_j for different participants may be different.What is the maximum possible number of days the expedition can last, following the requirements above? | Input: ['4 101 5 2 1 1 1 2 5 7 2'] Output:['2'] | [
0,
4
] |
Natasha is going to fly to Mars. She needs to build a rocket, which consists of several stages in some order. Each of the stages is defined by a lowercase Latin letter. This way, the rocket can be described by the string — concatenation of letters, which correspond to the stages.There are n stages available. The rocket must contain exactly k of them. Stages in the rocket should be ordered by their weight. So, after the stage with some letter can go only stage with a letter, which is at least two positions after in the alphabet (skipping one letter in between, or even more). For example, after letter 'c' can't go letters 'a', 'b', 'c' and 'd', but can go letters 'e', 'f', ..., 'z'.For the rocket to fly as far as possible, its weight should be minimal. The weight of the rocket is equal to the sum of the weights of its stages. The weight of the stage is the number of its letter in the alphabet. For example, the stage 'a 'weighs one ton,' b 'weighs two tons, and' z' — 26 tons.Build the rocket with the minimal weight or determine, that it is impossible to build a rocket at all. Each stage can be used at most once. | Input: ['5 3xyabd'] Output:['29'] | [
2
] |
This is an interactive problem.Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet.Let's define x as the distance to Mars. Unfortunately, Natasha does not know x. But it is known that 1 <= x <= m, where Natasha knows the number m. Besides, x and m are positive integers.Natasha can ask the rocket questions. Every question is an integer y (1 <= y <= m). The correct answer to the question is -1, if x<y, 0, if x=y, and 1, if x>y. But the rocket is broken — it does not always answer correctly. Precisely: let the correct answer to the current question be equal to t, then, if the rocket answers this question correctly, then it will answer t, otherwise it will answer -t.In addition, the rocket has a sequence p of length n. Each element of the sequence is either 0 or 1. The rocket processes this sequence in the cyclic order, that is 1-st element, 2-nd, 3-rd, ..., (n-1)-th, n-th, 1-st, 2-nd, 3-rd, ..., (n-1)-th, n-th, .... If the current element is 1, the rocket answers correctly, if 0 — lies. Natasha doesn't know the sequence p, but she knows its length — n.You can ask the rocket no more than 60 questions.Help Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions.Your solution will not be accepted, if it does not receive an answer 0 from the rocket (even if the distance to Mars is uniquely determined by the already received rocket's answers). | Input: ['5 21-1-110'] Output:['12453'] | [
4
] |
Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on n - 2 intermediate planets. Formally: we number all the planets from 1 to n. 1 is Earth, n is Mars. Natasha will make exactly n flights: 1 \to 2 \to ... n \to 1.Flight from x to y consists of two phases: take-off from planet x and landing to planet y. This way, the overall itinerary of the trip will be: the 1-st planet \to take-off from the 1-st planet \to landing to the 2-nd planet \to 2-nd planet \to take-off from the 2-nd planet \to ... \to landing to the n-th planet \to the n-th planet \to take-off from the n-th planet \to landing to the 1-st planet \to the 1-st planet.The mass of the rocket together with all the useful cargo (but without fuel) is m tons. However, Natasha does not know how much fuel to load into the rocket. Unfortunately, fuel can only be loaded on Earth, so if the rocket runs out of fuel on some other planet, Natasha will not be able to return home. Fuel is needed to take-off from each planet and to land to each planet. It is known that 1 ton of fuel can lift off a_i tons of rocket from the i-th planet or to land b_i tons of rocket onto the i-th planet. For example, if the weight of rocket is 9 tons, weight of fuel is 3 tons and take-off coefficient is 8 (a_i = 8), then 1.5 tons of fuel will be burnt (since 1.5 \cdot 8 = 9 + 3). The new weight of fuel after take-off will be 1.5 tons. Please note, that it is allowed to burn non-integral amount of fuel during take-off or landing, and the amount of initial fuel can be non-integral as well.Help Natasha to calculate the minimum mass of fuel to load into the rocket. Note, that the rocket must spend fuel to carry both useful cargo and the fuel itself. However, it doesn't need to carry the fuel which has already been burnt. Assume, that the rocket takes off and lands instantly. | Input: ['21211 87 5'] Output:['10.0000000000'] | [
3,
4
] |
Polycarp has just launched his new startup idea. The niche is pretty free and the key vector of development sounds really promising, so he easily found himself some investors ready to sponsor the company. However, he is yet to name the startup!Actually, Polycarp has already came up with the name but some improvement to it will never hurt. So now he wants to swap letters at some positions in it to obtain the better name. It isn't necessary for letters to be adjacent.In addition, each of the investors has chosen some index in the name and selected a set of letters that can go there. Indices chosen by different investors are pairwise distinct. If some indices aren't chosen by any investor then any letter can go there.Finally, Polycarp is sure that the smallest lexicographically name is the best. (Like why do you think Google decided to become Alphabet?)More formally, you are given a string consisting of lowercase Latin letters from "a" to "f". You can swap letters at any positions arbitrary number of times (zero swaps is also possible).What is the smallest lexicographically name you can obtain such that the letter at every position is among the allowed letters?If Polycarp can't produce any valid name then print "Impossible". | Input: ['bedefead52 e1 dc5 b7 ef6 ef'] Output:['deadbeef'] | [
2
] |
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n km. Let's say that Moscow is situated at the point with coordinate 0 km, and Saratov — at coordinate n km.Driving for a long time may be really difficult. Formally, if Leha has already covered i kilometers since he stopped to have a rest, he considers the difficulty of covering (i + 1)-th kilometer as a_{i + 1}. It is guaranteed that for every i \in [1, n - 1] a_i <= a_{i + 1}. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1 to n - 1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a_1, the kilometer after it — difficulty a_2, and so on.For example, if n = 5 and there is a rest site in coordinate 2, the difficulty of journey will be 2a_1 + 2a_2 + a_3: the first kilometer will have difficulty a_1, the second one — a_2, then Leha will have a rest, and the third kilometer will have difficulty a_1, the fourth — a_2, and the last one — a_3. Another example: if n = 7 and there are rest sites in coordinates 1 and 5, the difficulty of Leha's journey is 3a_1 + 2a_2 + a_3 + a_4.Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2^{n - 1} different distributions of rest sites (two distributions are different if there exists some point x such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p — the expected value of difficulty of his journey.Obviously, p \cdot 2^{n - 1} is an integer number. You have to calculate it modulo 998244353. | Input: ['21 2'] Output:['5'] | [
3
] |
Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) \in E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v, u) doesn't matter. The vertices are numbered from 1 to |V|.Construct a relatively prime graph with n vertices and m edges such that it is connected and it contains neither self-loops nor multiple edges.If there exists no valid graph with the given number of vertices and edges then output "Impossible".If there are multiple answers then print any of them. | Input: ['5 6'] Output:['Possible2 53 25 13 44 15 4'] | [
0,
2,
3
] |
Alice got an array of length n as a birthday present once again! This is the third year in a row! And what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice.Bob has chosen m changes of the following form. For some integer numbers x and d, he chooses an arbitrary position i (1 <= i <= n) and for every j \in [1, n] adds x + d \cdot dist(i, j) to the value of the j-th cell. dist(i, j) is the distance between positions i and j (i.e. dist(i, j) = |i - j|, where |x| is an absolute value of x).For example, if Alice currently has an array [2, 1, 2, 2] and Bob chooses position 3 for x = -1 and d = 2 then the array will become [2 - 1 + 2 \cdot 2,~1 - 1 + 2 \cdot 1,~2 - 1 + 2 \cdot 0,~2 - 1 + 2 \cdot 1] = [5, 2, 1, 3]. Note that Bob can't choose position i outside of the array (that is, smaller than 1 or greater than n).Alice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric.What is the maximum arithmetic mean value Bob can achieve? | Input: ['2 3-1 30 0-1 -4'] Output:['-2.500000000000000'] | [
2,
3
] |
You are given a ternary string (it is a string which consists only of characters '0', '1' and '2').You can swap any two adjacent (consecutive) characters '0' and '1' (i.e. replace "01" with "10" or vice versa) or any two adjacent (consecutive) characters '1' and '2' (i.e. replace "12" with "21" or vice versa).For example, for string "010210" we can perform the following moves: "010210" \rightarrow "100210"; "010210" \rightarrow "001210"; "010210" \rightarrow "010120"; "010210" \rightarrow "010201". Note than you cannot swap "02" \rightarrow "20" and vice versa. You cannot perform any other operations with the given string excluding described above.You task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times (possibly, zero).String a is lexicographically less than string b (if strings a and b have the same length) if there exists some position i (1 <= i <= |a|, where |s| is the length of the string s) such that for every j < i holds a_j = b_j, and a_i < b_i. | Input: ['100210'] Output:['001120'] | [
2
] |
There are n rectangles in a row. You can either turn each rectangle by 90 degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can turn all or none of them. You can not change the order of the rectangles.Find out if there is a way to make the rectangles go in order of non-ascending height. In other words, after all the turns, a height of every rectangle has to be not greater than the height of the previous rectangle (if it is such). | Input: ['33 44 63 5'] Output:['YES'] | [
2
] |
This is an interactive problem.Vasya and Vitya play a game. Vasya thought of two integers a and b from 1 to n and Vitya tries to guess them. Each round he tells Vasya two numbers x and y from 1 to n. If both x=a and y=b then Vitya wins. Else Vasya must say one of the three phrases: x is less than a; y is less than b; x is greater than a or y is greater than b. Vasya can't lie, but if multiple phrases are true, he may choose any of them. For example, if Vasya thought of numbers 2 and 4, then he answers with the phrase 3 to a query (3, 4), and he can answer with the phrase 1 or phrase 3 to a query (1, 5).Help Vitya win in no more than 600 rounds. | Input: ['533210'] Output:['4 33 43 31 52 4'] | [
4
] |
You are given a rectangular parallelepiped with sides of positive integer lengths A, B and C. Find the number of different groups of three integers (a, b, c) such that 1<=q a<=q b<=q c and parallelepiped A* B* C can be paved with parallelepipeds a* b* c. Note, that all small parallelepipeds have to be rotated in the same direction.For example, parallelepiped 1* 5* 6 can be divided into parallelepipeds 1* 3* 5, but can not be divided into parallelepipeds 1* 2* 3. | Input: ['41 1 11 6 12 2 2100 100 100'] Output:['144165'] | [
0,
3
] |
You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such integers.For instance, if we are given an array [10, 20, 30, 40], we can permute it so that it becomes [20, 40, 10, 30]. Then on the first and the second positions the integers became larger (20>10, 40>20) and did not on the third and the fourth, so for this permutation, the number that Vasya wants to maximize equals 2. Read the note for the first example, there is one more demonstrative test case.Help Vasya to permute integers in such way that the number of positions in a new array, where integers are greater than in the original one, is maximal. | Input: ['710 1 1 1 5 5 3'] Output:['4'] | [
3
] |
There is a rectangular grid of size n * m. Each cell has a number written on it; the number on the cell (i, j) is a_{i, j}. Your task is to calculate the number of paths from the upper-left cell (1, 1) to the bottom-right cell (n, m) meeting the following constraints: You can move to the right or to the bottom only. Formally, from the cell (i, j) you may move to the cell (i, j + 1) or to the cell (i + 1, j). The target cell can't be outside of the grid. The xor of all the numbers on the path from the cell (1, 1) to the cell (n, m) must be equal to k (xor operation is the bitwise exclusive OR, it is represented as '^' in Java or C++ and "xor" in Pascal). Find the number of such paths in the given grid. | Input: ['3 3 112 1 57 10 012 6 4'] Output:['3'] | [
0
] |
You are given an array d_1, d_2, ..., d_n consisting of n integer numbers.Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array. Let the sum of elements of the first part be sum_1, the sum of elements of the second part be sum_2 and the sum of elements of the third part be sum_3. Among all possible ways to split the array you have to choose a way such that sum_1 = sum_3 and sum_1 is maximum possible.More formally, if the first part of the array contains a elements, the second part of the array contains b elements and the third part contains c elements, then:sum_1 = \sum\limits_{1 <= i <= a}d_i, sum_2 = \sum\limits_{a + 1 <= i <= a + b}d_i, sum_3 = \sum\limits_{a + b + 1 <= i <= a + b + c}d_i.The sum of an empty array is 0.Your task is to find a way to split the array such that sum_1 = sum_3 and sum_1 is maximum possible. | Input: ['51 3 1 1 4'] Output:['5'] | [
4
] |
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a_1, a_2, ..., a_n, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all n problems in exactly k days.Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in k days he will solve all the n problems.The profit of the j-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the j-th day (i.e. if he solves problems with indices from l to r during a day, then the profit of the day is \max\limits_{l <= i <= r}a_i). The total profit of his practice is the sum of the profits over all k days of his practice.You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all n problems between k days satisfying the conditions above in such a way, that the total profit is maximum.For example, if n = 8, k = 3 and a = [5, 4, 2, 6, 5, 1, 9, 2], one of the possible distributions with maximum total profit is: [5, 4, 2], [6, 5], [1, 9, 2]. Here the total profit equals 5 + 6 + 9 = 20. | Input: ['8 35 4 2 6 5 1 9 2'] Output:['203 2 3'] | [
2
] |
There are n cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from 1 to n.It is known that, from the capital (the city with the number 1), you can reach any other city by moving along the roads.The President of Berland plans to improve the country's road network. The budget is enough to repair exactly n-1 roads. The President plans to choose a set of n-1 roads such that: it is possible to travel from the capital to any other city along the n-1 chosen roads, if d_i is the number of roads needed to travel from the capital to city i, moving only along the n-1 chosen roads, then d_1 + d_2 + ... + d_n is minimized (i.e. as minimal as possible). In other words, the set of n-1 roads should preserve the connectivity of the country, and the sum of distances from city 1 to all cities should be minimized (where you can only use the n-1 chosen roads).The president instructed the ministry to prepare k possible options to choose n-1 roads so that both conditions above are met.Write a program that will find k possible ways to choose roads for repair. If there are fewer than k ways, then the program should output all possible valid ways to choose roads. | Input: ['4 4 31 22 31 44 3'] Output:['211101011'] | [
0
] |
Polycarp likes numbers that are divisible by 3.He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.What is the maximum number of numbers divisible by 3 that Polycarp can obtain? | Input: ['3121'] Output:['2'] | [
2
] |
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i!=j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).For example, the following sequences are good: [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum is a power of two. Similarly, such an element can be found for a_2 and a_3), [1, 1, 1, 1023], [7, 39, 89, 25, 89], []. Note that, by definition, an empty sequence (with a length of 0) is good.For example, the following sequences are not good: [16] (for a_1=16, it is impossible to find another element a_j such that their sum is a power of two), [4, 16] (for a_1=4, it is impossible to find another element a_j such that their sum is a power of two), [1, 3, 2, 8, 8, 8] (for a_3=2, it is impossible to find another element a_j such that their sum is a power of two). You are given a sequence a_1, a_2, ..., a_n. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements. | Input: ['64 7 1 5 4 9'] Output:['1'] | [
0,
2
] |
You are given two strings s and t. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by 1. You can't choose a string if it is empty.For example: by applying a move to the string "where", the result is the string "here", by applying a move to the string "a", the result is an empty string "". You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings.Write a program that finds the minimum number of moves to make two given strings s and t equal. | Input: ['testwest'] Output:['2'] | [
0
] |
Sonya likes ice cream very much. She eats it even during programming competitions. That is why the girl decided that she wants to open her own ice cream shops.Sonya lives in a city with n junctions and n-1 streets between them. All streets are two-way and connect two junctions. It is possible to travel from any junction to any other using one or more streets. City Hall allows opening shops only on junctions. The girl cannot open shops in the middle of streets. Sonya has exactly k friends whom she can trust. If she opens a shop, one of her friends has to work there and not to allow anybody to eat an ice cream not paying for it. Since Sonya does not want to skip an important competition, she will not work in shops personally.Sonya wants all her ice cream shops to form a simple path of the length r (1 <= r <= k), i.e. to be located in different junctions f_1, f_2, ..., f_r and there is street between f_i and f_{i+1} for each i from 1 to r-1.The girl takes care of potential buyers, so she also wants to minimize the maximum distance between the junctions to the nearest ice cream shop. The distance between two junctions a and b is equal to the sum of all the street lengths that you need to pass to get from the junction a to the junction b. So Sonya wants to minimize\max_{a} \min_{1 <= i <= r} d_{a,f_i}where a takes a value of all possible n junctions, f_i — the junction where the i-th Sonya's shop is located, and d_{x,y} — the distance between the junctions x and y.Sonya is not sure that she can find the optimal shops locations, that is why she is asking you to help her to open not more than k shops that will form a simple path and the maximum distance between any junction and the nearest shop would be minimal. | Input: ['6 21 2 32 3 44 5 24 6 32 4 6'] Output:['4'] | [
2,
4
] |
Since Sonya has just learned the basics of matrices, she decided to play with them a little bit.Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so.The Manhattan distance between two cells (x_1, y_1) and (x_2, y_2) is defined as |x_1 - x_2| + |y_1 - y_2|. For example, the Manhattan distance between the cells (5, 2) and (7, 1) equals to |5-7|+|2-1|=3. Example of a rhombic matrix. Note that rhombic matrices are uniquely defined by n, m, and the coordinates of the cell containing the zero.She drew a n* m rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of n\cdot m numbers). Note that Sonya will not give you n and m, so only the sequence of numbers in this matrix will be at your disposal.Write a program that finds such an n* m rhombic matrix whose elements are the same as the elements in the sequence in some order. | Input: ['201 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4'] Output:['4 52 2'] | [
0
] |
Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.There are n flowers in a row in the exhibition. Sonya can put either a rose or a lily in the i-th position. Thus each of n positions should contain exactly one flower: a rose or a lily.She knows that exactly m people will visit this exhibition. The i-th visitor will visit all flowers from l_i to r_i inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible. | Input: ['5 31 32 42 5'] Output:['01100'] | [
2,
3
] |
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.The queries are independent (the answer on the query doesn't affect Polycarp's coins). | Input: ['5 42 4 8 2 4851410'] Output:['1-132'] | [
2
] |
The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are.Mathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows:Suppose we want to analyze the segment of n consecutive days. We have measured the temperatures during these n days; the temperature during i-th day equals a_i.We denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day x to day y, we calculate it as \frac{\sum \limits_{i = x}^{y} a_i}{y - x + 1} (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than k consecutive days. For example, if analyzing the measures [3, 4, 1, 2] and k = 3, we are interested in segments [3, 4, 1], [4, 1, 2] and [3, 4, 1, 2] (we want to find the maximum value of average temperature over these segments).You have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task? | Input: ['4 33 4 1 2'] Output:['2.666666666666667'] | [
0,
3
] |
Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment 0 and turn power off at moment M. Moreover, the lamp allows you to set a program of switching its state (states are "lights on" and "lights off"). Unfortunately, some program is already installed into the lamp.The lamp allows only good programs. Good program can be represented as a non-empty array a, where 0 < a_1 < a_2 < ... < a_{|a|} < M. All a_i must be integers. Of course, preinstalled program is a good program.The lamp follows program a in next manner: at moment 0 turns power and light on. Then at moment a_i the lamp flips its state to opposite (if it was lit, it turns off, and vice versa). The state of the lamp flips instantly: for example, if you turn the light off at moment 1 and then do nothing, the total time when the lamp is lit will be 1. Finally, at moment M the lamp is turning its power off regardless of its state.Since you are not among those people who read instructions, and you don't understand the language it's written in, you realize (after some testing) the only possible way to alter the preinstalled program. You can insert at most one element into the program a, so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of a, or even at the begining or at the end of a.Find such a way to alter the program that the total time when the lamp is lit is maximum possible. Maybe you should leave program untouched. If the lamp is lit from x till moment y, then its lit for y - x units of time. Segments of time when the lamp is lit are summed up. | Input: ['3 104 6 7'] Output:['8'] | [
2
] |
Codehorses has just hosted the second Codehorses Cup. This year, the same as the previous one, organizers are giving T-shirts for the winners.The valid sizes of T-shirts are either "M" or from 0 to 3 "X" followed by "S" or "L". For example, sizes "M", "XXS", "L", "XXXL" are valid and "XM", "Z", "XXXXL" are not.There are n winners to the cup for both the previous year and the current year. Ksenia has a list with the T-shirt sizes printed for the last year cup and is yet to send the new list to the printing office. Organizers want to distribute the prizes as soon as possible, so now Ksenia is required not to write the whole list from the scratch but just make some changes to the list of the previous year. In one second she can choose arbitrary position in any word and replace its character with some uppercase Latin letter. Ksenia can't remove or add letters in any of the words.What is the minimal number of seconds Ksenia is required to spend to change the last year list to the current one?The lists are unordered. That means, two lists are considered equal if and only if the number of occurrences of any string is the same in both lists. | Input: ['3XSXSMXLSXS'] Output:['2'] | [
2
] |
There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?New roads will also be one-way. | Input: ['9 9 11 21 32 31 55 66 11 89 87 1'] Output:['3'] | [
2
] |
You are given an array consisting of n integers a_1, a_2, ..., a_n, and a positive integer m. It is guaranteed that m is a divisor of n.In a single move, you can choose any position i between 1 and n and increase a_i by 1.Let's calculate c_r (0 <= r <= m-1) — the number of elements having remainder r when divided by m. In other words, for each remainder, let's find the number of corresponding elements in a with that remainder.Your task is to change the array in such a way that c_0 = c_1 = ... = c_{m-1} = \frac{n}{m}.Find the minimum number of moves to satisfy the above requirement. | Input: ['6 33 2 0 6 10 12'] Output:['33 2 0 7 10 14 '] | [
2
] |
Mishka started participating in a programming contest. There are n problems in the contest. Mishka's problem-solving skill is equal to k.Mishka arranges all problems from the contest into a list. Because of his weird principles, Mishka only solves problems from one of the ends of the list. Every time, he chooses which end (left or right) he will solve the next problem from. Thus, each problem Mishka solves is either the leftmost or the rightmost problem in the list.Mishka cannot solve a problem with difficulty greater than k. When Mishka solves the problem, it disappears from the list, so the length of the list decreases by 1. Mishka stops when he is unable to solve any problem from any end of the list.How many problems can Mishka solve? | Input: ['8 44 2 3 1 5 1 6 4'] Output:['5'] | [
0
] |
There are a lot of things which could be cut — trees, paper, "the rope". In this problem you are going to cut a sequence of integers.There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4, 1, 2, 3, 4, 5, 4, 4, 5, 5] \to two cuts \to [4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]. On each segment the number of even elements should be equal to the number of odd elements.The cost of the cut between x and y numbers is |x - y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins. | Input: ['6 41 2 5 10 15 20'] Output:['1'] | [
2
] |
On one of the planets of Solar system, in Atmosphere University, many students are fans of bingo game.It is well known that one month on this planet consists of n^2 days, so calendars, represented as square matrix n by n are extremely popular.Weather conditions are even more unusual. Due to the unique composition of the atmosphere, when interacting with sunlight, every day sky takes one of three colors: blue, green or red.To play the bingo, you need to observe the sky for one month — after each day, its cell is painted with the color of the sky in that day, that is, blue, green or red.At the end of the month, students examine the calendar. If at least one row or column contains only cells of one color, that month is called lucky.Let's call two colorings of calendar different, if at least one cell has different colors in them. It is easy to see that there are 3^{n \cdot n} different colorings. How much of them are lucky? Since this number can be quite large, print it modulo 998244353. | Input: ['1'] Output:['3'] | [
3
] |
Let's introduce a number system which is based on a roman digits. There are digits I, V, X, L which correspond to the numbers 1, 5, 10 and 50 respectively. The use of other roman digits is not allowed.Numbers in this system are written as a sequence of one or more digits. We define the value of the sequence simply as the sum of digits in it.For example, the number XXXV evaluates to 35 and the number IXI — to 12.Pay attention to the difference to the traditional roman system — in our system any sequence of digits is valid, moreover the order of digits doesn't matter, for example IX means 11, not 9.One can notice that this system is ambiguous, and some numbers can be written in many different ways. Your goal is to determine how many distinct integers can be represented by exactly n roman digits I, V, X, L. | Input: ['1'] Output:['4'] | [
0,
2,
3
] |
You've got a string a_1, a_2, ..., a_n, consisting of zeros and ones.Let's call a sequence of consecutive elements a_i, a_{i + 1}, ..., a_j (1<=q i<=q j<=q n) a substring of string a. You can apply the following operations any number of times: Choose some substring of string a (for example, you can choose entire string) and reverse it, paying x coins for it (for example, «0101101» \to «0111001»); Choose some substring of string a (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying y coins for it (for example, «0101101» \to «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring.What is the minimum number of coins you need to spend to get a string consisting only of ones? | Input: ['5 1 1001000'] Output:['11'] | [
0,
2,
3
] |
Allen wants to enter a fan zone that occupies a round square and has n entrances.There already is a queue of a_i people in front of the i-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute.Allen uses the following strategy to enter the fan zone: Initially he stands in the end of the queue in front of the first entrance. Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance). Determine the entrance through which Allen will finally enter the fan zone. | Input: ['42 3 2 0'] Output:['3'] | [
3,
4
] |
Allen has a LOT of money. He has n dollars in the bank. For security reasons, he wants to withdraw it in cash (we will not disclose the reasons here). The denominations for dollar bills are 1, 5, 10, 20, 100. What is the minimum number of bills Allen could receive after withdrawing his entire balance? | Input: ['125'] Output:['3'] | [
2
] |
Allen, having graduated from the MOO Institute of Techcowlogy (MIT), has started a startup! Allen is the president of his startup. He also hires n-1 other employees, each of which is assigned a direct superior. If u is a superior of v and v is a superior of w then also u is a superior of w. Additionally, there are no u and v such that u is the superior of v and v is the superior of u. Allen himself has no superior. Allen is employee number 1, and the others are employee numbers 2 through n.Finally, Allen must assign salaries to each employee in the company including himself. Due to budget constraints, each employee's salary is an integer between 1 and D. Additionally, no employee can make strictly more than his superior.Help Allen find the number of ways to assign salaries. As this number may be large, output it modulo 10^9 + 7. | Input: ['3 211'] Output:['5'] | [
3
] |
Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n \to \mathbb{R}, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the variables x_1, x_2, ..., x_n are all set to -1. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an i such that x_i = -1 and either setting x_i \to 0 or x_i \to 1.After n rounds all variables are set, and the game value resolves to f(x_1, x_2, ..., x_n). Allen wants to maximize the game value, and Bessie wants to minimize it.Your goal is to help Allen and Bessie find the expected game value! They will play r+1 times though, so between each game, exactly one value of f changes. In other words, between rounds i and i+1 for 1 <= i <= r, f(z_1, ..., z_n) \to g_i for some (z_1, ..., z_n) \in \{0, 1\}^n. You are to find the expected game value in the beginning and after each change. | Input: ['2 20 1 2 32 50 4'] Output:['1.5000002.2500003.250000'] | [
3
] |
For a vector \vec{v} = (x, y), define |v| = \sqrt{x^2 + y^2}.Allen had a bit too much to drink at the bar, which is at the origin. There are n vectors \vec{v_1}, \vec{v_2}, \cdots, \vec{v_n}. Allen will make n moves. As Allen's sense of direction is impaired, during the i-th move he will either move in the direction \vec{v_i} or -\vec{v_i}. In other words, if his position is currently p = (x, y), he will either move to p + \vec{v_i} or p - \vec{v_i}.Allen doesn't want to wander too far from home (which happens to also be the bar). You need to help him figure out a sequence of moves (a sequence of signs for the vectors) such that his final position p satisfies |p| <= 1.5 \cdot 10^6 so that he can stay safe. | Input: ['3999999 00 999999999999 0'] Output:['1 1 -1 '] | [
0,
2,
3
] |
Allen is hosting a formal dinner party. 2n people come to the event in n pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The 2n people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes the picture more aesthetic.Help Allen find the minimum number of swaps of adjacent positions he must perform to make it so that each couple occupies adjacent positions in the line. | Input: ['41 1 2 3 3 2 4 4'] Output:['2'] | [
2,
3
] |
Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no more than k other knights. Also, each knight has some number of coins. After a kill, a knight can pick up all victim's coins.Now each knight ponders: how many coins he can have if only he kills other knights?You should answer this question for each knight. | Input: ['4 24 5 9 71 2 11 33'] Output:['1 3 46 36 '] | [
2
] |
Nikita likes tasks on order statistics, for example, he can easily find the k-th number in increasing order on a segment of an array. But now Nikita wonders how many segments of an array there are such that a given number x is the k-th number in increasing order on this segment. In other words, you should find the number of segments of a given array such that there are exactly k numbers of this segment which are less than x.Nikita wants to get answer for this question for each k from 0 to n, where n is the size of the array. | Input: ['5 31 2 3 4 5'] Output:['6 5 4 0 0 0 '] | [
3
] |
You need to execute several tasks, each associated with number of processors it needs, and the compute power it will consume.You have sufficient number of analog computers, each with enough processors for any task. Each computer can execute up to one task at a time, and no more than two tasks total. The first task can be any, the second task on each computer must use strictly less power than the first. You will assign between 1 and 2 tasks to each computer. You will then first execute the first task on each computer, wait for all of them to complete, and then execute the second task on each computer that has two tasks assigned.If the average compute power per utilized processor (the sum of all consumed powers for all tasks presently running divided by the number of utilized processors) across all computers exceeds some unknown threshold during the execution of the first tasks, the entire system will blow up. There is no restriction on the second tasks execution. Find the lowest threshold for which it is possible.Due to the specifics of the task, you need to print the answer multiplied by 1000 and rounded up. | Input: ['68 10 9 9 8 101 1 1 1 1 1'] Output:['9000'] | [
2,
4
] |
There are two small spaceship, surrounded by two groups of enemy larger spaceships. The space is a two-dimensional plane, and one group of the enemy spaceships is positioned in such a way that they all have integer y-coordinates, and their x-coordinate is equal to -100, while the second group is positioned in such a way that they all have integer y-coordinates, and their x-coordinate is equal to 100.Each spaceship in both groups will simultaneously shoot two laser shots (infinite ray that destroys any spaceship it touches), one towards each of the small spaceships, all at the same time. The small spaceships will be able to avoid all the laser shots, and now want to position themselves at some locations with x=0 (with not necessarily integer y-coordinates), such that the rays shot at them would destroy as many of the enemy spaceships as possible. Find the largest numbers of spaceships that can be destroyed this way, assuming that the enemy spaceships can't avoid laser shots. | Input: ['3 91 2 31 2 3 7 8 9 11 12 13'] Output:['9'] | [
0
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.