question_text
stringlengths
2
3.82k
input_outputs
stringlengths
23
941
algo_tags
sequence
We are sum for we are manySome NumberThis version of the problem differs from the previous one only in the constraint on t. You can make hacks only if both versions of the problem are solved.You are given two positive integers l and r.Count the number of distinct triplets of integers (i, j, k) such that l <= i < j < k <= r and \operatorname{lcm}(i,j,k) >= i + j + k.Here \operatorname{lcm}(i, j, k) denotes the least common multiple (LCM) of integers i, j, and k.
Input: ['51 43 58 8668 866 86868'] Output:['3', '1', '78975', '969', '109229059713337', '']
[ 0, 3 ]
We are sum for we are manySome NumberThis version of the problem differs from the next one only in the constraint on t. You can make hacks only if both versions of the problem are solved.You are given two positive integers l and r.Count the number of distinct triplets of integers (i, j, k) such that l <= i < j < k <= r and \operatorname{lcm}(i,j,k) >= i + j + k.Here \operatorname{lcm}(i, j, k) denotes the least common multiple (LCM) of integers i, j, and k.
Input: ['51 43 58 8668 866 86868'] Output:['3', '1', '78975', '969', '109229059713337', '']
[ 0, 3, 4 ]
β€” Do you have a wish? β€” I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of n positive integers a_1,a_2,...,a_n fell down on you from the skies, along with a positive integer k <= n.You can apply the following operation at most k times: Choose an index 1 <= i <= n and an integer 1 <= x <= 10^9. Then do a_i := x (assign x to a_i). Then build a complete undirected weighted graph with n vertices numbered with integers from 1 to n, where edge (l, r) (1 <= l < r <= n) has weight \min(a_{l},a_{l+1},...,a_{r}).You have to find the maximum possible diameter of the resulting graph after performing at most k operations.The diameter of a graph is equal to \max\limits_{1 <= u < v <= n}{\operatorname{d}(u, v)}, where \operatorname{d}(u, v) is the length of the shortest path between vertex u and vertex v.
Input: ['63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2'] Output:['4', '168', '10', '1000000000', '9', '1000000000', '']
[ 2, 4 ]
An array is sorted if it has no inversionsA Young BoyYou are given an array of n positive integers a_1,a_2,...,a_n. In one operation you do the following: Choose any integer x. For all i such that a_i = x, do a_i := 0 (assign 0 to a_i). Find the minimum number of operations required to sort the array in non-decreasing order.
Input: ['533 3 241 3 1 354 1 5 3 242 4 1 211'] Output:['1', '2', '4', '3', '0', '']
[ 2 ]
I wonder, does the falling rain Forever yearn for it's disdain?Effluvium of the MindYou are given a positive integer n.Find any permutation p of length n such that the sum \operatorname{lcm}(1,p_1) + \operatorname{lcm}(2, p_2) + ... + \operatorname{lcm}(n, p_n) is as large as possible. Here \operatorname{lcm}(x, y) denotes the least common multiple (LCM) of integers x and y.A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Input: ['212'] Output:['1 ', '2 1 ', '']
[ 2 ]
God's Blessing on This PermutationForces!A Random PebbleYou are given a permutation p_1,p_2,...,p_n of length n and a positive integer k <= n. In one operation you can choose two indices i and j (1 <= i < j <= n) and swap p_i with p_j.Find the minimum number of operations needed to make the sum p_1 + p_2 + ... + p_k as small as possible.A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Input: ['43 12 3 13 31 2 34 23 4 1 21 11'] Output:['1', '0', '2', '0', '']
[ 2 ]
A club plans to hold a party and will invite some of its n members. The n members are identified by the numbers 1, 2, ..., n. If member i is not invited, the party will gain an unhappiness value of a_i.There are m pairs of friends among the n members. As per tradition, if both people from a friend pair are invited, they will share a cake at the party. The total number of cakes eaten will be equal to the number of pairs of friends such that both members have been invited.However, the club's oven can only cook two cakes at a time. So, the club demands that the total number of cakes eaten is an even number.What is the minimum possible total unhappiness value of the party, respecting the constraint that the total number of cakes eaten is even?
Input: ['41 013 12 1 31 35 51 2 3 4 51 21 31 41 52 35 51 1 1 1 11 22 33 44 55 1'] Output:['0', '2', '3', '2', '']
[ 0 ]
You are given two arrays of integers a_1,a_2,...,a_n and b_1,b_2,...,b_m. Alice and Bob are going to play a game. Alice moves first and they take turns making a move.They play on a grid of size n * m (a grid with n rows and m columns). Initially, there is a rook positioned on the first row and first column of the grid.During her/his move, a player can do one of the following two operations: Move the rook to a different cell on the same row or the same column of the current cell. A player cannot move the rook to a cell that has been visited 1000 times before (i.e., the rook can stay in a certain cell at most 1000 times during the entire game). Note that the starting cell is considered to be visited once at the beginning of the game. End the game immediately with a score of a_r+b_c, where (r, c) is the current cell (i.e., the rook is on the r-th row and c-th column). Bob wants to maximize the score while Alice wants to minimize it. If they both play this game optimally, what is the final score of the game?
Input: ['2 1', '3 2', '2', ''] Output:['4', '']
[ 4 ]
You are given a positive integer n. Since n may be very large, you are given its binary representation.You should compute the number of triples (a,b,c) with 0 <=q a,b,c <=q n such that a \oplus b, b \oplus c, and a \oplus c are the sides of a non-degenerate triangle. Here, \oplus denotes the bitwise XOR operation.You should output the answer modulo 998\,244\,353.Three positive values x, y, and z are the sides of a non-degenerate triangle if and only if x+y>z, x+z>y, and y+z>x.
Input: ['101'] Output:['12', '']
[ 0, 2, 3 ]
You are the owner of a harvesting field which can be modeled as an infinite line, whose positions are identified by integers.It will rain for the next n days. On the i-th day, the rain will be centered at position x_i and it will have intensity p_i. Due to these rains, some rainfall will accumulate; let a_j be the amount of rainfall accumulated at integer position j. Initially a_j is 0, and it will increase by \max(0,p_i-|x_i-j|) after the i-th day's rain.A flood will hit your field if, at any moment, there is a position j with accumulated rainfall a_j>m.You can use a magical spell to erase exactly one day's rain, i.e., setting p_i=0. For each i from 1 to n, check whether in case of erasing the i-th day's rain there is no flood.
Input: ['43 61 55 53 42 31 35 22 51 610 66 124 51 612 55 59 78 3'] Output:['001', '11', '00', '100110', '']
[ 0, 2, 3, 4 ]
A picture can be represented as an n* m grid (n rows and m columns) so that each of the n \cdot m cells is colored with one color. You have k pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most a_i cells with the i-th pigment.A picture is considered beautiful if each cell has at least 3 toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers 1 <=q x_1,x_2 <=q n and 1 <=q y_1,y_2 <=q m, the cell in the x_1-th row and y_1-th column is a toroidal neighbor of the cell in the x_2-th row and y_2-th column if one of following two conditions holds: x_1-x_2 \equiv \pm1 \pmod{n} and y_1=y_2, or y_1-y_2 \equiv \pm1 \pmod{m} and x_1=x_2. Notice that each cell has exactly 4 toroidal neighbors. For example, if n=3 and m=4, the toroidal neighbors of the cell (1, 2) (the cell on the first row and second column) are: (3, 2), (2, 2), (1, 3), (1, 1). They are shown in gray on the image below: The gray cells show toroidal neighbors of (1, 2). Is it possible to color all cells with the pigments provided and create a beautiful picture?
Input: ['64 6 312 9 83 3 28 83 3 29 54 5 210 115 4 29 1110 10 311 45 14'] Output:['Yes', 'No', 'Yes', 'Yes', 'No', 'No', '']
[ 2, 3 ]
You are given three integers n, k and f.Consider all binary strings (i. e. all strings consisting of characters 0 and/or 1) of length from 1 to n. For every such string s, you need to choose an integer c_s from 0 to k.A multiset of binary strings of length exactly n is considered beautiful if for every binary string s with length from 1 to n, the number of strings in the multiset such that s is their prefix is not exceeding c_s.For example, let n = 2, c_{0} = 3, c_{00} = 1, c_{01} = 2, c_{1} = 1, c_{10} = 2, and c_{11} = 3. The multiset of strings \{11, 01, 00, 01\} is beautiful, since: for the string 0, there are 3 strings in the multiset such that 0 is their prefix, and 3 <= c_0; for the string 00, there is one string in the multiset such that 00 is its prefix, and 1 <= c_{00}; for the string 01, there are 2 strings in the multiset such that 01 is their prefix, and 2 <= c_{01}; for the string 1, there is one string in the multiset such that 1 is its prefix, and 1 <= c_1; for the string 10, there are 0 strings in the multiset such that 10 is their prefix, and 0 <= c_{10}; for the string 11, there is one string in the multiset such that 11 is its prefix, and 1 <= c_{11}. Now, for the problem itself. You have to calculate the number of ways to choose the integer c_s for every binary string s of length from 1 to n in such a way that the maximum possible size of a beautiful multiset is exactly f.
Input: ['1 42 2', ''] Output:['3', '']
[ 0, 3 ]
You are given a tree consisting of n vertices. A number is written on each vertex; the number on vertex i is equal to a_i.Recall that a simple path is a path that visits each vertex at most once. Let the weight of the path be the bitwise XOR of the values written on vertices it consists of. Let's say that a tree is good if no simple path has weight 0.You can apply the following operation any number of times (possibly, zero): select a vertex of the tree and replace the value written on it with an arbitrary positive integer. What is the minimum number of times you have to apply this operation in order to make the tree good?
Input: ['6', '3 2 1 3 2 1', '4 5', '3 4', '1 4', '2 1', '6 1', ''] Output:['2', '']
[ 2 ]
There is a grid, consisting of n rows and m columns. The rows are numbered from 1 to n from bottom to top. The columns are numbered from 1 to m from left to right. The i-th column has the bottom a_i cells blocked (the cells in rows 1, 2, ..., a_i), the remaining n - a_i cells are unblocked.A robot is travelling across this grid. You can send it commands β€” move up, right, down or left. If a robot attempts to move into a blocked cell or outside the grid, it explodes.However, the robot is broken β€” it executes each received command k times. So if you tell it to move up, for example, it will move up k times (k cells). You can't send it commands while the robot executes the current one.You are asked q queries about the robot. Each query has a start cell, a finish cell and a value k. Can you send the robot an arbitrary number of commands (possibly, zero) so that it reaches the finish cell from the start cell, given that it executes each command k times?The robot must stop in the finish cell. If it visits the finish cell while still executing commands, it doesn't count.
Input: ['11 10', '9 0 0 10 3 4 8 11 10 8', '6', '1 2 1 3 1', '1 2 1 3 2', '4 3 4 5 2', '5 3 11 5 3', '5 3 11 5 2', '11 9 9 10 1', ''] Output:['YES', 'NO', 'NO', 'NO', 'YES', 'YES', '']
[ 2, 3, 4 ]
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) 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)"); bracket sequences ")(", "(" and ")" are not. There was an RBS. Some brackets have been replaced with question marks. Is it true that there is a unique way to replace question marks with brackets, so that the resulting sequence is an RBS?
Input: ['5(?))??????()???(?)()?)'] Output:['YES', 'NO', 'YES', 'YES', 'NO', '']
[ 2 ]
There are three doors in front of you, numbered from 1 to 3 from left to right. Each door has a lock on it, which can only be opened with a key with the same number on it as the number on the door.There are three keys β€” one for each door. Two of them are hidden behind the doors, so that there is no more than one key behind each door. So two doors have one key behind them, one door doesn't have a key behind it. To obtain a key hidden behind a door, you should first unlock that door. The remaining key is in your hands.Can you open all the doors?
Input: ['430 1 210 3 223 1 021 3 0'] Output:['YES', 'NO', 'YES', 'NO', '']
[ 0, 2, 3 ]
You are given three integers n, l, and r. You need to construct an array a_1,a_2,...,a_n (l<= a_i<= r) such that \gcd(i,a_i) are all distinct or report there's no solution.Here \gcd(x, y) denotes the greatest common divisor (GCD) of integers x and y.
Input: ['4', '5 1 5', '9 1000 2000', '10 30 35', '1 1000000000 1000000000', ''] Output:['YES', '1 2 3 4 5', 'YES', '1145 1926 1440 1220 1230 1350 1001 1000 1233', 'NO', 'YES', '1000000000', '']
[ 3 ]
You are given an array a consisting of n positive integers.You are allowed to perform this operation any number of times (possibly, zero): choose an index i (2 <= i <= n), and change a_i to a_i - a_{i-1}. Is it possible to make a_i=0 for all 2<= i<= n?
Input: ['425 1031 2 341 1 1 199 9 8 2 4 4 3 5 3'] Output:['YES', 'YES', 'YES', 'NO', '']
[ 2, 3 ]
You are given an integer array a_1,..., a_n, where 1<= a_i <= n for all i.There's a "replace" function f which takes a pair of integers (l, r), where l <= r, as input and outputs the pair f\big( (l, r) \big)=<=ft(\min\{a_l,a_{l+1},...,a_r\},\, \max\{a_l,a_{l+1},...,a_r\}\right).Consider repeated calls of this function. That is, from a starting pair (l, r) we get f\big((l, r)\big), then f\big(f\big((l, r)\big)\big), then f\big(f\big(f\big((l, r)\big)\big)\big), and so on.Now you need to answer q queries. For the i-th query you have two integers l_i and r_i (1<= l_i<= r_i<= n). You must answer the minimum number of times you must apply the "replace" function to the pair (l_i,r_i) to get (1, n), or report that it is impossible.
Input: ['5 6', '2 5 4 1 3', '4 4', '1 5', '1 4', '3 5', '4 5', '2 3', ''] Output:['-1', '0', '1', '2', '3', '4', '']
[ 4 ]
Kawashiro Nitori is a girl who loves competitive programming. One day she found a rooted tree consisting of n vertices. The root is vertex 1. As an advanced problem setter, she quickly thought of a problem.Kawashiro Nitori has a vertex set U=\{1,2,...,n\}. She's going to play a game with the tree and the set. In each operation, she will choose a vertex set T, where T is a partial virtual tree of U, and change U into T.A vertex set S_1 is a partial virtual tree of a vertex set S_2, if S_1 is a subset of S_2, S_1 \neq S_2, and for all pairs of vertices i and j in S_1, \operatorname{LCA}(i,j) is in S_1, where \operatorname{LCA}(x,y) denotes the lowest common ancestor of vertices x and y on the tree. Note that a vertex set can have many different partial virtual trees.Kawashiro Nitori wants to know for each possible k, if she performs the operation exactly k times, in how many ways she can make U=\{1\} in the end? Two ways are considered different if there exists an integer z (1 <= z <= k) such that after z operations the sets U are different.Since the answer could be very large, you need to find it modulo p. It's guaranteed that p is a prime number.
Input: ['4 998244353', '1 2', '2 3', '1 4', ''] Output:['1 6 6 ']
[ 3 ]
You are given a connected undirected graph consisting of n vertices and m edges. The weight of the i-th edge is i.Here is a wrong algorithm of finding a minimum spanning tree (MST) of a graph:vis := an array of length ns := a set of edgesfunction dfs(u): vis[u] := true iterate through each edge (u, v) in the order from smallest to largest edge weight if vis[v] = false add edge (u, v) into the set (s) dfs(v)function findMST(u): reset all elements of (vis) to false reset the edge set (s) to empty dfs(u) return the edge set (s)Each of the calls findMST(1), findMST(2), ..., findMST(n) gives you a spanning tree of the graph. Determine which of these trees are minimum spanning trees.
Input: ['5 5', '1 2', '3 5', '1 3', '3 2', '4 2', ''] Output:['01111', '']
[ 2 ]
You are given an array a consisting of n non-negative integers. It is guaranteed that a is sorted from small to large.For each operation, we generate a new array b_i=a_{i+1}-a_{i} for 1 <= i < n. Then we sort b from small to large, replace a with b, and decrease n by 1.After performing n-1 operations, n becomes 1. You need to output the only integer in array a (that is to say, you need to output a_1).
Input: ['531 10 10044 8 9 1350 0 0 8 1362 4 8 16 32 6470 0 0 0 0 0 0'] Output:['81', '3', '1', '2', '0', '']
[ 0 ]
Doremy is asked to test n contests. Contest i can only be tested on day i. The difficulty of contest i is a_i. Initially, Doremy's IQ is q. On day i Doremy will choose whether to test contest i or not. She can only test a contest if her current IQ is strictly greater than 0.If Doremy chooses to test contest i on day i, the following happens: if a_i>q, Doremy will feel she is not wise enough, so q decreases by 1; otherwise, nothing changes. If she chooses not to test a contest, nothing changes.Doremy wants to test as many contests as possible. Please give Doremy a solution.
Input: ['51 112 11 23 11 2 14 21 4 3 15 25 1 2 4 3'] Output:['1', '11', '110', '1110', '01111', '']
[ 2, 4 ]
You are given a connected undirected graph with n vertices and m edges. Vertices of the graph are numbered by integers from 1 to n and edges of the graph are numbered by integers from 1 to m.Your task is to answer q queries, each consisting of two integers l and r. The answer to each query is the smallest non-negative integer k such that the following condition holds: For all pairs of integers (a, b) such that l<= a<= b<= r, vertices a and b are reachable from one another using only the first k edges (that is, edges 1, 2, ..., k).
Input: ['32 1 21 21 11 25 5 51 21 32 43 43 51 43 42 22 53 53 2 11 32 31 3'] Output:['0 1 ', '3 3 0 5 5 ', '2 ', '']
[ 2, 4 ]
This is the hard version of the problem. The only difference between the versions is the constraints on n, k, a_i, and the sum of n over all test cases. You can make hacks only if both versions of the problem are solved.Note the unusual memory limit.You are given an array of integers a_1, a_2, ..., a_n of length n, and an integer k.The cost of an array of integers p_1, p_2, ..., p_n of length n is \max\limits_{1 <= i <= n}<=ft(<=ft \lfloor \frac{a_i}{p_i} \right \rfloor \right) - \min\limits_{1 <= i <= n}<=ft(<=ft \lfloor \frac{a_i}{p_i} \right \rfloor \right).Here, \lfloor \frac{x}{y} \rfloor denotes the integer part of the division of x by y. Find the minimum cost of an array p such that 1 <= p_i <= k for all 1 <= i <= n.
Input: ['75 24 5 6 8 115 124 5 6 8 113 12 9 157 32 3 5 5 6 9 106 5654 286 527 1436 2450 26813 9516 340 22412 21 3'] Output:['2', '0', '13', '1', '4', '7', '0', '']
[ 0, 2, 3 ]
This is the easy version of the problem. The only difference between the versions is the constraints on n, k, a_i, and the sum of n over all test cases. You can make hacks only if both versions of the problem are solved.Note the unusual memory limit.You are given an array of integers a_1, a_2, ..., a_n of length n, and an integer k.The cost of an array of integers p_1, p_2, ..., p_n of length n is \max\limits_{1 <= i <= n}<=ft(<=ft \lfloor \frac{a_i}{p_i} \right \rfloor \right) - \min\limits_{1 <= i <= n}<=ft(<=ft \lfloor \frac{a_i}{p_i} \right \rfloor \right).Here, \lfloor \frac{x}{y} \rfloor denotes the integer part of the division of x by y. Find the minimum cost of an array p such that 1 <= p_i <= k for all 1 <= i <= n.
Input: ['75 24 5 6 8 115 124 5 6 8 113 12 9 157 32 3 5 5 6 9 106 5654 286 527 1436 2450 26813 9516 340 22412 21 3'] Output:['2', '0', '13', '1', '4', '7', '0', '']
[ 0, 2, 4 ]
Qpwoeirut has taken up architecture and ambitiously decided to remodel his city.Qpwoeirut's city can be described as a row of n buildings, the i-th (1 <= i <= n) of which is h_i floors high. You can assume that the height of every floor in this problem is equal. Therefore, building i is taller than the building j if and only if the number of floors h_i in building i is larger than the number of floors h_j in building j.Building i is cool if it is taller than both building i-1 and building i+1 (and both of them exist). Note that neither the 1-st nor the n-th building can be cool.To remodel the city, Qpwoeirut needs to maximize the number of cool buildings. To do this, Qpwoeirut can build additional floors on top of any of the buildings to make them taller. Note that he cannot remove already existing floors.Since building new floors is expensive, Qpwoeirut wants to minimize the number of floors he builds. Find the minimum number of floors Qpwoeirut needs to build in order to maximize the number of cool buildings.
Input: ['632 1 251 2 1 4 363 1 4 5 5 284 2 1 3 5 3 6 161 10 1 1 10 181 10 11 1 10 11 10 1'] Output:['2', '0', '3', '3', '0', '4', '']
[ 2 ]
You have a sequence of n colored blocks. The color of the i-th block is c_i, an integer between 1 and n.You will place the blocks down in sequence on an infinite coordinate grid in the following way. Initially, you place block 1 at (0, 0). For 2 <= i <= n, if the (i - 1)-th block is placed at position (x, y), then the i-th block can be placed at one of positions (x + 1, y), (x - 1, y), (x, y + 1) (but not at position (x, y - 1)), as long no previous block was placed at that position. A tower is formed by s blocks such that they are placed at positions (x, y), (x, y + 1), ..., (x, y + s - 1) for some position (x, y) and integer s. The size of the tower is s, the number of blocks in it. A tower of color r is a tower such that all blocks in it have the color r.For each color r from 1 to n, solve the following problem independently: Find the maximum size of a tower of color r that you can form by placing down the blocks according to the rules.
Input: ['671 2 3 1 2 3 164 2 2 2 4 41155 4 5 3 563 3 3 1 3 381 2 3 4 4 3 2 1'] Output:['3 2 2 0 0 0 0 ', '0 3 0 2 0 0 ', '1 ', '0 0 1 1 1 ', '1 0 4 0 0 0 ', '2 2 2 2 0 0 0 0 ', '']
[ 2, 3 ]
You have a sequence a_1, a_2, ..., a_n of length n, consisting of integers between 1 and m. You also have a string s, consisting of m characters B.You are going to perform the following n operations. At the i-th (1 <= i <= n) operation, you replace either the a_i-th or the (m + 1 - a_i)-th character of s with A. You can replace the character at any position multiple times through the operations. Find the lexicographically smallest string you can get after these operations.A string x is lexicographically smaller than a string y of the same length if and only if in the first position where x and y differ, the string x has a letter that appears earlier in the alphabet than the corresponding letter in y.
Input: ['64 51 1 3 11 524 11 1 1 12 41 32 77 54 55 5 3 5'] Output:['ABABA', 'BABBB', 'A', 'AABB', 'ABABBBB', 'ABABA', '']
[ 2 ]
After watching a certain anime before going to sleep, Mark dreams of standing in an old classroom with a blackboard that has a sequence of n positive integers a_1, a_2,...,a_n on it.Then, professor Koro comes in. He can perform the following operation: select an integer x that appears at least 2 times on the board, erase those 2 appearances, and write x+1 on the board. Professor Koro then asks Mark the question, "what is the maximum possible number that could appear on the board after some operations?"Mark quickly solves this question, but he is still slower than professor Koro. Thus, professor Koro decides to give Mark additional challenges. He will update the initial sequence of integers q times. Each time, he will choose positive integers k and l, then change a_k to l. After each update, he will ask Mark the same question again.Help Mark answer these questions faster than Professor Koro!Note that the updates are persistent. Changes made to the sequence a will apply when processing future updates.
Input: ['5 4', '2 2 2 4 5', '2 3', '5 3', '4 1', '1 4', ''] Output:['6', '5', '4', '5', '']
[ 0, 2, 4 ]
Mark has just purchased a rack of n lightbulbs. The state of the lightbulbs can be described with binary string s = s_1s_2... s_n, where s_i=\texttt{1} means that the i-th lightbulb is turned on, while s_i=\texttt{0} means that the i-th lightbulb is turned off.Unfortunately, the lightbulbs are broken, and the only operation he can perform to change the state of the lightbulbs is the following: Select an index i from 2,3,...,n-1 such that s_{i-1}\ne s_{i+1}. Toggle s_i. Namely, if s_i is \texttt{0}, set s_i to \texttt{1} or vice versa. Mark wants the state of the lightbulbs to be another binary string t. Help Mark determine the minimum number of operations to do so.
Input: ['4401000010410100100501001000116000101010011'] Output:['2', '-1', '-1', '5', '']
[ 2, 3 ]
One night, Mark realized that there is an essay due tomorrow. He hasn't written anything yet, so Mark decided to randomly copy-paste substrings from the prompt to make the essay.More formally, the prompt is a string s of initial length n. Mark will perform the copy-pasting operation c times. Each operation is described by two integers l and r, which means that Mark will append letters s_l s_{l+1} ... s_r to the end of string s. Note that the length of s increases after this operation.Of course, Mark needs to be able to see what has been written. After copying, Mark will ask q queries: given an integer k, determine the k-th letter of the final string s.
Input: ['24 3 3mark1 45 73 8110127 3 3creamii2 33 42 991112'] Output:['m', 'a', 'r', 'e', 'a', 'r', '']
[ 0 ]
Mark is cleaning a row of n rooms. The i-th room has a nonnegative dust level a_i. He has a magical cleaning machine that can do the following three-step operation. Select two indices i<j such that the dust levels a_i, a_{i+1}, ..., a_{j-1} are all strictly greater than 0. Set a_i to a_i-1. Set a_j to a_j+1. Mark's goal is to make a_1 = a_2 = ... = a_{n-1} = 0 so that he can nicely sweep the n-th room. Determine the minimum number of operations needed to reach his goal.
Input: ['432 0 050 2 0 2 062 0 3 0 4 640 0 0 10'] Output:['3', '5', '11', '0', '']
[ 2 ]
Mark is asked to take a group photo of 2n people. The i-th person has height h_i units.To do so, he ordered these people into two rows, the front row and the back row, each consisting of n people. However, to ensure that everyone is seen properly, the j-th person of the back row must be at least x units taller than the j-th person of the front row for each j between 1 and n, inclusive.Help Mark determine if this is possible.
Input: ['33 61 3 9 10 12 163 12 5 2 2 2 51 28 6'] Output:['YES', 'NO', 'YES', '']
[ 2 ]
This is the hard version of this problem. The difference between easy and hard versions is the constraint on k and the time limit. Notice that you need to calculate the answer for all positive integers n \in [1,k] in this version. You can make hacks only if both versions of the problem are solved.Cirno is playing a war simulator game with n towers (numbered from 1 to n) and n bots (numbered from 1 to n). The i-th tower is initially occupied by the i-th bot for 1 <= i <= n.Before the game, Cirno first chooses a permutation p = [p_1, p_2, ..., p_n] of length n (A permutation of length n is an array of length n where each integer between 1 and n appears exactly once). After that, she can choose a sequence a = [a_1, a_2, ..., a_n] (1 <= a_i <= n and a_i!=i for all 1 <= i <= n).The game has n rounds of attacks. In the i-th round, if the p_i-th bot is still in the game, it will begin its attack, and as the result the a_{p_i}-th tower becomes occupied by the p_i-th bot; the bot that previously occupied the a_{p_i}-th tower will no longer occupy it. If the p_i-th bot is not in the game, nothing will happen in this round.After each round, if a bot doesn't occupy any towers, it will be eliminated and leave the game. Please note that no tower can be occupied by more than one bot, but one bot can occupy more than one tower during the game.At the end of the game, Cirno will record the result as a sequence b = [b_1, b_2, ..., b_n], where b_i is the number of the bot that occupies the i-th tower at the end of the game.However, as a mathematics master, she wants you to solve the following counting problem instead of playing games:Count the number of different pairs of sequences a, b from all possible choices of sequence a and permutation p.Calculate the answers for all n such that 1 <= n <= k. Since these numbers may be large, output them modulo M.
Input: ['8 998244353', ''] Output:['0', '2', '24', '360', '6800', '153150', '4057452', '123391016', '']
[ 3 ]
This is the easy version of this problem. The difference between easy and hard versions is the constraint on k and the time limit. Also, in this version of the problem, you only need to calculate the answer when n=k. You can make hacks only if both versions of the problem are solved.Cirno is playing a war simulator game with n towers (numbered from 1 to n) and n bots (numbered from 1 to n). The i-th tower is initially occupied by the i-th bot for 1 <= i <= n.Before the game, Cirno first chooses a permutation p = [p_1, p_2, ..., p_n] of length n (A permutation of length n is an array of length n where each integer between 1 and n appears exactly once). After that, she can choose a sequence a = [a_1, a_2, ..., a_n] (1 <= a_i <= n and a_i!=i for all 1 <= i <= n).The game has n rounds of attacks. In the i-th round, if the p_i-th bot is still in the game, it will begin its attack, and as the result the a_{p_i}-th tower becomes occupied by the p_i-th bot; the bot that previously occupied the a_{p_i}-th tower will no longer occupy it. If the p_i-th bot is not in the game, nothing will happen in this round.After each round, if a bot doesn't occupy any towers, it will be eliminated and leave the game. Please note that no tower can be occupied by more than one bot, but one bot can occupy more than one tower during the game.At the end of the game, Cirno will record the result as a sequence b = [b_1, b_2, ..., b_n], where b_i is the number of the bot that occupies the i-th tower at the end of the game.However, as a mathematics master, she wants you to solve the following counting problem instead of playing games:Count the number of different pairs of sequences a and b that we can get from all possible choices of sequence a and permutation p.Since this number may be large, output it modulo M.
Input: ['1 998244353', ''] Output:['0', '']
[ 3 ]
Mio has an array a consisting of n integers, and an array b consisting of m integers. Mio can do the following operation to a: Choose an integer i (1 <=q i <=q n) that has not been chosen before, then add 1 to a_i, subtract 2 from a_{i+1}, add 3 to a_{i+2} an so on. Formally, the operation is to add (-1)^{j-i} \cdot (j-i+1) to a_j for i <=q j <=q n.Mio wants to transform a so that it will contain b as a subarray. Could you answer her question, and provide a sequence of operations to do so, if it is possible?An array b is a subarray of an array a if b can be obtained from a by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input: ['5', '5', '1 2 3 4 5', '5', '2 0 6 0 10', '5', '1 2 3 4 5', '3', '3 5 3', '8', '-3 2 -3 -4 4 0 1 -2', '4', '10 -6 7 -7', '5', '1 2 3 4 5', '4', '1 10 1 1', '5', '0 0 0 0 0', '2', '10 12', ''] Output:['1', '1 ', '1', '4 ', '5', '1 3 4 6 8 ', '-1', '-1', '']
[ 3 ]
Cirno has a DAG (Directed Acyclic Graph) with n nodes and m edges. The graph has exactly one node that has no out edges. The i-th node has an integer a_i on it.Every second the following happens: Let S be the set of nodes x that have a_x > 0. For all x \in S, 1 is subtracted from a_x, and then for each node y, such that there is an edge from x to y, 1 is added to a_y.Find the first moment of time when all a_i become 0. Since the answer can be very large, output it modulo 998\,244\,353.
Input: ['53 21 1 11 22 35 51 0 0 0 01 22 33 44 51 510 11998244353 0 0 0 998244353 0 0 0 0 01 22 33 44 55 66 77 88 99 101 37 95 61293 1145 9961 9961 19191 22 33 45 41 42 46 910 10 10 10 10 101 21 32 34 36 33 56 56 16 2'] Output:['3', '5', '4', '28010', '110', '']
[ 0, 3 ]
Eric has an array b of length m, then he generates n additional arrays c_1, c_2, ..., c_n, each of length m, from the array b, by the following way:Initially, c_i = b for every 1 <= i <= n. Eric secretly chooses an integer k (1 <= k <= n) and chooses c_k to be the special array.There are two operations that Eric can perform on an array c_t: Operation 1: Choose two integers i and j (2 <=q i < j <=q m-1), subtract 1 from both c_t[i] and c_t[j], and add 1 to both c_t[i-1] and c_t[j+1]. That operation can only be used on a non-special array, that is when t \neq k.; Operation 2: Choose two integers i and j (2 <=q i < j <=q m-2), subtract 1 from both c_t[i] and c_t[j], and add 1 to both c_t[i-1] and c_t[j+2]. That operation can only be used on a special array, that is when t = k.Note that Eric can't perform an operation if any element of the array will become less than 0 after that operation.Now, Eric does the following: For every non-special array c_i (i \neq k), Eric uses only operation 1 on it at least once. For the special array c_k, Eric uses only operation 2 on it at least once.Lastly, Eric discards the array b.For given arrays c_1, c_2, ..., c_n, your task is to find out the special array, i.e. the value k. Also, you need to find the number of times of operation 2 was used on it.
Input: ['7', '3 9', '0 1 2 0 0 2 1 1 0', '0 1 1 1 2 0 0 2 0', '0 1 2 0 0 1 2 1 0', '3 7', '25 15 20 15 25 20 20', '26 14 20 14 26 20 20', '25 15 20 15 20 20 25', '3 9', '25 15 20 15 25 20 20 20 20', '26 14 20 14 26 20 20 20 20', '25 15 20 15 25 15 20 20 25', '3 11', '25 15 20 15 25 20 20 20 20 20 20', '26 14 20 14 26 20 20 20 20 20 20', '25 15 20 15 25 20 15 20 20 20 25', '3 13', '25 15 20 15 25 20 20 20 20 20 20 20 20', '26 14 20 14 26 20 20 20 20 20 20 20 20', '25 15 20 15 25 20 20 15 20 20 20 20 25', '3 15', '25 15 20 15 25 20 20 20 20 20 20 20 20 20 20', '26 14 20 14 26 20 20 20 20 20 20 20 20 20 20', '25 15 20 15 25 20 20 20 15 20 20 20 20 20 25', '3 9', '909459 479492 676924 224197 162866 164495 193268 742456 728277', '948845 455424 731850 327890 304150 237351 251763 225845 798316', '975446 401170 792914 272263 300770 242037 236619 334316 725899', ''] Output:['3 1', '3 10', '3 15', '3 20', '3 25', '3 30', '1 1378716', '']
[ 3 ]
There are n houses numbered from 1 to n on a circle. For each 1 <=q i <=q n - 1, house i and house i + 1 are neighbours; additionally, house n and house 1 are also neighbours.Initially, m of these n houses are infected by a deadly virus. Each morning, Cirno can choose a house which is uninfected and protect the house from being infected permanently.Every day, the following things happen in order: Cirno chooses an uninfected house, and protect it permanently. All uninfected, unprotected houses which have at least one infected neighbor become infected. Cirno wants to stop the virus from spreading. Find the minimum number of houses that will be infected in the end, if she optimally choose the houses to protect.Note that every day Cirno always chooses a house to protect before the virus spreads. Also, a protected house will not be infected forever.
Input: ['810 33 6 86 22 520 33 7 1241 51 11 21 31 4110 52 4 6 8 105 53 2 5 4 11000000000 111000000000 41 1000000000 10 16'] Output:['7', '5', '11', '28', '9', '5', '2', '15', '']
[ 2 ]
Luke likes to eat. There are n piles of food aligned in a straight line in front of him. The i-th pile contains a_i units of food. Luke will walk from the 1-st pile towards the n-th pile, and he wants to eat every pile of food without walking back. When Luke reaches the i-th pile, he can eat that pile if and only if |v - a_i| <=q x, where x is a fixed integer, and v is Luke's food affinity.Before Luke starts to walk, he can set v to any integer. Also, for each i (1 <=q i <=q n), Luke can change his food affinity to any integer before he eats the i-th pile.Find the minimum number of changes needed to eat every pile of food.Note that the initial choice for v is not considered as a change.
Input: ['75 33 8 5 6 75 33 10 9 8 712 825 3 3 17 8 6 1 16 15 25 17 2310 21 2 3 4 5 6 7 8 9 108 22 4 6 8 6 4 12 148 22 7 8 9 6 13 21 2815 511 4 13 23 7 10 5 21 20 11 17 5 29 16 11'] Output:['0', '1', '2', '1', '2', '4', '6', '']
[ 0, 2 ]
AquaMoon has two binary sequences a and b, which contain only 0 and 1. AquaMoon can perform the following two operations any number of times (a_1 is the first element of a, a_2 is the second element of a, and so on): Operation 1: if a contains at least two elements, change a_2 to \operatorname{min}(a_1,a_2), and remove the first element of a. Operation 2: if a contains at least two elements, change a_2 to \operatorname{max}(a_1,a_2), and remove the first element of a.Note that after a removal of the first element of a, the former a_2 becomes the first element of a, the former a_3 becomes the second element of a and so on, and the length of a reduces by one.Determine if AquaMoon can make a equal to b by using these operations.
Input: ['106 2001001116 2110111016 2000001116 2111111018 510000101110107 4101000110018 6010100100100108 40101010110018 41010101001107 5101110011100'] Output:['YES', 'YES', 'NO', 'NO', 'NO', 'YES', 'YES', 'NO', 'NO', 'YES', '']
[ 2 ]
There are n chests. The i-th chest contains a_i coins. You need to open all n chests in order from chest 1 to chest n.There are two types of keys you can use to open a chest: a good key, which costs k coins to use; a bad key, which does not cost any coins, but will halve all the coins in each unopened chest, including the chest it is about to open. The halving operation will round down to the nearest integer for each chest halved. In other words using a bad key to open chest i will do a_i = \lfloor{\frac{a_i}{2}\rfloor}, a_{i+1} = \lfloor\frac{a_{i+1}}{2}\rfloor, ..., a_n = \lfloor \frac{a_n}{2}\rfloor; any key (both good and bad) breaks after a usage, that is, it is a one-time use. You need to use in total n keys, one for each chest. Initially, you have no coins and no keys. If you want to use a good key, then you need to buy it.During the process, you are allowed to go into debt; for example, if you have 1 coin, you are allowed to buy a good key worth k=3 coins, and your balance will become -2 coins.Find the maximum number of coins you can have after opening all n chests in order from chest 1 to chest n.
Input: ['54 510 10 3 11 213 1210 10 2912 515 74 89 45 18 69 67 67 11 96 23 592 5785 60'] Output:['11', '0', '13', '60', '58', '']
[ 0, 2, 3 ]
You are given an array a_1, a_2, ... a_n. Count the number of pairs of indices 1 <=q i, j <=q n such that a_i < i < a_j < j.
Input: ['581 1 2 3 8 2 1 421 2100 2 1 6 3 4 1 2 8 321 100000000030 1000000000 2'] Output:['3', '0', '10', '0', '1', '']
[ 2, 4 ]
You are given n strings s_1, s_2, ..., s_n of length at most \mathbf{8}. For each string s_i, determine if there exist two strings s_j and s_k such that s_i = s_j + s_k. That is, s_i is the concatenation of s_j and s_k. Note that j can be equal to k.Recall that the concatenation of strings s and t is s + t = s_1 s_2 ... s_p t_1 t_2 ... t_q, where p and q are the lengths of strings s and t respectively. For example, concatenation of "code" and "forces" is "codeforces".
Input: ['35ababababcabacbc3xxxxxx8codeforcescodescodforcforcesecode'] Output:['10100', '011', '10100101', '']
[ 0 ]
Luca has a cypher made up of a sequence of n wheels, each with a digit a_i written on it. On the i-th wheel, he made b_i moves. Each move is one of two types: up move (denoted by \texttt{U}): it increases the i-th digit by 1. After applying the up move on 9, it becomes 0. down move (denoted by \texttt{D}): it decreases the i-th digit by 1. After applying the down move on 0, it becomes 9. Example for n=4. The current sequence is 0 0 0 0. Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher.
Input: ['339 3 13 DDD4 UDUU2 DU20 99 DDDDDDDDD9 UUUUUUUUU50 5 9 8 310 UUUUUUUUUU3 UUD8 UUDUUDDD10 UUDUUDUDDU4 UUUU'] Output:['2 1 1 ', '9 0 ', '0 4 9 6 9 ', '']
[ 0 ]
There is a string s of length 3, consisting of uppercase and lowercase English letters. Check if it is equal to "YES" (without quotes), where each letter can be in any case. For example, "yES", "Yes", "yes" are all allowable.
Input: ['10YESyESyesYesYeSNooorZyEzYasXES'] Output:['YES', 'YES', 'YES', 'YES', 'YES', 'NO', 'NO', 'NO', 'NO', 'NO', '']
[ 0 ]
Multiset β€”is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. Two multisets are equal when each value occurs the same number of times. For example, the multisets \{2,2,4\} and \{2,4,2\} are equal, but the multisets \{1,2,2\} and \{1,1,2\} β€” are not.You are given two multisets a and b, each consisting of n integers.In a single operation, any element of the b multiset can be doubled or halved (rounded down). In other words, you have one of the following operations available for an element x of the b multiset: replace x with x \cdot 2, or replace x with \lfloor \frac{x}{2} \rfloor (round down). Note that you cannot change the elements of the a multiset.See if you can make the multiset b become equal to the multiset a in an arbitrary number of operations (maybe 0).For example, if n = 4, a = \{4, 24, 5, 2\}, b = \{4, 1, 6, 11\}, then the answer is yes. We can proceed as follows: Replace 1 with 1 \cdot 2 = 2. We get b = \{4, 2, 6, 11\}. Replace 11 with \lfloor \frac{11}{2} \rfloor = 5. We get b = \{4, 2, 6, 5\}. Replace 6 with 6 \cdot 2 = 12. We get b = \{4, 2, 12, 5\}. Replace 12 with 12 \cdot 2 = 24. We get b = \{4, 2, 24, 5\}. Got equal multisets a = \{4, 24, 5, 2\} and b = \{4, 2, 24, 5\}.
Input: ['542 4 5 241 4 6 1131 4 174 5 3154 7 10 13 142 14 14 26 4252 2 4 4 428 46 62 71 9861 2 10 16 64 8020 43 60 74 85 99'] Output:['YES', 'NO', 'YES', 'YES', 'YES', '']
[ 2, 3 ]
Let s be a string of lowercase Latin letters. Its price is the sum of the indices of letters (an integer between 1 and 26) that are included in it. For example, the price of the string abca is 1+2+3+1=7.The string w and the integer p are given. Remove the minimal number of letters from w so that its price becomes less than or equal to p and print the resulting string. Note that the resulting string may be empty. You can delete arbitrary letters, they do not have to go in a row. If the price of a given string w is less than or equal to p, then nothing needs to be deleted and w must be output.Note that when you delete a letter from w, the order of the remaining letters is preserved. For example, if you delete the letter e from the string test, you get tst.
Input: ['5abca2abca6codeforces1codeforces10codeforces100'] Output:['aa', 'abc', '', 'cdc', 'codeforces']
[ 2 ]
Along the railroad there are stations indexed from 1 to 10^9. An express train always travels along a route consisting of n stations with indices u_1, u_2, ..., u_n, where (1 <= u_i <= 10^9). The train travels along the route from left to right. It starts at station u_1, then stops at station u_2, then at u_3, and so on. Station u_n β€” the terminus.It is possible that the train will visit the same station more than once. That is, there may be duplicates among the values u_1, u_2, ..., u_n.You are given k queries, each containing two different integers a_j and b_j (1 <= a_j, b_j <= 10^9). For each query, determine whether it is possible to travel by train from the station with index a_j to the station with index b_j.For example, let the train route consist of 6 of stations with indices [3, 7, 1, 5, 1, 4] and give 3 of the following queries: a_1 = 3, b_1 = 5It is possible to travel from station 3 to station 5 by taking a section of the route consisting of stations [3, 7, 1, 5]. Answer: YES. a_2 = 1, b_2 = 7You cannot travel from station 1 to station 7 because the train cannot travel in the opposite direction. Answer: NO. a_3 = 3, b_3 = 10It is not possible to travel from station 3 to station 10 because station 10 is not part of the train's route. Answer: NO.
Input: ['36 33 7 1 5 1 43 51 73 103 31 2 12 11 24 57 52 1 1 1 2 4 41 31 42 14 11 2'] Output:['YES', 'NO', 'NO', 'YES', 'YES', 'NO', 'NO', 'YES', 'YES', 'NO', 'YES', '']
[ 2 ]
Polycarp has a poor memory. Each day he can remember no more than 3 of different letters. Polycarp wants to write a non-empty string of s consisting of lowercase Latin letters, taking minimum number of days. In how many days will he be able to do it?Polycarp initially has an empty string and can only add characters to the end of that string.For example, if Polycarp wants to write the string lollipops, he will do it in 2 days: on the first day Polycarp will memorize the letters l, o, i and write lolli; On the second day Polycarp will remember the letters p, o, s, add pops to the resulting line and get the line lollipops. If Polycarp wants to write the string stringology, he will do it in 4 days: in the first day will be written part str; on day two will be written part ing; on the third day, part of olog will be written; on the fourth day, part of y will be written. For a given string s, print the minimum number of days it will take Polycarp to write it.
Input: ['6lollipopsstringologyabracadabracodeforcestestf'] Output:['2', '4', '3', '4', '1', '1', '']
[ 2 ]
A triple of points i, j and k on a coordinate line is called beautiful if i < j < k and k - i <= d.You are given a set of points on a coordinate line, initially empty. You have to process queries of three types: add a point; remove a point; calculate the number of beautiful triples consisting of points belonging to the set.
Input: ['7 5', '8 5 3 2 1 5 6', ''] Output:['0', '0', '1', '2', '5', '1', '5', '']
[ 3 ]
You wanted to write a text t consisting of m lowercase Latin letters. But instead, you have written a text s consisting of n lowercase Latin letters, and now you want to fix it by obtaining the text t from the text s.Initially, the cursor of your text editor is at the end of the text s (after its last character). In one move, you can do one of the following actions: press the "left" button, so the cursor is moved to the left by one position (or does nothing if it is pointing at the beginning of the text, i. e. before its first character); press the "right" button, so the cursor is moved to the right by one position (or does nothing if it is pointing at the end of the text, i. e. after its last character); press the "home" button, so the cursor is moved to the beginning of the text (before the first character of the text); press the "end" button, so the cursor is moved to the end of the text (after the last character of the text); press the "backspace" button, so the character before the cursor is removed from the text (if there is no such character, nothing happens). Your task is to calculate the minimum number of moves required to obtain the text t from the text s using the given set of actions, or determine it is impossible to obtain the text t from the text s.You have to answer T independent test cases.
Input: ['69 4aaaaaaaaaaaaa7 3abacabaaaa5 4aabcdabcd4 2abbabb6 4barakabaka8 7questionproblem'] Output:['5', '6', '3', '4', '4', '-1', '']
[ 0, 2 ]
Monocarp had a permutation a of n integers 1, 2, ..., n (a permutation is an array where each element from 1 to n occurs exactly once).Then Monocarp calculated an array of integers b of size n, where b_i = <=ft\lfloor \frac{i}{a_i} \right\rfloor. For example, if the permutation a is [2, 1, 4, 3], then the array b is equal to <=ft[ <=ft\lfloor \frac{1}{2} \right\rfloor, <=ft\lfloor \frac{2}{1} \right\rfloor, <=ft\lfloor \frac{3}{4} \right\rfloor, <=ft\lfloor \frac{4}{3} \right\rfloor \right] = [0, 2, 0, 1].Unfortunately, the Monocarp has lost his permutation, so he wants to restore it. Your task is to find a permutation a that corresponds to the given array b. If there are multiple possible permutations, then print any of them. The tests are constructed in such a way that least one suitable permutation exists.
Input: ['440 2 0 121 150 0 1 4 130 1 3'] Output:['2 1 4 3 ', '1 2 ', '3 4 2 1 5 ', '3 2 1 ', '']
[ 2, 3, 4 ]
There are n workers and m tasks. The workers are numbered from 1 to n. Each task i has a value a_i β€” the index of worker who is proficient in this task.Every task should have a worker assigned to it. If a worker is proficient in the task, they complete it in 1 hour. Otherwise, it takes them 2 hours.The workers work in parallel, independently of each other. Each worker can only work on one task at once.Assign the workers to all tasks in such a way that the tasks are completed as early as possible. The work starts at time 0. What's the minimum time all tasks can be completed by?
Input: ['42 41 2 1 22 41 1 1 15 55 1 3 2 41 11'] Output:['2', '3', '1', '1', '']
[ 2, 4 ]
Recall that a permutation of length n is an array where each element from 1 to n occurs exactly once.For a fixed positive integer d, let's define the cost of the permutation p of length n as the number of indices i (1 <= i < n) such that p_i \cdot d = p_{i + 1}.For example, if d = 3 and p = [5, 2, 6, 7, 1, 3, 4], then the cost of such a permutation is 2, because p_2 \cdot 3 = p_3 and p_5 \cdot 3 = p_6.Your task is the following one: for a given value n, find the permutation of length n and the value d with maximum possible cost (over all ways to choose the permutation and d). If there are multiple answers, then print any of them.
Input: ['223'] Output:['2', '1 2', '3', '2 1 3', '']
[ 2 ]
Pupils Alice and Ibragim are best friends. It's Ibragim's birthday soon, so Alice decided to gift him a new puzzle. The puzzle can be represented as a matrix with 2 rows and n columns, every element of which is either 0 or 1. In one move you can swap two values in neighboring cells.More formally, let's number rows 1 to 2 from top to bottom, and columns 1 to n from left to right. Also, let's denote a cell in row x and column y as (x, y). We consider cells (x_1, y_1) and (x_2, y_2) neighboring if |x_1 - x_2| + |y_1 - y_2| = 1.Alice doesn't like the way in which the cells are currently arranged, so she came up with her own arrangement, with which she wants to gift the puzzle to Ibragim. Since you are her smartest friend, she asked you to help her find the minimal possible number of operations in which she can get the desired arrangement. Find this number, or determine that it's not possible to get the new arrangement.
Input: ['5', '0 1 0 1 0', '1 1 0 0 1', '1 0 1 0 1', '0 0 1 1 0', ''] Output:['5', '']
[ 2 ]
Little pirate Serega robbed a ship with puzzles of different kinds. Among all kinds, he liked only one, the hardest.A puzzle is a table of n rows and m columns, whose cells contain each number from 1 to n \cdot m exactly once.To solve a puzzle, you have to find a sequence of cells in the table, such that any two consecutive cells are adjacent by the side in the table. The sequence can have arbitrary length and should visit each cell one or more times. For a cell containing the number i, denote the position of the first occurrence of this cell in the sequence as t_i. The sequence solves the puzzle, if t_1 < t_2 < ... < t_{nm}. In other words, the cell with number x should be first visited before the cell with number x + 1 for each x.Let's call a puzzle solvable, if there exists at least one suitable sequence.In one move Serega can choose two arbitrary cells in the table (not necessarily adjacent by the side) and swap their numbers. He would like to know the minimum number of moves to make his puzzle solvable, but he is too impatient. Thus, please tell if the minimum number of moves is 0, 1, or at least 2. In the case, where 1 move is required, please also find the number of suitable cell pairs to swap.
Input: ['3 3', '2 1 3', '6 7 4', '9 8 5', ''] Output:['0', '']
[ 0 ]
Recently in Divanovo, a huge river locks system was built. There are now n locks, the i-th of them has the volume of v_i liters, so that it can contain any amount of water between 0 and v_i liters. Each lock has a pipe attached to it. When the pipe is open, 1 liter of water enters the lock every second.The locks system is built in a way to immediately transfer all water exceeding the volume of the lock i to the lock i + 1. If the lock i + 1 is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates 5 locks with two open pipes at locks 1 and 3. Because locks 1, 3, and 4 are already filled, effectively the water goes to locks 2 and 5. Note that the volume of the i-th lock may be greater than the volume of the i + 1-th lock.To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in q independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the j-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after t_j seconds.Please help the mayor to solve this tricky problem and answer his queries.
Input: ['5', '4 1 5 4 1', '6', '1', '6', '2', '3', '4', '5', ''] Output:['-1', '3', '-1', '-1', '4', '3', '']
[ 2, 3, 4 ]
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees.There are n trees growing near the path, the current levels of moisture of each tree are denoted by the array a_1, a_2, ..., a_n. Leon has learned three abilities which will help him to dry and water the soil. Choose a position i and decrease the level of moisture of the trees 1, 2, ..., i by 1. Choose a position i and decrease the level of moisture of the trees i, i + 1, ..., n by 1. Increase the level of moisture of all trees by 1. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to 0.
Input: ['4', '3', '-2 -2 -2', '3', '10 4 7', '4', '4 -4 4 -4', '5', '1 -2 3 -4 5', ''] Output:['2', '13', '36', '33', '']
[ 2 ]
During a daily walk Alina noticed a long number written on the ground. Now Alina wants to find some positive number of same length without leading zeroes, such that the sum of these two numbers is a palindrome. Recall that a number is called a palindrome, if it reads the same right to left and left to right. For example, numbers 121, 66, 98989 are palindromes, and 103, 239, 1241 are not palindromes.Alina understands that a valid number always exist. Help her find one!
Input: ['3', '2', '99', '4', '1023', '3', '385', ''] Output:['32', '8646', '604']
[ 3 ]
You are given a table a of size n * m. We will consider the table rows numbered from top to bottom from 1 to n, and the columns numbered from left to right from 1 to m. We will denote a cell that is in the i-th row and in the j-th column as (i, j). In the cell (i, j) there is written a number (i - 1) \cdot m + j, that is a_{ij} = (i - 1) \cdot m + j.A turtle initially stands in the cell (1, 1) and it wants to come to the cell (n, m). From the cell (i, j) it can in one step go to one of the cells (i + 1, j) or (i, j + 1), if it exists. A path is a sequence of cells in which for every two adjacent in the sequence cells the following satisfies: the turtle can reach from the first cell to the second cell in one step. A cost of a path is the sum of numbers that are written in the cells of the path. For example, with n = 2 and m = 3 the table will look as shown above. The turtle can take the following path: (1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3). The cost of such way is equal to a_{11} + a_{12} + a_{13} + a_{23} = 12. On the other hand, the paths (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 1) and (1, 1) \rightarrow (1, 3) are incorrect, because in the first path the turtle can't make a step (2, 2) \rightarrow (2, 1), and in the second path it can't make a step (1, 1) \rightarrow (1, 3).You are asked to tell the turtle a minimal possible cost of a path from the cell (1, 1) to the cell (n, m). Please note that the cells (1, 1) and (n, m) are a part of the way.
Input: ['71 12 33 27 11 105 510000 10000'] Output:['1', '12', '13', '28', '55', '85', '500099995000', '']
[ 2, 3 ]
Ibti was thinking about a good title for this problem that would fit the round theme (numerus ternarium). He immediately thought about the third derivative, but that was pretty lame so he decided to include the best band in the world β€” Three Days Grace.You are given a multiset A with initial size n, whose elements are integers between 1 and m. In one operation, do the following: select a value x from the multiset A, then select two integers p and q such that p, q > 1 and p \cdot q = x. Insert p and q to A, delete x from A. Note that the size of the multiset A increases by 1 after each operation. We define the balance of the multiset A as \max(a_i) - \min(a_i). Find the minimum possible balance after performing any number (possible zero) of operations.
Input: ['45 102 4 2 4 23 5012 2 32 406 352 51 5'] Output:['0', '1', '2', '4', '']
[ 2, 3 ]
You are given an integer n and an array a_1,a_2,...,a_n.In one operation, you can choose an index i (1 <= i \lt n) for which a_i \neq a_{i+1} and delete both a_i and a_{i+1} from the array. After deleting a_i and a_{i+1}, the remaining parts of the array are concatenated.For example, if a=[1,4,3,3,6,2], then after performing an operation with i=2, the resulting array will be [1,3,6,2].What is the maximum possible length of an array of equal elements obtainable from a by performing several (perhaps none) of the aforementioned operations?
Input: ['571 2 3 2 1 3 31161 1 1 2 2 281 1 2 2 3 3 1 1121 5 2 3 3 3 4 4 4 4 3 3'] Output:['3', '1', '0', '4', '2', '']
[ 2 ]
You are given a permutation a_1,a_2,...,a_n of integers from 0 to n - 1. Your task is to find how many permutations b_1,b_2,...,b_n are similar to permutation a. Two permutations a and b of size n are considered similar if for all intervals [l,r] (1 <= l <= r <= n), the following condition is satisfied: \operatorname{MEX}([a_l,a_{l+1},...,a_r])=\operatorname{MEX}([b_l,b_{l+1},...,b_r]), where the \operatorname{MEX} of a collection of integers c_1,c_2,...,c_k is defined as the smallest non-negative integer x which does not occur in collection c. For example, \operatorname{MEX}([1,2,3,4,5])=0, and \operatorname{MEX}([0,1,2,4,5])=3.Since the total number of such permutations can be very large, you will have to print its remainder modulo 10^9+7.In this problem, a permutation of size n is an array consisting of n distinct integers from 0 to n-1 in arbitrary order. For example, [1,0,2,4,3] is a permutation, while [0,1,1] is not, since 1 appears twice in the array. [0,1,3] is also not a permutation, since n=3 and there is a 3 in the array.
Input: ['554 0 3 2 11040 1 2 361 2 4 0 5 381 3 7 2 5 0 6 4'] Output:['2', '1', '1', '4', '72', '']
[ 3 ]
You are given a positive integer n. Your task is to find any three integers a, b and c (0 <= a, b, c <= 10^9) for which (a\oplus b)+(b\oplus c)+(a\oplus c)=n, or determine that there are no such integers.Here a \oplus b denotes the bitwise XOR of a and b. For example, 2 \oplus 4 = 6 and 3 \oplus 1=2.
Input: ['541122046194723326'] Output:['3 3 1', '-1', '2 4 6', '69 420 666', '12345678 87654321 100000000', '']
[ 3 ]
There is a binary string t of length 10^{100}, and initally all of its bits are \texttt{0}. You are given a binary string s, and perform the following operation some times: Select some substring of t, and replace it with its XOR with s.^ After several operations, the string t has exactly two bits \texttt{1}; that is, there are exactly two distinct indices p and q such that the p-th and q-th bits of t are \texttt{1}, and the rest of the bits are \texttt{0}. Find the lexicographically largest^\ddagger string t satisfying these constraints, or report that no such string exists.^ Formally, choose an index i such that 0 <=q i <=q 10^{100}-|s|. For all 1 <=q j <=q |s|, if s_j = \texttt{1}, then toggle t_{i+j}. That is, if t_{i+j}=\texttt{0}, set t_{i+j}=\texttt{1}. Otherwise if t_{i+j}=\texttt{1}, set t_{i+j}=\texttt{0}.^\ddagger A binary string a is lexicographically larger than a binary string b of the same length if in the first position where a and b differ, the string a has a bit \texttt{1} and the corresponding bit in b is \texttt{0}.
Input: ['1', ''] Output:['1 2', '']
[ 3 ]
There is an array a of length n. You may perform the following operation on it: Choose two indices l and r where 1 <= l <= r <= n and a_l = a_r. Then, reverse the subsegment from the l-th to the r-th element, i. e. set [a_l, a_{l + 1}, ..., a_{r - 1}, a_r] to [a_r, a_{r-1}, ..., a_{l+1}, a_l]. You are also given another array b of length n which is a permutation of a. Find a sequence of at most n^2 operations that transforms array a into b, or report that no such sequence exists.
Input: ['581 2 4 3 1 2 1 11 1 3 4 2 1 2 171 2 3 1 3 2 31 3 2 3 1 2 331 1 21 2 121 22 1111'] Output:['YES', '2', '5 8', '1 6', 'YES', '2', '1 4', '3 6', 'NO', 'NO', 'YES', '0', '']
[ 3 ]
You are given a permutation a of length n. Recall that permutation is an array consisting of n distinct integers from 1 to n in arbitrary order.You have a strength of s and perform n moves on the permutation a. The i-th move consists of the following: Pick two integers x and y such that i <=q x <=q y <=q \min(i+s,n), and swap the positions of the integers x and y in the permutation a. Note that you can select x=y in the operation, in which case no swap will occur. You want to turn a into another permutation b after n moves. However, some elements of b are missing and are replaced with -1 instead. Count the number of ways to replace each -1 in b with some integer from 1 to n so that b is a permutation and it is possible to turn a into b with a strength of s. Since the answer can be large, output it modulo 998\,244\,353.
Input: ['63 12 1 33 -1 -13 22 1 33 -1 -14 11 4 3 24 3 1 26 44 2 6 3 1 56 1 5 -1 3 -17 41 3 6 2 7 4 52 5 -1 -1 -1 4 -114 141 2 3 4 5 6 7 8 9 10 11 12 13 14-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1'] Output:['1', '2', '0', '2', '12', '331032489', '']
[ 0, 2 ]
This is an interactive problem.Initially, there is an array a = [1, 2, ..., n], where n is an odd positive integer. The jury has selected \frac{n-1}{2} disjoint pairs of elements, and then the elements in those pairs are swapped. For example, if a=[1,2,3,4,5], and the pairs 1 <=ftrightarrow 4 and 3 <=ftrightarrow 5 are swapped, then the resulting array is [4, 2, 5, 1, 3]. As a result of these swaps, exactly one element will not change position. You need to find this element.To do this, you can ask several queries. In each query, you can pick two integers l and r (1 <=q l <=q r <=q n). In return, you will be given the elements of the subarray [a_l, a_{l + 1}, ..., a_r] sorted in increasing order. Find the element which did not change position. You can make at most \mathbf{15} queries.The array a is fixed before the interaction and does not change after your queries.Recall that an array b is a subarray of the array a if b can be obtained from a by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input: ['2', '5', '', '1 2 4 5', '', '1 3 5', '', '3', '', '1'] Output:['', '', '? 1 4', '', '? 3 5', '', '! 2', '', '? 1 1', '', '! 1', '']
[ 4 ]
You are given an array a of length n. The array is called 3SUM-closed if for all distinct indices i, j, k, the sum a_i + a_j + a_k is an element of the array. More formally, a is 3SUM-closed if for all integers 1 <=q i < j < k <=q n, there exists some integer 1 <=q l <=q n such that a_i + a_j + a_k = a_l.Determine if a is 3SUM-closed.
Input: ['43-1 0 151 -2 -2 1 -360 0 0 0 0 04-1 2 -3 4'] Output:['YES', 'NO', 'YES', 'NO', '']
[ 0 ]
There are n piles of sand where the i-th pile has a_i blocks of sand. The i-th pile is called too tall if 1 < i < n and a_i > a_{i-1} + a_{i+1}. That is, a pile is too tall if it has more sand than its two neighbours combined. (Note that piles on the ends of the array cannot be too tall.)You are given an integer k. An operation consists of picking k consecutive piles of sand and adding one unit of sand to them all. Formally, pick 1 <=q l,r <=q n such that r-l+1=k. Then for all l <=q i <=q r, update a_i >=ts a_i+1.What is the maximum number of piles that can simultaneously be too tall after some (possibly zero) operations?
Input: ['35 22 9 2 4 14 41 3 2 13 11 3 1'] Output:['2', '0', '1', '']
[ 2 ]
There is an array a with n-1 integers. Let x be the bitwise XOR of all elements of the array. The number x is added to the end of the array a (now it has length n), and then the elements are shuffled.You are given the newly formed array a. What is x? If there are multiple possible values of x, you can output any of them.
Input: ['444 3 2 556 1 10 7 1066 6 6 6 6 63100 100 0'] Output:['3', '7', '6', '0', '']
[ 0 ]
You are given n points on the plane, the coordinates of the i-th point are (x_i, y_i). No two points have the same coordinates.The distance between points i and j is defined as d(i,j) = |x_i - x_j| + |y_i - y_j|.For each point, you have to choose a color, represented by an integer from 1 to n. For every ordered triple of different points (a,b,c), the following constraints should be met: if a, b and c have the same color, then d(a,b) = d(a,c) = d(b,c); if a and b have the same color, and the color of c is different from the color of a, then d(a,b) < d(a,c) and d(a,b) < d(b,c). Calculate the number of different ways to choose the colors that meet these constraints.
Input: ['3', '1 0', '3 0', '2 1', ''] Output:['9', '']
[ 0, 2 ]
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.The jury has chosen a string s consisting of n characters; each character of s is a lowercase Latin letter. Your task is to guess this string; initially, you know only its length.You may ask queries of two types: 1 i β€” the query of the first type, where i is an integer from 1 to n. In response to this query, the jury will tell you the character s_i; 2 l r β€” the query of the second type, where l and r are integers such that 1 <= l <= r <= n. In response to this query, the jury will tell you the number of different characters among s_l, s_{l+1}, ..., s_r. You are allowed to ask no more than 26 queries of the first type, and no more than 6000 queries of the second type. Your task is to restore the string s.For each test in this problem, the string s is fixed beforehand, and will be the same for every submission.
Input: ['5', '4', 'u', '2', 'g', 'e', 's', '1', ''] Output:['? 2 1 5', '? 1 2', '? 2 1 2', '? 1 1', '? 1 3', '? 1 4', '? 2 4 5', '! guess', '']
[ 4 ]
You are given two strings s and t, both of length n. Each character in both string is 'a', 'b' or 'c'.In one move, you can perform one of the following actions: choose an occurrence of "ab" in s and replace it with "ba"; choose an occurrence of "bc" in s and replace it with "cb". You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string s to make it equal to string t?
Input: ['53cabcab1ab6abbabcbbaacb10bcaabababccbbababaac2baab'] Output:['YES', 'NO', 'YES', 'YES', 'NO', '']
[ 2, 4 ]
The store sells n items, the price of the i-th item is p_i. The store's management is going to hold a promotion: if a customer purchases at least x items, y cheapest of them are free.The management has not yet decided on the exact values of x and y. Therefore, they ask you to process q queries: for the given values of x and y, determine the maximum total value of items received for free, if a customer makes one purchase.Note that all queries are independent; they don't affect the store's stock.
Input: ['5 3', '5 3 1 5 2', '3 2', '1 1', '5 3', ''] Output:['8', '5', '6', '']
[ 2 ]
You are walking through a parkway near your house. The parkway has n+1 benches in a row numbered from 1 to n+1 from left to right. The distance between the bench i and i+1 is a_i meters.Initially, you have m units of energy. To walk 1 meter of distance, you spend 1 unit of your energy. You can't walk if you have no energy. Also, you can restore your energy by sitting on benches (and this is the only way to restore the energy). When you are sitting, you can restore any integer amount of energy you want (if you sit longer, you restore more energy). Note that the amount of your energy can exceed m.Your task is to find the minimum amount of energy you have to restore (by sitting on benches) to reach the bench n+1 from the bench 1 (and end your walk).You have to answer t independent test cases.
Input: ['33 11 2 14 53 3 5 25 161 2 3 4 5'] Output:['3', '8', '0', '']
[ 2 ]
You are given a positive integer k. For a multiset of integers S, define f(S) as the following. If the number of elements in S is less than k, f(S)=0. Otherwise, define f(S) as the maximum product you can get by choosing exactly k integers from S. More formally, let |S| denote the number of elements in S. Then, If |S|<k, f(S)=0. Otherwise, f(S)=\max\limits_{T\subseteq S,|T|=k}<=ft(\prod\limits_{i\in T}i\right). You are given a multiset of integers, A. Compute \sum\limits_{B\subseteq A} f(B) modulo 10^9+7.Note that in this problem, we distinguish the elements by indices instead of values. That is, a multiset consisting of n elements always has 2^n distinct subsets regardless of whether some of its elements are equal.
Input: ['3 2', '-1 2 4', ''] Output:['10']
[ 0, 2, 3 ]
Suppose you are given a 1-indexed sequence a of non-negative integers, whose length is n, and two integers x, y. In consecutive t seconds (t can be any positive real number), you can do one of the following operations: Select 1<= i<n, decrease a_i by x\cdot t, and decrease a_{i+1} by y\cdot t. Select 1<= i<n, decrease a_i by y\cdot t, and decrease a_{i+1} by x\cdot t. Define the minimum amount of time (it might be a real number) required to make all elements in the sequence less than or equal to 0 as f(a).For example, when x=1, y=2, it takes 3 seconds to deal with the array [3,1,1,3]. We can: In the first 1.5 seconds do the second operation with i=1. In the next 1.5 seconds do the first operation with i=3. We can prove that it's not possible to make all elements less than or equal to 0 in less than 3 seconds, so f([3,1,1,3])=3.Now you are given a 1-indexed sequence b of positive integers, whose length is n. You are also given positive integers x, y. Process q queries of the following two types: 1 k v: change b_k to v. 2 l r: print f([b_l,b_{l+1},...,b_r]).
Input: ['4 3', '1 2', '3 1 1 4', '2 1 4', '1 1 1', '2 1 3', ''] Output:['3.500000000000000', '1.000000000000000', '']
[ 0, 3 ]
Fishingprince loves trees. A tree is a connected undirected graph without cycles.Fishingprince has a tree of n vertices. The vertices are numbered 1 through n. Let d(x,y) denote the shortest distance on the tree from vertex x to vertex y, assuming that the length of each edge is 1.However, the tree was lost in an accident. Fortunately, Fishingprince still remembers some information about the tree. More specifically, for every triple of integers x,y,z (1<= x<y<= n, 1<= z<= n) he remembers whether d(x,z)=d(y,z) or not.Help him recover the structure of the tree, or report that no tree satisfying the constraints exists.
Input: ['52002103001 0000003001 010000500000 01001 00000 0110000000 10000 0000000000 1101000000'] Output:['Yes', '1 2', 'No', 'Yes', '1 3', '2 3', 'No', 'Yes', '1 2', '1 4', '2 3', '2 5', '']
[ 0 ]
We say an infinite sequence a_{0}, a_{1}, a_2, ... is non-increasing if and only if for all i>= 0, a_i >= a_{i+1}.There is an infinite right and down grid. The upper-left cell has coordinates (0,0). Rows are numbered 0 to infinity from top to bottom, columns are numbered from 0 to infinity from left to right.There is also a non-increasing infinite sequence a_{0}, a_{1}, a_2, .... You are given a_0, a_1, ..., a_n; for all i>n, a_i=0. For every pair of x, y, the cell with coordinates (x,y) (which is located at the intersection of x-th row and y-th column) is white if y<a_x and black otherwise.Initially there is one doll named Jina on (0,0). You can do the following operation. Select one doll on (x,y). Remove it and place a doll on (x,y+1) and place a doll on (x+1,y). Note that multiple dolls can be present at a cell at the same time; in one operation, you remove only one. Your goal is to make all white cells contain 0 dolls.What's the minimum number of operations needed to achieve the goal? Print the answer modulo 10^9+7.
Input: ['2', '2 2 0', ''] Output:['5']
[ 3 ]
A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).You are given a permutation of 1,2,...,n, [a_1,a_2,...,a_n]. For integers i, j such that 1<= i<j<= n, define \operatorname{mn}(i,j) as \min\limits_{k=i}^j a_k, and define \operatorname{mx}(i,j) as \max\limits_{k=i}^j a_k.Let us build an undirected graph of n vertices, numbered 1 to n. For every pair of integers 1<= i<j<= n, if \operatorname{mn}(i,j)=a_i and \operatorname{mx}(i,j)=a_j both holds, or \operatorname{mn}(i,j)=a_j and \operatorname{mx}(i,j)=a_i both holds, add an undirected edge of length 1 between vertices i and j.In this graph, find the length of the shortest path from vertex 1 to vertex n. We can prove that 1 and n will always be connected via some path, so a shortest path always exists.
Input: ['51121 251 4 2 3 552 1 5 3 4107 4 8 1 6 10 3 5 2 9'] Output:['0', '1', '1', '4', '6', '']
[ 2, 4 ]
Fishingprince is playing with an array [a_1,a_2,...,a_n]. He also has a magic number m.He can do the following two operations on it: Select 1<= i<= n such that a_i is divisible by m (that is, there exists an integer t such that m \cdot t = a_i). Replace a_i with m copies of \frac{a_i}{m}. The order of the other elements doesn't change. For example, when m=2 and a=[2,3] and i=1, a changes into [1,1,3]. Select 1<= i<= n-m+1 such that a_i=a_{i+1}=...=a_{i+m-1}. Replace these m elements with a single m \cdot a_i. The order of the other elements doesn't change. For example, when m=2 and a=[3,2,2,3] and i=2, a changes into [3,4,3]. Note that the array length might change during the process. The value of n above is defined as the current length of the array (might differ from the n in the input).Fishingprince has another array [b_1,b_2,...,b_k]. Please determine if he can turn a into b using any number (possibly zero) of operations.
Input: ['55 21 2 2 4 241 4 4 26 21 2 2 8 2 221 168 33 3 3 3 3 3 3 346 6 6 68 33 9 6 3 12 12 36 12169 3 2 2 2 3 4 12 4 12 4 12 4 12 4 48 33 9 6 3 12 12 36 12712 2 4 3 4 12 56'] Output:['Yes', 'Yes', 'No', 'Yes', 'No', '']
[ 2, 3 ]
For a collection of integers S, define \operatorname{mex}(S) as the smallest non-negative integer that does not appear in S.NIT, the cleaver, decides to destroy the universe. He is not so powerful as Thanos, so he can only destroy the universe by snapping his fingers several times.The universe can be represented as a 1-indexed array a of length n. When NIT snaps his fingers, he does the following operation on the array: He selects positive integers l and r such that 1<= l<= r<= n. Let w=\operatorname{mex}(\{a_l,a_{l+1},...,a_r\}). Then, for all l<= i<= r, set a_i to w. We say the universe is destroyed if and only if for all 1<= i<= n, a_i=0 holds.Find the minimum number of times NIT needs to snap his fingers to destroy the universe. That is, find the minimum number of operations NIT needs to perform to make all elements in the array equal to 0.
Input: ['440 0 0 050 1 2 3 470 2 3 0 1 2 011000000000'] Output:['0', '1', '2', '1', '']
[ 2 ]
NIT, the cleaver, is new in town! Thousands of people line up to orz him. To keep his orzers entertained, NIT decided to let them solve the following problem related to \operatorname{or} z. Can you solve this problem too?You are given a 1-indexed array of n integers, a, and an integer z. You can do the following operation any number (possibly zero) of times: Select a positive integer i such that 1<= i<= n. Then, simutaneously set a_i to (a_i\operatorname{or} z) and set z to (a_i\operatorname{and} z). In other words, let x and y respectively be the current values of a_i and z. Then set a_i to (x\operatorname{or}y) and set z to (x\operatorname{and}y). Here \operatorname{or} and \operatorname{and} denote the bitwise operations OR and AND respectively.Find the maximum possible value of the maximum value in a after any number (possibly zero) of operations.
Input: ['52 33 45 50 2 4 6 81 9105 77 15 30 29 273 3954874310293834 10284344 13635445'] Output:['7', '13', '11', '31', '48234367', '']
[ 2 ]
The only difference between this problem and D1 is the bound on the size of the tree.You are given an unrooted tree with n vertices. There is some hidden vertex x in that tree that you are trying to find.To do this, you may ask k queries v_1, v_2, ..., v_k where the v_i are vertices in the tree. After you are finished asking all of the queries, you are given k numbers d_1, d_2, ..., d_k, where d_i is the number of edges on the shortest path between v_i and x. Note that you know which distance corresponds to which query.What is the minimum k such that there exists some queries v_1, v_2, ..., v_k that let you always uniquely identify x (no matter what x is).Note that you don't actually need to output these queries.
Input: ['3121 2102 42 15 73 108 66 11 34 79 6'] Output:['0', '1', '2', '']
[ 2 ]
The only difference between this problem and D2 is the bound on the size of the tree.You are given an unrooted tree with n vertices. There is some hidden vertex x in that tree that you are trying to find.To do this, you may ask k queries v_1, v_2, ..., v_k where the v_i are vertices in the tree. After you are finished asking all of the queries, you are given k numbers d_1, d_2, ..., d_k, where d_i is the number of edges on the shortest path between v_i and x. Note that you know which distance corresponds to which query.What is the minimum k such that there exists some queries v_1, v_2, ..., v_k that let you always uniquely identify x (no matter what x is).Note that you don't actually need to output these queries.
Input: ['3121 2102 42 15 73 108 66 11 34 79 6'] Output:['0', '1', '2', '']
[ 0, 2 ]
You are given a grid with n rows and m columns. We denote the square on the i-th (1<= i<= n) row and j-th (1<= j<= m) column by (i, j) and the number there by a_{ij}. All numbers are equal to 1 or to -1. You start from the square (1, 1) and can move one square down or one square to the right at a time. In the end, you want to end up at the square (n, m).Is it possible to move in such a way so that the sum of the values written in all the visited cells (including a_{11} and a_{nm}) is 0?
Input: ['51 111 21 -11 41 -1 1 -13 41 -1 -1 -1-1 1 1 -11 1 1 -13 41 -1 1 1-1 1 -1 11 -1 1 1'] Output:['NO', 'YES', 'YES', 'YES', 'NO', '']
[ 0, 2 ]
Mike and Joe are playing a game with some stones. Specifically, they have n piles of stones of sizes a_1, a_2, ..., a_n. These piles are arranged in a circle.The game goes as follows. Players take turns removing some positive number of stones from a pile in clockwise order starting from pile 1. Formally, if a player removed stones from pile i on a turn, the other player removes stones from pile ((i\bmod n) + 1) on the next turn.If a player cannot remove any stones on their turn (because the pile is empty), they lose. Mike goes first.If Mike and Joe play optimally, who will win?
Input: ['21372100 100'] Output:['Mike', 'Joe', '']
[ 2 ]
Let's call a binary string T of length m indexed from 1 to m paranoid if we can obtain a string of length 1 by performing the following two kinds of operations m-1 times in any order : Select any substring of T that is equal to 01, and then replace it with 1. Select any substring of T that is equal to 10, and then replace it with 0.For example, if T = 001, we can select the substring [T_2T_3] and perform the first operation. So we obtain T = 01.You are given a binary string S of length n indexed from 1 to n. Find the number of pairs of integers (l, r) 1 <= l <= r <= n such that S[l ... r] (the substring of S from l to r) is a paranoid string.
Input: ['511201310041001511111'] Output:['1', '3', '4', '8', '5', '']
[ 2 ]
Define the score of some binary string T as the absolute difference between the number of zeroes and ones in it. (for example, T= 010001 contains 4 zeroes and 2 ones, so the score of T is |4-2| = 2).Define the creepiness of some binary string S as the maximum score among all of its prefixes (for example, the creepiness of S= 01001 is equal to 2 because the score of the prefix S[1 ... 4] is 2 and the rest of the prefixes have a score of 2 or less).Given two integers a and b, construct a binary string consisting of a zeroes and b ones with the minimum possible creepiness.
Input: ['5', '1 1', '1 2', '5 2', '4 5', '3 7', ''] Output:['10', '011', '0011000', '101010101', '0001111111', '']
[ 2 ]
You are given a binary string S of length n indexed from 1 to n. You can perform the following operation any number of times (possibly zero):Choose two integers l and r (1 <= l <= r <= n). Let cnt_0 be the number of times 0 occurs in S[l ... r] and cnt_1 be the number of times 1 occurs in S[l ... r]. You can pay |cnt_0 - cnt_1| + 1 coins and sort the S[l ... r]. (by S[l ... r] we mean the substring of S starting at position l and ending at position r)For example if S = 11001, we can perform the operation on S[2 ... 4], paying |2 - 1| + 1 = 2 coins, and obtain S = 10011 as a new string.Find the minimum total number of coins required to sort S in increasing order.
Input: ['71121031014100051101061100002001000010001010011000'] Output:['0', '1', '1', '3', '2', '2', '5', '']
[ 2, 4 ]
Yeri has an array of n + 2 non-negative integers : a_0, a_1, ..., a_n, a_{n + 1}.We know that a_0 = a_{n + 1} = 0.She wants to make all the elements of a equal to zero in the minimum number of operations.In one operation she can do one of the following: Choose the leftmost maximum element and change it to the maximum of the elements on its left. Choose the rightmost maximum element and change it to the maximum of the elements on its right.Help her find the minimum number of operations needed to make all elements of a equal to zero.
Input: ['6', '1 4 2 4 0 2', ''] Output:['7', '']
[ 2 ]
Let's call an array a of m integers a_1, a_2, ..., a_m Decinc if a can be made increasing by removing a decreasing subsequence (possibly empty) from it. For example, if a = [3, 2, 4, 1, 5], we can remove the decreasing subsequence [a_1, a_4] from a and obtain a = [2, 4, 5], which is increasing.You are given a permutation p of numbers from 1 to n. Find the number of pairs of integers (l, r) with 1 <= l <= r <= n such that p[l ... r] (the subarray of p from l to r) is a Decinc array.
Input: ['3', '2 3 1', ''] Output:['6', '']
[ 0, 2 ]
AmShZ has traveled to Italy from Iran for the Thom Yorke concert. There are n cities in Italy indexed from 1 to n and m directed roads indexed from 1 to m. Initially, Keshi is located in the city 1 and wants to go to AmShZ's house in the city n. Since Keshi doesn't know the map of Italy, AmShZ helps him to see each other as soon as possible.In the beginning of each day, AmShZ can send one of the following two messages to Keshi: AmShZ sends the index of one road to Keshi as a blocked road. Then Keshi will understand that he should never use that road and he will remain in his current city for the day. AmShZ tells Keshi to move. Then, Keshi will randomly choose one of the cities reachable from his current city and move there. (city B is reachable from city A if there's an out-going road from city A to city B which hasn't become blocked yet). If there are no such cities, Keshi will remain in his current city.Note that AmShZ always knows Keshi's current location. AmShZ and Keshi want to find the smallest possible integer d for which they can make sure that they will see each other after at most d days. Help them find d.
Input: ['2 1', '1 2', ''] Output:['1', '']
[ 2 ]
We are given a rooted tree consisting of n vertices numbered from 1 to n. The root of the tree is the vertex 1 and the parent of the vertex v is p_v.There is a number written on each vertex, initially all numbers are equal to 0. Let's denote the number written on the vertex v as a_v.For each v, we want a_v to be between l_v and r_v (l_v <=q a_v <=q r_v).In a single operation we do the following: Choose some vertex v. Let b_1, b_2, ..., b_k be vertices on the path from the vertex 1 to vertex v (meaning b_1 = 1, b_k = v and b_i = p_{b_{i + 1}}). Choose a non-decreasing array c of length k of nonnegative integers: 0 <=q c_1 <=q c_2 <=q ... <=q c_k. For each i (1 <=q i <=q k), increase a_{b_i} by c_i. What's the minimum number of operations needed to achieve our goal?
Input: ['4211 52 931 14 52 46 1041 2 16 95 64 52 451 2 3 45 54 43 32 21 1'] Output:['1', '2', '2', '5', '']
[ 2 ]
We have an array of length n. Initially, each element is equal to 0 and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by 1. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by 1. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array a. Determine whether it's possible to obtain a after some operations or not.
Input: ['7', '2', '1 0', '4', '2 -1 -1 0', '4', '1 -4 3 0', '4', '1 -1 1 -1', '5', '1 2 3 4 -10', '7', '2 -1 1 -2 0 0 0', '1', '0', ''] Output:['No', 'Yes', 'No', 'No', 'Yes', 'Yes', 'Yes', '']
[ 2 ]
Marian is at a casino. The game at the casino works like this.Before each round, the player selects a number between 1 and 10^9. After that, a dice with 10^9 faces is rolled so that a random number between 1 and 10^9 appears. If the player guesses the number correctly their total money is doubled, else their total money is halved. Marian predicted the future and knows all the numbers x_1, x_2, ..., x_n that the dice will show in the next n rounds. He will pick three integers a, l and r (l <=q r). He will play r-l+1 rounds (rounds between l and r inclusive). In each of these rounds, he will guess the same number a. At the start (before the round l) he has 1 dollar.Marian asks you to determine the integers a, l and r (1 <=q a <=q 10^9, 1 <=q l <=q r <=q n) such that he makes the most money at the end.Note that during halving and multiplying there is no rounding and there are no precision errors. So, for example during a game, Marian could have money equal to \dfrac{1}{1024}, \dfrac{1}{128}, \dfrac{1}{2}, 1, 2, 4, etc. (any value of 2^t, where t is an integer of any sign).
Input: ['454 4 3 4 4511 1 11 1 1111000000000108 8 8 9 9 6 6 9 6 6'] Output:['4 1 5', '1 2 2', '1000000000 1 1', '6 6 10', '']
[ 2, 3 ]
Given an array a of positive integers with length n, determine if there exist three distinct indices i, j, k such that a_i + a_j + a_k ends in the digit 3.
Input: ['6420 22 19 8441 11 1 202241100 1100 1100 1111512 34 56 78 9041 9 8 4616 38 94 25 18 99'] Output:['YES', 'YES', 'NO', 'NO', 'YES', 'YES', '']
[ 0, 3 ]