question_text
stringlengths
2
3.82k
input_outputs
stringlengths
23
941
algo_tags
sequence
You have r red and b blue beans. You'd like to distribute them among several (maybe, one) packets in such a way that each packet: has at least one red bean (or the number of red beans r_i >= 1); has at least one blue bean (or the number of blue beans b_i >= 1); the number of red and blue beans should differ in no more than d (or |r_i - b_i| <= d) Can you distribute all beans?
Input: ['4', '1 1 0', '2 7 3', '6 1 4', '5 4 0', ''] Output:['YES', 'YES', 'NO', 'NO', '']
[ 3 ]
In the 2050 Conference, some people from the competitive programming community meet together and are going to take a photo. The n people form a line. They are numbered from 1 to n from left to right. Each of them either holds a cardboard with the letter 'C' or a cardboard with the letter 'P'.Let C=\{c_1,c_2,...,c_m\} (c_1<c_2<... <c_m) be the set of people who hold cardboards of 'C'. Let P=\{p_1,p_2,...,p_k\} (p_1<p_2<... <p_k) be the set of people who hold cardboards of 'P'. The photo is good if and only if it satisfies the following constraints: C\cup P=\{1,2,...,n\} C\cap P =\emptyset . c_i-c_{i-1}<=q c_{i+1}-c_i(1< i <m). p_i-p_{i-1}>=q p_{i+1}-p_i(1< i <k). Given an array a_1,..., a_n, please find the number of good photos satisfying the following condition: \sum\limits_{x\in C} a_x < \sum\limits_{y\in P} a_y.The answer can be large, so output it modulo 998\,244\,353. Two photos are different if and only if there exists at least one person who holds a cardboard of 'C' in one photo but holds a cardboard of 'P' in the other.
Input: ['3', '5', '2 1 2 1 1', '4', '9 2 2 2', '1', '998244353', ''] Output:['10', '7', '1', '']
[ 4 ]
Fillomino is a classic logic puzzle. (You do not need to know Fillomino in order to solve this problem.) In one classroom in Yunqi town, some volunteers are playing a board game variant of it:Consider an n by n chessboard. Its rows are numbered from 1 to n from the top to the bottom. Its columns are numbered from 1 to n from the left to the right. A cell on an intersection of x-th row and y-th column is denoted (x, y). The main diagonal of the chessboard is cells (x, x) for all 1 <= x <= n.A permutation of \{1, 2, 3, ..., n\} is written on the main diagonal of the chessboard. There is exactly one number written on each of the cells. The problem is to partition the cells under and on the main diagonal (there are exactly 1+2+ ... +n such cells) into n connected regions satisfying the following constraints: Every region should be connected. That means that we can move from any cell of a region to any other cell of the same region visiting only cells of the same region and moving from a cell to an adjacent cell. The x-th region should contain cell on the main diagonal with number x for all 1<= x<= n. The number of cells that belong to the x-th region should be equal to x for all 1<= x<= n. Each cell under and on the main diagonal should belong to exactly one region.
Input: ['3', '2 3 1', ''] Output:['2', '2 3', '3 3 1', '']
[ 2 ]
The 2050 volunteers are organizing the "Run! Chase the Rising Sun" activity. Starting on Apr 25 at 7:30 am, runners will complete the 6km trail around the Yunqi town.There are n+1 checkpoints on the trail. They are numbered by 0, 1, ..., n. A runner must start at checkpoint 0 and finish at checkpoint n. No checkpoint is skippable β€” he must run from checkpoint 0 to checkpoint 1, then from checkpoint 1 to checkpoint 2 and so on. Look at the picture in notes section for clarification.Between any two adjacent checkpoints, there are m different paths to choose. For any 1<= i<= n, to run from checkpoint i-1 to checkpoint i, a runner can choose exactly one from the m possible paths. The length of the j-th path between checkpoint i-1 and i is b_{i,j} for any 1<= j<= m and 1<= i<= n.To test the trail, we have m runners. Each runner must run from the checkpoint 0 to the checkpoint n once, visiting all the checkpoints. Every path between every pair of adjacent checkpoints needs to be ran by exactly one runner. If a runner chooses the path of length l_i between checkpoint i-1 and i (1<= i<= n), his tiredness is \min_{i=1}^n l_i, i. e. the minimum length of the paths he takes.Please arrange the paths of the m runners to minimize the sum of tiredness of them.
Input: ['2', '2 3', '2 3 4', '1 3 5', '3 2', '2 3', '4 1', '3 5', ''] Output:['2 3 4', '5 3 1', '2 3', '4 1', '3 5', '']
[ 2 ]
A number is called 2050-number if it is 2050, 20500, ..., (2050 \cdot 10^k for integer k >= 0).Given a number n, you are asked to represent n as the sum of some (not necessarily distinct) 2050-numbers. Compute the minimum number of 2050-numbers required for that.
Input: ['6', '205', '2050', '4100', '20500', '22550', '25308639900', ''] Output:['-1', '1', '2', '1', '2', '36', '']
[ 2, 3 ]
This time around, Baby Ehab will play with permutations. He has n cubes arranged in a row, with numbers from 1 to n written on them. He'll make exactly j operations. In each operation, he'll pick up 2 cubes and switch their positions.He's wondering: how many different sequences of cubes can I have at the end? Since Baby Ehab is a turbulent person, he doesn't know how many operations he'll make, so he wants the answer for every possible j between 1 and k.
Input: ['2 3', ''] Output:['1 1 1 ', '']
[ 3 ]
This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array a of length n written on it, and then he does the following: he picks a range (l, r) and cuts the subsegment a_l, a_{l + 1}, ..., a_r out, removing the rest of the array. he then cuts this range into multiple subranges. to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their least common multiple (LCM). Formally, he partitions the elements of a_l, a_{l + 1}, ..., a_r into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for q independent ranges (l, r), tell Baby Ehab the minimum number of subarrays he needs.
Input: ['6 3', '2 3 10 7 5 14', '1 6', '2 4', '3 5', ''] Output:['3', '1', '2', '']
[ 4 ]
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input: ['4', '6 3 9 12', ''] Output:['1', '2']
[ 3 ]
Baby Ehab is known for his love for a certain operation. He has an array a of length n, and he decided to keep doing the following operation on it: he picks 2 adjacent elements; he then removes them and places a single integer in their place: their bitwise XOR. Note that the length of the array decreases by one. Now he asks you if he can make all elements of the array equal. Since babies like to make your life harder, he requires that you leave at least 2 elements remaining.
Input: ['2', '3', '0 2 2', '4', '2 3 1 10', ''] Output:['YES', 'NO', '']
[ 0, 2 ]
Given an array a of length n, you can do at most k operations of the following type on it: choose 2 different elements in the array, add 1 to the first, and subtract 1 from the second. However, all the elements of a have to remain non-negative after this operation. What is lexicographically the smallest array you can obtain?An array x is lexicographically smaller than an array y if there exists an index i such that x_i<y_i, and x_j=y_j for all 1 <= j < i. Less formally, at the first index i in which they differ, x_i<y_i.
Input: ['2', '3 1', '3 1 4', '2 10', '1 0', ''] Output:['2 1 5 ', '0 1 ', '']
[ 2 ]
Phoenix wonders what it is like to rob diamonds from a jewelry store!There are n types of diamonds. The i-th type has weight w_i and value v_i. The store initially has a_i diamonds of the i-th type.Each day, for q days, one of the following will happen: A new shipment of k_i diamonds of type d_i arrive. The store sells k_i diamonds of type d_i. Phoenix wonders what will happen if he robs the store using a bag that can fit diamonds with total weight not exceeding c_i. If he greedily takes diamonds of the largest value that fit, how much value would be taken? If there are multiple diamonds with the largest value, he will take the one with minimum weight. If, of the diamonds with the largest value, there are multiple with the same minimum weight, he will take any of them. Of course, since Phoenix is a law-abiding citizen, this is all a thought experiment and he never actually robs any diamonds from the store. This means that queries of type 3 do not affect the diamonds in the store.
Input: ['3 5', '2 3 4', '1 5 1', '0 2 4', '3 6', '1 3 3', '3 10', '2 2 3', '3 30', ''] Output:['8', '16', '13', '']
[ 4 ]
Phoenix loves playing with bits β€” specifically, by using the bitwise operations AND, OR, and XOR. He has n integers a_1, a_2, ..., a_n, and will perform q of the following queries: replace all numbers a_i where l <= a_i <= r with a_i AND x; replace all numbers a_i where l <= a_i <= r with a_i OR x; replace all numbers a_i where l <= a_i <= r with a_i XOR x; output how many distinct integers a_i where l <= a_i <= r. For each query, Phoenix is given l, r, and x. Note that he is considering the values of the numbers, not their indices.
Input: ['5 6', '5 4 3 2 1', '1 2 3 2', '4 2 5', '3 2 5 3', '4 1 6', '2 1 1 8', '4 8 10', ''] Output:['3', '2', '1', '']
[ 0 ]
In Fire City, there are n intersections and m one-way roads. The i-th road goes from intersection a_i to b_i and has length l_i miles. There are q cars that may only drive along those roads. The i-th car starts at intersection v_i and has an odometer that begins at s_i, increments for each mile driven, and resets to 0 whenever it reaches t_i. Phoenix has been tasked to drive cars along some roads (possibly none) and return them to their initial intersection with the odometer showing 0.For each car, please find if this is possible. A car may visit the same road or intersection an arbitrary number of times. The odometers don't stop counting the distance after resetting, so odometers may also be reset an arbitrary number of times.
Input: ['4 4', '1 2 1', '2 3 1', '3 1 2', '1 4 3', '3', '1 1 3', '1 2 4', '4 0 1', ''] Output:['YES', 'NO', 'YES', '']
[ 3 ]
Phoenix's homeland, the Fire Nation had n cities that were connected by m roads, but the roads were all destroyed by an earthquake. The Fire Nation wishes to repair n-1 of these roads so that all the cities are connected again. The i-th city has a_i tons of asphalt. x tons of asphalt are used up when repairing a road, and to repair a road between i and j, cities i and j must have at least x tons of asphalt between them. In other words, if city i had a_i tons of asphalt and city j had a_j tons, there would remain a_i+a_j-x tons after repairing the road between them. Asphalt can be moved between cities if the road between them is already repaired.Please determine if it is possible to connect all the cities, and if so, output any sequence of roads to repair.
Input: ['5 4 1', '0 0 0 4 0', '1 2', '2 3', '3 4', '4 5', ''] Output:['YES', '3', '2', '1', '4', '']
[ 2 ]
There are n computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer i-1 and computer i+1 are both on, computer i (2 <= i <= n-1) will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically.If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo M.
Input: ['3 100000007', ''] Output:['6', '']
[ 3 ]
To satisfy his love of matching socks, Phoenix has brought his n socks (n is even) to the sock store. Each of his socks has a color c_i and is either a left sock or right sock. Phoenix can pay one dollar to the sock store to either: recolor a sock to any color c' (1 <= c' <= n) turn a left sock into a right sock turn a right sock into a left sock The sock store may perform each of these changes any number of times. Note that the color of a left sock doesn't change when it turns into a right sock, and vice versa. A matching pair of socks is a left and right sock with the same color. What is the minimum cost for Phoenix to make n/2 matching pairs? Each sock must be included in exactly one matching pair.
Input: ['4', '6 3 3', '1 2 3 2 2 2', '6 2 4', '1 1 2 2 2 2', '6 5 1', '6 5 4 3 2 1', '4 0 4', '4 4 4 3', ''] Output:['2', '3', '5', '3', '']
[ 2 ]
Phoenix has n blocks of height h_1, h_2, ..., h_n, and all h_i don't exceed some value x. He plans to stack all n blocks into m separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than x. Please help Phoenix build m towers that look beautiful. Each tower must have at least one block and all blocks must be used.
Input: ['2', '5 2 3', '1 2 3 1 2', '4 3 3', '1 1 2 3', ''] Output:['YES', '1 1 1 2 2', 'YES', '1 2 2 3']
[ 2 ]
Phoenix is playing with a new puzzle, which consists of n identical puzzle pieces. Each puzzle piece is a right isosceles triangle as shown below. A puzzle piece The goal of the puzzle is to create a square using the n pieces. He is allowed to rotate and move the pieces around, but none of them can overlap and all n pieces must be used (of course, the square shouldn't contain any holes as well). Can he do it?
Input: ['3', '2', '4', '6', ''] Output:['YES', 'YES', 'NO', '']
[ 0, 3 ]
Phoenix has collected n pieces of gold, and he wants to weigh them together so he can feel rich. The i-th piece of gold has weight w_i. All weights are distinct. He will put his n pieces of gold on a weight scale, one piece at a time. The scale has an unusual defect: if the total weight on it is exactly x, it will explode. Can he put all n gold pieces onto the scale in some order, without the scale exploding during the process? If so, help him find some possible order. Formally, rearrange the array w so that for each i (1 <= i <= n), \sum\limits_{j = 1}^{i}w_j!=x.
Input: ['3', '3 2', '3 2 1', '5 3', '1 2 3 4 8', '1 5', '5', ''] Output:['YES', '3 2 1', 'YES', '8 1 2 3 4', 'NO', '']
[ 2, 3 ]
This is an interactive problem.Baby Ehab loves crawling around his apartment. It has n rooms numbered from 0 to n-1. For every pair of rooms, a and b, there's either a direct passage from room a to room b, or from room b to room a, but never both.Baby Ehab wants to go play with Baby Badawy. He wants to know if he could get to him. However, he doesn't know anything about his apartment except the number of rooms. He can ask the baby sitter two types of questions: is the passage between room a and room b directed from a to b or the other way around? does room x have a passage towards any of the rooms s_1, s_2, ..., s_k? He can ask at most 9n queries of the first type and at most 2n queries of the second type.After asking some questions, he wants to know for every pair of rooms a and b whether there's a path from a to b or not. A path from a to b is a sequence of passages that starts from room a and ends at room b.
Input: ['1', '4', '', '0', '', '0', '', '1', '', '1', '', '1'] Output:['2 3 3 0 1 2', '', '1 0 1', '', '1 0 2', '', '2 2 1 1', '', '3', '1111', '1111', '1111', '0001']
[ 4 ]
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it: pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range; stick some of the elements together in the same order they were in the array; end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece. More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than \lceil \frac{x}{2} \rceil times in it.He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input: ['6 2', '1 3 2 3 3 2', '1 6', '2 5', ''] Output:['1', '2', '']
[ 2, 4 ]
Now you get Baby Ehab's first words: "Given an integer n, find the longest subsequence of [1,2, ..., n-1] whose product is 1 modulo n." Please solve the problem.A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly all) elements. The product of an empty subsequence is equal to 1.
Input: ['5', ''] Output:['3', '1 2 3 ']
[ 2 ]
Baby Badawy's first words were "AND 0 SUM BIG", so he decided to solve the following problem. Given two integers n and k, count the number of arrays of length n such that: all its elements are integers between 0 and 2^k-1 (inclusive); the bitwise AND of all its elements is 0; the sum of its elements is as large as possible. Since the answer can be very large, print its remainder when divided by 10^9+7.
Input: ['2', '2 2', '100000 20', ''] Output:['4', '226732710', '']
[ 3 ]
Given an array a of length n, tell us whether it has a non-empty subsequence such that the product of its elements is not a perfect square.A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements.
Input: ['2', '3', '1 5 4', '2', '100 10000', ''] Output:['YES', 'NO', '']
[ 3 ]
You are given 2 arrays a and b, both of size n. You can swap two elements in b at most once (or leave it as it is), and you are required to minimize the value \sum_{i}|a_{i}-b_{i}|.Find the minimum possible value of this sum.
Input: ['5', '5 4 3 2 1', '1 2 3 4 5', ''] Output:['4']
[ 0 ]
An array is called beautiful if all the elements in the array are equal.You can transform an array using the following steps any number of times: Choose two indices i and j (1 <=q i,j <=q n), and an integer x (1 <=q x <=q a_i). Let i be the source index and j be the sink index. Decrease the i-th element by x, and increase the j-th element by x. The resulting values at i-th and j-th index are a_i-x and a_j+x respectively. The cost of this operation is x \cdot |j-i| . Now the i-th index can no longer be the sink and the j-th index can no longer be the source. The total cost of a transformation is the sum of all the costs in step 3.For example, array [0, 2, 3, 3] can be transformed into a beautiful array [2, 2, 2, 2] with total cost 1 \cdot |1-3| + 1 \cdot |1-4| = 5.An array is called balanced, if it can be transformed into a beautiful array, and the cost of such transformation is uniquely defined. In other words, the minimum cost of transformation into a beautiful array equals the maximum cost.You are given an array a_1, a_2, ..., a_n of length n, consisting of non-negative integers. Your task is to find the number of balanced arrays which are permutations of the given array. Two arrays are considered different, if elements at some position differ. Since the answer can be large, output it modulo 10^9 + 7.
Input: ['3', '1 2 3', ''] Output:['6']
[ 3 ]
You are given an array a of n (n >=q 2) positive integers and an integer p. Consider an undirected weighted graph of n vertices numbered from 1 to n for which the edges between the vertices i and j (i<j) are added in the following manner: If gcd(a_i, a_{i+1}, a_{i+2}, ..., a_{j}) = min(a_i, a_{i+1}, a_{i+2}, ..., a_j), then there is an edge of weight min(a_i, a_{i+1}, a_{i+2}, ..., a_j) between i and j. If i+1=j, then there is an edge of weight p between i and j. Here gcd(x, y, ...) denotes the greatest common divisor (GCD) of integers x, y, ....Note that there could be multiple edges between i and j if both of the above conditions are true, and if both the conditions fail for i and j, then there is no edge between these vertices.The goal is to find the weight of the minimum spanning tree of this graph.
Input: ['4', '2 5', '10 10', '2 5', '3 3', '4 5', '5 2 4 9', '8 8', '5 3 3 6 10 100 9 15', ''] Output:['5', '3', '12', '46', '']
[ 2 ]
A sequence of n non-negative integers (n >= 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \& denotes the bitwise AND operation.You are given an array a of size n (n >=q 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input: ['4', '3', '1 1 1', '5', '1 2 3 4 5', '5', '0 2 0 3 0', '4', '1 3 5 1', ''] Output:['6', '0', '36', '4', '']
[ 3 ]
Let us denote by d(n) the sum of all divisors of the number n, i.e. d(n) = \sum\limits_{k | n} k.For example, d(1) = 1, d(4) = 1+2+4=7, d(6) = 1+2+3+6=12.For a given number c, find the minimum n such that d(n) = c.
Input: ['12', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '39', '691', ''] Output:['1', '-1', '2', '3', '-1', '5', '4', '7', '-1', '-1', '18', '-1', '']
[ 0, 3 ]
Polycarp is wondering about buying a new computer, which costs c tugriks. To do this, he wants to get a job as a programmer in a big company.There are n positions in Polycarp's company, numbered starting from one. An employee in position i earns a[i] tugriks every day. The higher the position number, the more tugriks the employee receives. Initially, Polycarp gets a position with the number 1 and has 0 tugriks.Each day Polycarp can do one of two things: If Polycarp is in the position of x, then he can earn a[x] tugriks. If Polycarp is in the position of x (x < n) and has at least b[x] tugriks, then he can spend b[x] tugriks on an online course and move to the position x+1. For example, if n=4, c=15, a=[1, 3, 10, 11], b=[1, 2, 7], then Polycarp can act like this: On the first day, Polycarp is in the 1-st position and earns 1 tugrik. Now he has 1 tugrik; On the second day, Polycarp is in the 1-st position and move to the 2-nd position. Now he has 0 tugriks; On the third day, Polycarp is in the 2-nd position and earns 3 tugriks. Now he has 3 tugriks; On the fourth day, Polycarp is in the 2-nd position and is transferred to the 3-rd position. Now he has 1 tugriks; On the fifth day, Polycarp is in the 3-rd position and earns 10 tugriks. Now he has 11 tugriks; On the sixth day, Polycarp is in the 3-rd position and earns 10 tugriks. Now he has 21 tugriks; Six days later, Polycarp can buy himself a new computer. Find the minimum number of days after which Polycarp will be able to buy himself a new computer.
Input: ['3', '4 15', '1 3 10 11', '1 2 7', '4 100', '1 5 10 50', '3 14 12', '2 1000000000', '1 1', '1', ''] Output:['6', '13', '1000000000', '']
[ 0, 2 ]
A permutation is a sequence of n integers from 1 to n, in which all the numbers occur exactly once. For example, [1], [3, 5, 2, 1, 4], [1, 3, 2] are permutations, and [2, 3, 2], [4, 3, 1], [0] are not.Polycarp was given four integers n, l, r (1 <= l <= r <= n) and s (1 <= s <= \frac{n (n+1)}{2}) and asked to find a permutation p of numbers from 1 to n that satisfies the following condition: s = p_l + p_{l+1} + ... + p_r. For example, for n=5, l=3, r=5, and s=8, the following permutations are suitable (not all options are listed): p = [3, 4, 5, 2, 1]; p = [5, 2, 4, 3, 1]; p = [5, 2, 1, 3, 4]. But, for example, there is no permutation suitable for the condition above for n=4, l=1, r=1, and s=5.Help Polycarp, for the given n, l, r, and s, find a permutation of numbers from 1 to n that fits the condition above. If there are several suitable permutations, print any of them.
Input: ['5', '5 2 3 5', '5 3 4 1', '3 1 2 4', '2 2 2 2', '2 1 1 3', ''] Output:['1 2 3 4 5 ', '-1', '1 3 2 ', '1 2 ', '-1', '']
[ 0, 2, 3 ]
You are given a number n and an array b_1, b_2, ..., b_{n+2}, obtained according to the following algorithm: some array a_1, a_2, ..., a_n was guessed; array a was written to array b, i.e. b_i = a_i (1 <= i <= n); The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. b_{n+1} = a_1+a_2+...+a_n; The (n+2)-th element of the array b was written some number x (1 <= x <= 10^9), i.e. b_{n+2} = x; The array b was shuffled. For example, the array b=[2, 3, 7, 12 ,2] it could be obtained in the following ways: a=[2, 2, 3] and x=12; a=[3, 2, 7] and x=2. For the given array b, find any array a that could have been guessed initially.
Input: ['4', '3', '2 3 7 12 2', '4', '9 1 7 1 6 5', '5', '18 2 2 3 2 9 2', '3', '2 6 9 2 1', ''] Output:['2 3 7 ', '-1', '2 2 2 3 9 ', '1 2 6 ', '']
[ 2 ]
You are given an array a consisting of n (n >= 3) positive integers. It is known that in this array, all the numbers except one are the same (for example, in the array [4, 11, 4, 4] all numbers except one are equal to 4).Print the index of the element that does not equal others. The numbers in the array are numbered from one.
Input: ['4', '4', '11 13 11 11', '5', '1 4 4 4 4', '10', '3 3 3 3 10 3 3 3 3 3', '3', '20 20 10', ''] Output:['2', '1', '5', '3', '']
[ 0 ]
Alice and Bob have a rectangular board consisting of n rows and m columns. Each row contains exactly one chip.Alice and Bob play the following game. They choose two integers l and r such that 1 <= l <= r <= m and cut the board in such a way that only the part of it between column l and column r (inclusive) remains. So, all columns to the left of column l and all columns to the right of column r no longer belong to the board.After cutting the board, they move chips on the remaining part of the board (the part from column l to column r). They make alternating moves, and the player which cannot make a move loses the game. The first move is made by Alice, the second β€” by Bob, the third β€” by Alice, and so on. During their move, the player must choose one of the chips from the board and move it any positive number of cells to the left (so, if the chip was in column i, it can move to any column j < i, and the chips in the leftmost column cannot be chosen).Alice and Bob have q pairs of numbers L_i and R_i. For each such pair, they want to determine who will be the winner of the game if l = L_i and r = R_i. Note that these games should be considered independently (they don't affect the state of the board for the next games), and both Alice and Bob play optimally.
Input: ['8 10', '1 3 3 7 4 2 6 9', '7', '2 3', '1 3', '1 4', '1 10', '5 10', '8 10', '9 10', ''] Output:['BAAAAAB']
[ 0 ]
A chainword is a special type of crossword. As most of the crosswords do, it has cells that you put the letters in and some sort of hints to what these letters should be.The letter cells in a chainword are put in a single row. We will consider chainwords of length m in this task.A hint to a chainword is a sequence of segments such that the segments don't intersect with each other and cover all m letter cells. Each segment contains a description of the word in the corresponding cells.The twist is that there are actually two hints: one sequence is the row above the letter cells and the other sequence is the row below the letter cells. When the sequences are different, they provide a way to resolve the ambiguity in the answers.You are provided with a dictionary of n words, each word consists of lowercase Latin letters. All words are pairwise distinct.An instance of a chainword is the following triple: a string of m lowercase Latin letters; the first hint: a sequence of segments such that the letters that correspond to each segment spell a word from the dictionary; the second hint: another sequence of segments such that the letters that correspond to each segment spell a word from the dictionary. Note that the sequences of segments don't necessarily have to be distinct.Two instances of chainwords are considered different if they have different strings, different first hints or different second hints.Count the number of different instances of chainwords. Since the number might be pretty large, output it modulo 998\,244\,353.
Input: ['3 5', 'ababa', 'ab', 'a', ''] Output:['11', '']
[ 0 ]
You have a large rectangular board which is divided into n * m cells (the board has n rows and m columns). Each cell is either white or black.You paint each white cell either red or blue. Obviously, the number of different ways to paint them is 2^w, where w is the number of white cells.After painting the white cells of the board, you want to place the maximum number of dominoes on it, according to the following rules: each domino covers two adjacent cells; each cell is covered by at most one domino; if a domino is placed horizontally (it covers two adjacent cells in one of the rows), it should cover only red cells; if a domino is placed vertically (it covers two adjacent cells in one of the columns), it should cover only blue cells. Let the value of the board be the maximum number of dominoes you can place. Calculate the sum of values of the board over all 2^w possible ways to paint it. Since it can be huge, print it modulo 998\,244\,353.
Input: ['3 4', '**oo', 'oo*o', '**oo', ''] Output:['144', '']
[ 2, 3 ]
Let's define the cost of a string s as the number of index pairs i and j (1 <= i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}.You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost β€” find any of them.
Input: ['9 4', ''] Output:['aabacadbb', '']
[ 0, 2 ]
You have a card deck of n cards, numbered from top to bottom, i. e. the top card has index 1 and bottom card β€” index n. Each card has its color: the i-th card has color a_i.You should process q queries. The j-th query is described by integer t_j. For each query you should: find the highest card in the deck with color t_j, i. e. the card with minimum index; print the position of the card you found; take the card and place it on top of the deck.
Input: ['7 5', '2 1 1 4 3 3 1', '3 2 1 1 4', ''] Output:['5 2 3 1 5 ']
[ 0 ]
You are given three integers a, b and c.Find two positive integers x and y (x > 0, y > 0) such that: the decimal representation of x without leading zeroes consists of a digits; the decimal representation of y without leading zeroes consists of b digits; the decimal representation of gcd(x, y) without leading zeroes consists of c digits. gcd(x, y) denotes the greatest common divisor (GCD) of integers x and y.Output x and y. If there are multiple answers, output any of them.
Input: ['4', '2 3 1', '2 2 2', '6 6 2', '1 1 1', ''] Output:['11 492', '13 26', '140133 160776', '1 1', '']
[ 3 ]
You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press β€” upvote and downvote.However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes.n reviewers enter the site one by one. Each reviewer is one of the following types: type 1: a reviewer has watched the movie, and they like it β€” they press the upvote button; type 2: a reviewer has watched the movie, and they dislike it β€” they press the downvote button; type 3: a reviewer hasn't watched the movie β€” they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie. Each reviewer votes on the movie exactly once.Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one.What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to?
Input: ['4', '1', '2', '3', '1 2 3', '5', '1 1 1 1 1', '3', '3 3 2', ''] Output:['0', '2', '5', '2', '']
[ 2 ]
The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task.There is a permutation p_i of numbers from 1 to 2n. You can make two types of operations. Swap p_1 and p_2, p_3 and p_4, ..., p_{2n-1} and p_{2n}. Swap p_1 and p_{n+1}, p_2 and p_{n+2}, ..., p_{n} and p_{2n}. The task is to find the minimal number of operations required to sort the given permutation.The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task.
Input: ['3', '6 3 2 5 4 1', ''] Output:['3', '']
[ 0 ]
Joseph really likes the culture of Japan. Last year he learned Japanese traditional clothes and visual arts and now he is trying to find out the secret of the Japanese game called Nonogram.In the one-dimensional version of the game, there is a row of n empty cells, some of which are to be filled with a pen. There is a description of a solution called a profile β€” a sequence of positive integers denoting the lengths of consecutive sets of filled cells. For example, the profile of [4, 3, 1] means that there are sets of four, three, and one filled cell, in that order, with at least one empty cell between successive sets. A suitable solution for n = 12 and p = [4, 3, 1]. A wrong solution: the first four filled cells should be consecutive. A wrong solution: there should be at least one empty cell before the last filled cell. Joseph found out that for some numbers n and profiles p there are lots of ways to fill the cells to satisfy the profile. Now he is in the process of solving a nonogram consisting of n cells and a profile p. He has already created a mask of p β€” he has filled all the cells that must be filled in every solution of the nonogram. The mask for n = 12 and p = [4, 3, 1]: all the filled cells above are filled in every solution. After a break, he lost the source profile p. He only has n and the mask m. Help Joseph find any profile p' with the mask m or say that there is no such profile and Joseph has made a mistake.
Input: ['__#_____', ''] Output:['2', '3 2 ', '']
[ 3 ]
The popular improv website Interpretation Impetus hosts regular improv contests and maintains a rating of the best performers. However, since improv can often go horribly wrong, the website is notorious for declaring improv contests unrated. It now holds a wager before each improv contest where the participants try to predict whether it will be rated or unrated, and they are now more popular than the improv itself.Izzy and n other participants take part in each wager. First, they each make their prediction, expressed as 1 ("rated") or 0 ("unrated"). Izzy always goes last, so she knows the predictions of the other participants when making her own. Then, the actual competition takes place and it is declared either rated or unrated.You need to write a program that will interactively play as Izzy. There will be m wagers held in 2021, and Izzy's goal is to have at most 1.3\cdot b + 100 wrong predictions after all those wagers, where b is the smallest number of wrong predictions that any other wager participant will have after all those wagers. The number b is not known in advance. Izzy also knows nothing about the other participants β€” they might somehow always guess correctly, or their predictions might be correlated. Izzy's predictions, though, do not affect the predictions of the other participants and the decision on the contest being rated or not β€” in other words, in each test case, your program always receives the same inputs, no matter what it outputs.
Input: ['3 4', '000', '', '1', '100', '', '1', '001', '', '0', '111', '', '1', ''] Output:['', '', '0', '', '', '0', '', '', '1', '', '', '1', '', '']
[ 2, 3 ]
Diana loves playing with numbers. She's got n cards with positive integer numbers a_i written on them. She spends her free time multiplying the numbers on the cards. She picks a non-empty subset of the cards and multiplies all the numbers a_i written on them.Diana is happy when the product of the numbers ends with her favorite digit d. Now she is curious what cards she should pick so that the product of the numbers on them is the largest possible and the last decimal digit of the product is d. Please, help her.
Input: ['6 4', '4 11 8 2 1 13', ''] Output:['5', '1 2 4 11 13 ', '']
[ 3 ]
The student council is preparing for the relay race at the sports festival.The council consists of n members. They will run one after the other in the race, the speed of member i is s_i. The discrepancy d_i of the i-th stage is the difference between the maximum and the minimum running speed among the first i members who ran. Formally, if a_i denotes the speed of the i-th member who participated in the race, then d_i = \max(a_1, a_2, ..., a_i) - \min(a_1, a_2, ..., a_i).You want to minimize the sum of the discrepancies d_1 + d_2 + ... + d_n. To do this, you are allowed to change the order in which the members run. What is the minimum possible sum that can be achieved?
Input: ['3', '3 1 2', ''] Output:['3', '']
[ 2 ]
The student council has a shared document file. Every day, some members of the student council write the sequence TMT (short for Towa Maji Tenshi) in it.However, one day, the members somehow entered the sequence into the document at the same time, creating a jumbled mess. Therefore, it is Suguru Doujima's task to figure out whether the document has malfunctioned. Specifically, he is given a string of length n whose characters are all either T or M, and he wants to figure out if it is possible to partition it into some number of disjoint subsequences, all of which are equal to TMT. That is, each character of the string should belong to exactly one of the subsequences.A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input: ['5', '3', 'TMT', '3', 'MTT', '6', 'TMTMTT', '6', 'TMTTTT', '6', 'TTMMTT', ''] Output:['YES', 'NO', 'YES', 'NO', 'YES', '']
[ 2 ]
Touko's favorite sequence of numbers is a permutation a_1, a_2, ..., a_n of 1, 2, ..., n, and she wants some collection of permutations that are similar to her favorite permutation.She has a collection of q intervals of the form [l_i, r_i] with 1 <= l_i <= r_i <= n. To create permutations that are similar to her favorite permutation, she coined the following definition: A permutation b_1, b_2, ..., b_n allows an interval [l', r'] to holds its shape if for any pair of integers (x, y) such that l' <= x < y <= r', we have b_x < b_y if and only if a_x < a_y. A permutation b_1, b_2, ..., b_n is k-similar if b allows all intervals [l_i, r_i] for all 1 <= i <= k to hold their shapes. Yuu wants to figure out all k-similar permutations for Touko, but it turns out this is a very hard task; instead, Yuu will encode the set of all k-similar permutations with directed acylic graphs (DAG). Yuu also coined the following definitions for herself: A permutation b_1, b_2, ..., b_n satisfies a DAG G' if for all edge u \to v in G', we must have b_u < b_v. A k-encoding is a DAG G_k on the set of vertices 1, 2, ..., n such that a permutation b_1, b_2, ..., b_n satisfies G_k if and only if b is k-similar. Since Yuu is free today, she wants to figure out the minimum number of edges among all k-encodings for each k from 1 to q.
Input: ['4 3', '2 4 1 3', '1 3', '2 4', '1 4', ''] Output:['2', '4', '3', '']
[ 0 ]
Yuu Koito and Touko Nanami are newlyweds! On the wedding day, Yuu gifted Touko a directed tree with n nodes and rooted at 1, and a labeling a which is some DFS order of the tree. Every edge in this tree is directed away from the root.After calling dfs(1) the following algorithm returns a as a DFS order of a tree rooted at 1 :order := 0a := array of length n function dfs(u): order := order + 1 a[u] := order for all v such that there is a directed edge (u -> v): dfs(v)Note that there may be different DFS orders for a given tree.Touko likes the present so much she decided to play with it! On each day following the wedding day, Touko performs this procedure once: Among all directed edges u \rightarrow v such that a_u < a_v, select the edge u' \rightarrow v' with the lexicographically smallest pair (a_{u'}, a_{v'}). Swap a_{u'} and a_{v'}.Days have passed since their wedding, and Touko has somehow forgotten which date the wedding was and what was the original labeling a! Fearing that Yuu might get angry, Touko decided to ask you to derive these two pieces of information using the current labeling.Being her good friend, you need to find the number of days that have passed since the wedding, and the original labeling of the tree. However, there is a chance that Touko might have messed up her procedures, which result in the current labeling being impossible to obtain from some original labeling; in that case, please inform Touko as well.
Input: ['7', '4 5 2 1 7 6 3', '1 5', '7 6', '1 2', '2 7', '3 4', '1 3', ''] Output:['YES', '5', '1 4 2 3 7 6 5', '']
[ 0 ]
As a teacher, Riko Hakozaki often needs to help her students with problems from various subjects. Today, she is asked a programming task which goes as follows.You are given an undirected complete graph with n nodes, where some edges are pre-assigned with a positive weight while the rest aren't. You need to assign all unassigned edges with non-negative weights so that in the resulting fully-assigned complete graph the XOR sum of all weights would be equal to 0.Define the ugliness of a fully-assigned complete graph the weight of its minimum spanning tree, where the weight of a spanning tree equals the sum of weights of its edges. You need to assign the weights so that the ugliness of the resulting graph is as small as possible.As a reminder, an undirected complete graph with n nodes contains all edges (u, v) with 1 <= u < v <= n; such a graph has \frac{n(n-1)}{2} edges.She is not sure how to solve this problem, so she asks you to solve it for her.
Input: ['4 4', '2 1 14', '1 4 14', '3 2 15', '4 3 8', ''] Output:['15', '']
[ 0, 2 ]
Seiji Maki doesn't only like to observe relationships being unfolded, he also likes to observe sequences of numbers, especially permutations. Today, he has his eyes on almost sorted permutations.A permutation a_1, a_2, ..., a_n of 1, 2, ..., n is said to be almost sorted if the condition a_{i + 1} >= a_i - 1 holds for all i between 1 and n - 1 inclusive.Maki is considering the list of all almost sorted permutations of 1, 2, ..., n, given in lexicographical order, and he wants to find the k-th permutation in this list. Can you help him to find such permutation?Permutation p is lexicographically smaller than a permutation q if and only if the following holds: in the first position where p and q differ, the permutation p has a smaller element than the corresponding element in q.
Input: ['5', '1 1', '1 2', '3 3', '6 5', '3 4', ''] Output:['1 ', '-1', '2 1 3 ', '1 2 4 3 5 6 ', '3 2 1 ', '']
[ 4 ]
A bitstring is a string that contains only the characters 0 and 1.Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.
Input: ['2', '1', '00', '11', '01', '3', '011001', '111010', '010001', ''] Output:['010', '011001010', '']
[ 2 ]
You are given a string s, consisting of lowercase Latin letters. While there is at least one character in the string s that is repeated at least twice, you perform the following operation: you choose the index i (1 <= i <= |s|) such that the character at position i occurs at least two times in the string s, and delete the character at position i, that is, replace s with s_1 s_2 ... s_{i-1} s_{i+1} s_{i+2} ... s_n. For example, if s="codeforces", then you can apply the following sequence of operations: i=6 \Rightarrow s="codefrces"; i=1 \Rightarrow s="odefrces"; i=7 \Rightarrow s="odefrcs"; Given a given string s, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique.A string a of length n is lexicographically less than a string b of length m, if: there is an index i (1 <= i <= \min(n, m)) such that the first i-1 characters of the strings a and b are the same, and the i-th character of the string a is less than i-th character of string b; or the first \min(n, m) characters in the strings a and b are the same and n < m. For example, the string a="aezakmi" is lexicographically less than the string b="aezus".
Input: ['6', 'codeforces', 'aezakmi', 'abacaba', 'convexhull', 'swflldjgpaxs', 'myneeocktxpqjpz', ''] Output:['odfrces', 'ezakmi', 'cba', 'convexhul', 'wfldjgpaxs', 'myneocktxqjpz', '']
[ 0, 2 ]
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The k-th layer of the triangle contains k points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers (r, c) (1 <= c <= r), where r is the number of the layer, and c is the number of the point in the layer. From each point (r, c) there are two directed edges to the points (r+1, c) and (r+1, c+1), but only one of the edges is activated. If r + c is even, then the edge to the point (r+1, c) is activated, otherwise the edge to the point (r+1, c+1) is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point (r_1, c_1) it is possible to reach the point (r_2, c_2), if there is a path between them only from activated edges. For example, in the picture above, there is a path from (1, 1) to (3, 2), but there is no path from (2, 1) to (1, 1).Initially, you are at the point (1, 1). For each turn, you can: Replace activated edge for point (r, c). That is if the edge to the point (r+1, c) is activated, then instead of it, the edge to the point (r+1, c+1) becomes activated, otherwise if the edge to the point (r+1, c+1), then instead if it, the edge to the point (r+1, c) becomes activated. This action increases the cost of the path by 1; Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of n points of an infinite triangle (r_1, c_1), (r_2, c_2), ..., (r_n, c_n). Find the minimum cost path from (1, 1), passing through all n points in arbitrary order.
Input: ['4', '3', '1 4 2', '1 3 1', '2', '2 4', '2 3', '2', '1 1000000000', '1 1000000000', '4', '3 10 5 8', '2 5 2 4', ''] Output:['0', '1', '999999999', '2', '']
[ 3 ]
You are given an array a of length n consisting of integers. You can apply the following operation, consisting of several steps, on the array a zero or more times: you select two different numbers in the array a_i and a_j; you remove i-th and j-th elements from the array. For example, if n=6 and a=[1, 6, 1, 1, 4, 4], then you can perform the following sequence of operations: select i=1, j=5. The array a becomes equal to [6, 1, 1, 4]; select i=1, j=2. The array a becomes equal to [1, 4]. What can be the minimum size of the array after applying some sequence of operations to it?
Input: ['5', '6', '1 6 1 1 4 4', '2', '1 2', '2', '1 1', '5', '4 5 4 5 4', '6', '2 3 2 1 3 1', ''] Output:['0', '0', '2', '1', '0', '']
[ 2 ]
You are given the strings a and b, consisting of lowercase Latin letters. You can do any number of the following operations in any order: if |a| > 0 (the length of the string a is greater than zero), delete the first character of the string a, that is, replace a with a_2 a_3 ... a_n; if |a| > 0, delete the last character of the string a, that is, replace a with a_1 a_2 ... a_{n-1}; if |b| > 0 (the length of the string b is greater than zero), delete the first character of the string b, that is, replace b with b_2 b_3 ... b_n; if |b| > 0, delete the last character of the string b, that is, replace b with b_1 b_2 ... b_{n-1}. Note that after each of the operations, the string a or b may become empty.For example, if a="hello" and b="icpc", then you can apply the following sequence of operations: delete the first character of the string a \Rightarrow a="ello" and b="icpc"; delete the first character of the string b \Rightarrow a="ello" and b="cpc"; delete the first character of the string b \Rightarrow a="ello" and b="pc"; delete the last character of the string a \Rightarrow a="ell" and b="pc"; delete the last character of the string b \Rightarrow a="ell" and b="p". For the given strings a and b, find the minimum number of operations for which you can make the strings a and b equal. Note that empty strings are also equal.
Input: ['5', 'a', 'a', 'abcd', 'bc', 'hello', 'codeforces', 'hello', 'helo', 'dhjakjsnasjhfksafasd', 'adjsnasjhfksvdafdser', ''] Output:['0', '2', '13', '3', '20', '']
[ 0 ]
You are given a number k and a string s of length n, consisting of the characters '.' and '*'. You want to replace some of the '*' characters with 'x' characters so that the following conditions are met: The first character '*' in the original string should be replaced with 'x'; The last character '*' in the original string should be replaced with 'x'; The distance between two neighboring replaced characters 'x' must not exceed k (more formally, if you replaced characters at positions i and j (i < j) and at positions [i+1, j-1] there is no "x" symbol, then j-i must be no more than k). For example, if n=7, s=.**.*** and k=3, then the following strings will satisfy the conditions above: .xx.*xx; .x*.x*x; .xx.xxx. But, for example, the following strings will not meet the conditions: .**.*xx (the first character '*' should be replaced with 'x'); .x*.xx* (the last character '*' should be replaced with 'x'); .x*.*xx (the distance between characters at positions 2 and 6 is greater than k=3). Given n, k, and s, find the minimum number of '*' characters that must be replaced with 'x' in order to meet the above conditions.
Input: ['5', '7 3', '.**.***', '5 1', '..*..', '5 2', '*.*.*', '3 2', '*.*', '1 1', '*', ''] Output:['3', '1', '3', '2', '1', '']
[ 2 ]
Polycarp found a rectangular table consisting of n rows and m columns. He noticed that each cell of the table has its number, obtained by the following algorithm "by columns": cells are numbered starting from one; cells are numbered from left to right by columns, and inside each column from top to bottom; number of each cell is an integer one greater than in the previous cell. For example, if n = 3 and m = 5, the table will be numbered as follows: \begin{matrix} 1 & 4 & 7 & 10 & 13 \\ 2 & 5 & 8 & 11 & 14 \\ 3 & 6 & 9 & 12 & 15 \\ \end{matrix} However, Polycarp considers such numbering inconvenient. He likes the numbering "by rows": cells are numbered starting from one; cells are numbered from top to bottom by rows, and inside each row from left to right; number of each cell is an integer one greater than the number of the previous cell. For example, if n = 3 and m = 5, then Polycarp likes the following table numbering: \begin{matrix} 1 & 2 & 3 & 4 & 5 \\ 6 & 7 & 8 & 9 & 10 \\ 11 & 12 & 13 & 14 & 15 \\ \end{matrix} Polycarp doesn't have much time, so he asks you to find out what would be the cell number in the numbering "by rows", if in the numbering "by columns" the cell has the number x?
Input: ['5', '1 1 1', '2 2 3', '3 5 11', '100 100 7312', '1000000 1000000 1000000000000', ''] Output:['1', '2', '9', '1174', '1000000000000', '']
[ 3 ]
*The two images are equivalent, feel free to use either one.
Input: ['1', ''] Output:['1', '']
[ 3 ]
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't.The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares.The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions...
Input: ['4 3', '*..', '.*.', '..*', '...', ''] Output:['3', '']
[ 2 ]
There is a binary string a of length n. In one operation, you can select any prefix of a with an equal number of 0 and 1 symbols. Then all symbols in the prefix are inverted: each 0 becomes 1 and each 1 becomes 0.For example, suppose a=0111010000. In the first operation, we can select the prefix of length 8 since it has four 0's and four 1's: [01110100]00\to [10001011]00. In the second operation, we can select the prefix of length 2 since it has one 0 and one 1: [10]00101100\to [01]00101100. It is illegal to select the prefix of length 4 for the third operation, because it has three 0's and one 1. Can you transform the string a into the string b using some finite number of operations (possibly, none)?
Input: ['5', '10', '0111010000', '0100101100', '4', '0000', '0000', '3', '001', '000', '12', '010101010101', '100110011010', '6', '000111', '110100', ''] Output:['YES', 'YES', 'NO', 'YES', 'NO', '']
[ 2, 3 ]
There is a grid with n rows and m columns. Every cell of the grid should be colored either blue or yellow.A coloring of the grid is called stupid if every row has exactly one segment of blue cells and every column has exactly one segment of yellow cells.In other words, every row must have at least one blue cell, and all blue cells in a row must be consecutive. Similarly, every column must have at least one yellow cell, and all yellow cells in a column must be consecutive. An example of a stupid coloring. Examples of clever colorings. The first coloring is missing a blue cell in the second row, and the second coloring has two yellow segments in the second column. How many stupid colorings of the grid are there? Two colorings are considered different if there is some cell that is colored differently.
Input: ['2 2', ''] Output:['2', '']
[ 3 ]
There is a deck of n cards. The i-th card has a number a_i on the front and a number b_i on the back. Every integer between 1 and 2n appears exactly once on the cards.A deck is called sorted if the front values are in increasing order and the back values are in decreasing order. That is, if a_i< a_{i+1} and b_i> b_{i+1} for all 1<= i<n.To flip a card i means swapping the values of a_i and b_i. You must flip some subset of cards (possibly, none), then put all the cards in any order you like. What is the minimum number of cards you must flip in order to sort the deck?
Input: ['5', '3 10', '6 4', '1 9', '5 8', '2 7', ''] Output:['2', '']
[ 2 ]
There are n cities numbered from 1 to n, and city i has beauty a_i.A salesman wants to start at city 1, visit every city exactly once, and return to city 1.For all i\ne j, a flight from city i to city j costs \max(c_i,a_j-a_i) dollars, where c_i is the price floor enforced by city i. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip.
Input: ['3', '1 9', '2 1', '4 1', ''] Output:['11', '']
[ 2, 4 ]
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not.You are given a binary string s of length n. Construct two balanced bracket sequences a and b of length n such that for all 1<= i<= n: if s_i=1, then a_i=b_i if s_i=0, then a_i\ne b_i If it is impossible, you should report about it.
Input: ['3', '6', '101101', '10', '1001101101', '4', '1100', ''] Output:['YES', '()()()', '((()))', 'YES', '()()((()))', '(())()()()', 'NO', '']
[ 2 ]
Vanya invented an interesting trick with a set of integers.Let an illusionist have a set of positive integers S. He names a positive integer x. Then an audience volunteer must choose some subset (possibly, empty) of S without disclosing it to the illusionist. The volunteer tells the illusionist the size of the chosen subset. And here comes the trick: the illusionist guesses whether the sum of the subset elements does not exceed x. The sum of elements of an empty subset is considered to be 0.Vanya wants to prepare the trick for a public performance. He prepared some set of distinct positive integers S. Vasya wants the trick to be successful. He calls a positive number x unsuitable, if he can't be sure that the trick would be successful for every subset a viewer can choose.Vanya wants to count the number of unsuitable integers for the chosen set S.Vanya plans to try different sets S. He wants you to write a program that finds the number of unsuitable integers for the initial set S, and after each change to the set S. Vanya will make q changes to the set, and each change is one of the following two types: add a new integer a to the set S, or remove some integer a from the set S.
Input: ['3 11', '1 2 3', '2 1', '1 5', '1 6', '1 7', '2 6', '2 2', '2 3', '1 10', '2 5', '2 7', '2 10', ''] Output:['4', '1', '6', '12', '19', '13', '8', '2', '10', '3', '0', '0', '']
[ 4 ]
You are given two tables A and B of size n * m. We define a sorting by column as the following: we choose a column and reorder the rows of the table by the value in this column, from the rows with the smallest value to the rows with the largest. In case there are two or more rows with equal value in this column, their relative order does not change (such sorting algorithms are called stable).You can find this behavior of sorting by column in many office software for managing spreadsheets. Petya works in one, and he has a table A opened right now. He wants to perform zero of more sortings by column to transform this table to table B.Determine if it is possible to do so, and if yes, find a sequence of columns to sort by. Note that you do not need to minimize the number of sortings.
Input: ['2 2', '2 2', '1 2', '1 2', '2 2', ''] Output:['1', '1']
[ 0, 2 ]
Vasya is a CEO of a big construction company. And as any other big boss he has a spacious, richly furnished office with two crystal chandeliers. To stay motivated Vasya needs the color of light at his office to change every day. That's why he ordered both chandeliers that can change its color cyclically. For example: red – brown – yellow – red – brown – yellow and so on. There are many chandeliers that differs in color set or order of colors. And the person responsible for the light made a critical mistake β€” they bought two different chandeliers.Since chandeliers are different, some days they will have the same color, but some days β€” different. Of course, it looks poor and only annoys Vasya. As a result, at the k-th time when chandeliers will light with different colors, Vasya will become very angry and, most probably, will fire the person who bought chandeliers.Your task is to calculate the day, when it happens (counting from the day chandeliers were installed). You can think that Vasya works every day without weekends and days off.
Input: ['4 2 4', '4 2 3 1', '2 1', ''] Output:['5', '']
[ 0, 3, 4 ]
It was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nastya's friend presented her an integer array a. Several hours after starting her journey home Nastya remembered about the present. To entertain herself she decided to check, are there four different indices x, y, z, w such that a_x + a_y = a_z + a_w.Her train has already arrived the destination, but she still hasn't found the answer. Can you help her unravel the mystery?
Input: ['6', '2 1 5 2 7 4', ''] Output:['YES', '2 3 1 6 ']
[ 0, 3 ]
You are given two strings x and y, both consist only of lowercase Latin letters. Let |s| be the length of string s.Let's call a sequence a a merging sequence if it consists of exactly |x| zeros and exactly |y| ones in some order.A merge z is produced from a sequence a by the following rules: if a_i=0, then remove a letter from the beginning of x and append it to the end of z; if a_i=1, then remove a letter from the beginning of y and append it to the end of z. Two merging sequences a and b are different if there is some position i such that a_i \neq b_i.Let's call a string z chaotic if for all i from 2 to |z| z_{i-1} \neq z_i.Let s[l,r] for some 1 <= l <= r <= |s| be a substring of consecutive letters of s, starting from position l and ending at position r inclusive.Let f(l_1, r_1, l_2, r_2) be the number of different merging sequences of x[l_1,r_1] and y[l_2,r_2] that produce chaotic merges. Note that only non-empty substrings of x and y are considered.Calculate \sum \limits_{1 <= l_1 <= r_1 <= |x| \\ 1 <= l_2 <= r_2 <= |y|} f(l_1, r_1, l_2, r_2). Output the answer modulo 998\,244\,353.
Input: ['aaa', 'bb', ''] Output:['24', '']
[ 3 ]
You are given three positive (greater than zero) integers c, d and x. You have to find the number of pairs of positive integers (a, b) such that equality c \cdot lcm(a, b) - d \cdot gcd(a, b) = x holds. Where lcm(a, b) is the least common multiple of a and b and gcd(a, b) is the greatest common divisor of a and b.
Input: ['4', '1 1 3', '4 2 6', '3 3 7', '2 7 25', ''] Output:['4', '3', '0', '8', '']
[ 3 ]
Let's say you are standing on the XY-plane at point (0, 0) and you want to reach point (n, n).You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your x coordinate, or up, i. e. vertically and in the direction that increase your y coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than n - 1 direction changes.As a result, your path will be a polygonal chain from (0, 0) to (n, n), consisting of at most n line segments where each segment has positive integer length and vertical and horizontal segments alternate.Not all paths are equal. You have n integers c_1, c_2, ..., c_n where c_i is the cost of the i-th segment.Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of k segments (k <= n), then the cost of the path is equal to \sum\limits_{i=1}^{k}{c_i \cdot length_i} (segments are numbered from 1 to k in the order they are in the path).Find the path of the minimum cost and print its cost.
Input: ['3', '2', '13 88', '3', '2 3 1', '5', '4 3 2 1 4', ''] Output:['202', '13', '19', '']
[ 0, 2, 3 ]
You are given a string s, consisting only of characters '0' or '1'. Let |s| be the length of s.You are asked to choose some integer k (k > 0) and find a sequence a of length k such that: 1 <= a_1 < a_2 < ... < a_k <= |s|; a_{i-1} + 1 < a_i for all i from 2 to k. The characters at positions a_1, a_2, ..., a_k are removed, the remaining characters are concatenated without changing the order. So, in other words, the positions in the sequence a should not be adjacent.Let the resulting string be s'. s' is called sorted if for all i from 2 to |s'| s'_{i-1} <= s'_i.Does there exist such a sequence a that the resulting string s' is sorted?
Input: ['5', '10101011011', '0000', '11111', '110', '1100', ''] Output:['YES', 'YES', 'YES', 'YES', 'NO', '']
[ 0, 2 ]
You have a board represented as a grid with 2 * n cells.The first k_1 cells on the first row and first k_2 cells on the second row are colored in white. All other cells are colored in black.You have w white dominoes (2 * 1 tiles, both cells are colored in white) and b black dominoes (2 * 1 tiles, both cells are colored in black).You can place a white domino on the board if both board's cells are white and not occupied by any other domino. In the same way, you can place a black domino if both cells are black and not occupied by any other domino.Can you place all w + b dominoes on the board if you can place dominoes both horizontally and vertically?
Input: ['5', '1 0 1', '1 0', '1 1 1', '0 0', '3 0 0', '1 3', '4 3 1', '2 2', '5 4 3', '3 1', ''] Output:['NO', 'YES', 'NO', 'YES', 'YES', '']
[ 3 ]
Alice and Bob are going to celebrate Christmas by playing a game with a tree of presents. The tree has n nodes (numbered 1 to n, with some node r as its root). There are a_i presents are hanging from the i-th node.Before beginning the game, a special integer k is chosen. The game proceeds as follows: Alice begins the game, with moves alternating each turn; in any move, the current player may choose some node (for example, i) which has depth at least k. Then, the player picks some positive number of presents hanging from that node, let's call it m (1 <= m <= a_i); the player then places these m presents on the k-th ancestor (let's call it j) of the i-th node (the k-th ancestor of vertex i is a vertex j such that i is a descendant of j, and the difference between the depth of j and the depth of i is exactly k). Now, the number of presents of the i-th node (a_i) is decreased by m, and, correspondingly, a_j is increased by m; Alice and Bob both play optimally. The player unable to make a move loses the game.For each possible root of the tree, find who among Alice or Bob wins the game.Note: The depth of a node i in a tree with root r is defined as the number of edges on the simple path from node r to node i. The depth of root r itself is zero.
Input: ['5 1', '1 2', '1 3', '5 2', '4 3', '0 3 2 4 4', ''] Output:['1 0 0 1 1 ']
[ 3 ]
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.There is a city in which Dixit lives. In the city, there are n houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the i-th house is k_i.Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of |k_A - k_B|, where k_i is the number of roads leading to the house i. If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given β€” for each house β€” only the number of incoming roads to that house (k_i).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
Input: ['3', '1 1 1', 'Yes'] Output:['? 1 2', '! 1 2']
[ 0, 2 ]
Gaurang has grown up in a mystical universe. He is faced by n consecutive 2D planes. He shoots a particle of decay age k at the planes.A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age k-1. If a particle has decay age equal to 1, it will NOT produce a copy.For example, if there are two planes and a particle is shot with decay age 3 (towards the right), the process is as follows: (here, D(x) refers to a single particle with decay age x) the first plane produces a D(2) to the left and lets D(3) continue on to the right; the second plane produces a D(2) to the left and lets D(3) continue on to the right; the first plane lets D(2) continue on to the left and produces a D(1) to the right; the second plane lets D(1) continue on to the right (D(1) cannot produce any copies). In total, the final multiset S of particles is \{D(3), D(2), D(2), D(1)\}. (See notes for visual explanation of this test case.)Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset S, given n and k.Since the size of the multiset can be very large, you have to output it modulo 10^9+7.Note: Particles can go back and forth between the planes without colliding with each other.
Input: ['4', '2 3', '2 2', '3 1', '1 3', ''] Output:['4', '3', '1', '2', '']
[ 0 ]
You are given n rectangles, each of height 1. Each rectangle's width is a power of 2 (i. e. it can be represented as 2^x for some non-negative integer x). You are also given a two-dimensional box of width W. Note that W may or may not be a power of 2. Moreover, W is at least as large as the width of the largest rectangle.You have to find the smallest height of this box, such that it is able to fit all the given rectangles. It is allowed to have some empty space left in this box after fitting all the rectangles.You cannot rotate the given rectangles to make them fit into the box. Moreover, any two distinct rectangles must not overlap, i. e., any two distinct rectangles must have zero intersection area.See notes for visual explanation of sample input.
Input: ['2', '5 16', '1 2 8 4 8', '6 10', '2 8 8 2 2 8', ''] Output:['2', '3', '']
[ 2, 4 ]
The \text{gcdSum} of a positive integer is the gcd of that integer with its sum of digits. Formally, \text{gcdSum}(x) = gcd(x, \text{ sum of digits of } x) for a positive integer x. gcd(a, b) denotes the greatest common divisor of a and b β€” the largest integer d such that both integers a and b are divisible by d.For example: \text{gcdSum}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3.Given an integer n, find the smallest integer x >= n such that \text{gcdSum}(x) > 1.
Input: ['3', '11', '31', '75', ''] Output:['12', '33', '75', '']
[ 0, 3 ]
This is the hard version of the problem. The only difference is that in this version 0 <=q k <=q 20.There is an array a_1, a_2, ..., a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square.Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer.What is the minimum number of continuous segments you should use if you will make changes optimally?
Input: ['35 218 6 2 4 111 46 2 2 8 9 1 3 6 3 9 71 01'] Output:['1', '2', '1', '']
[ 2, 3 ]
This is the easy version of the problem. The only difference is that in this version k = 0.There is an array a_1, a_2, ..., a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square.Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important.What is the minimum number of continuous segments you should use if you will make changes optimally?
Input: ['35 018 6 2 4 15 06 8 1 24 81 01'] Output:['3', '2', '1', '']
[ 2, 3 ]
It is the hard version of the problem. The only difference is that in this version 3 <= k <= n.You are given a positive integer n. Find k positive integers a_1, a_2, ..., a_k, such that: a_1 + a_2 + ... + a_k = n LCM(a_1, a_2, ..., a_k) <= \frac{n}{2} Here LCM is the least common multiple of numbers a_1, a_2, ..., a_k.We can show that for given constraints the answer always exists.
Input: ['2', '6 4', '9 5', ''] Output:['1 2 2 1 ', '1 3 3 1 1 ', '']
[ 3 ]
It is the easy version of the problem. The only difference is that in this version k = 3.You are given a positive integer n. Find k positive integers a_1, a_2, ..., a_k, such that: a_1 + a_2 + ... + a_k = n LCM(a_1, a_2, ..., a_k) <= \frac{n}{2} Here LCM is the least common multiple of numbers a_1, a_2, ..., a_k.We can show that for given constraints the answer always exists.
Input: ['3', '3 3', '8 3', '14 3', ''] Output:['1 1 1', '4 2 2', '2 6 6', '']
[ 3 ]
You are given an array a_1, a_2, ..., a_n consisting of n positive integers and a positive integer m.You should divide elements of this array into some arrays. You can order the elements in the new arrays as you want.Let's call an array m-divisible if for each two adjacent numbers in the array (two numbers on the positions i and i+1 are called adjacent for each i) their sum is divisible by m. An array of one element is m-divisible.Find the smallest number of m-divisible arrays that a_1, a_2, ..., a_n is possible to divide into.
Input: ['4', '6 4', '2 2 8 6 9 4', '10 8', '1 1 1 5 2 4 4 8 6 7', '1 1', '666', '2 2', '2 4', ''] Output:['3', '6', '1', '1', '']
[ 2, 3 ]
You are given an integer n and an array a_1, a_2, ..., a_n. You should reorder the elements of the array a in such way that the sum of \textbf{MEX} on prefixes (i-th prefix is a_1, a_2, ..., a_i) is maximized.Formally, you should find an array b_1, b_2, ..., b_n, such that the sets of elements of arrays a and b are equal (it is equivalent to array b can be found as an array a with some reordering of its elements) and \sum\limits_{i=1}^{n} \textbf{MEX}(b_1, b_2, ..., b_i) is maximized.\textbf{MEX} of a set of nonnegative integers is the minimal nonnegative integer such that it is not in the set.For example, \textbf{MEX}(\{1, 2, 3\}) = 0, \textbf{MEX}(\{0, 1, 2, 4, 5\}) = 3.
Input: ['3', '7', '4 2 0 1 3 3 7', '5', '2 2 8 6 9', '1', '0', ''] Output:['0 1 2 3 4 7 3 ', '2 6 8 9 2 ', '0 ', '']
[ 0, 2 ]
You are given a multiset S initially consisting of n distinct non-negative integers. A multiset is a set, that can contain some elements multiple times.You will perform the following operation k times: Add the element \lceil\frac{a+b}{2}\rceil (rounded up) into S, where a = \operatorname{mex}(S) and b = \max(S). If this number is already in the set, it is added again. Here \operatorname{max} of a multiset denotes the maximum integer in the multiset, and \operatorname{mex} of a multiset denotes the smallest non-negative integer that is not present in the multiset. For example: \operatorname{mex}(\{1,4,0,2\})=3; \operatorname{mex}(\{2,5,1\})=0. Your task is to calculate the number of distinct elements in S after k operations will be done.
Input: ['5', '4 1', '0 1 3 4', '3 1', '0 1 4', '3 0', '0 1 4', '3 2', '0 1 2', '3 2', '1 2 3', ''] Output:['4', '4', '3', '5', '3', '']
[ 3 ]
Kawashiro Nitori is a girl who loves competitive programming.One day she found a string and an integer. As an advanced problem setter, she quickly thought of a problem.Given a string s and a parameter k, you need to check if there exist k+1 non-empty strings a_1,a_2...,a_{k+1}, such that s=a_1+a_2+... +a_k+a_{k+1}+R(a_k)+R(a_{k-1})+...+R(a_{1}). Here + represents concatenation. We define R(x) as a reversed string x. For example R(abcd) = dcba. Note that in the formula above the part R(a_{k+1}) is intentionally skipped.
Input: ['7', '5 1', 'qwqwq', '2 1', 'ab', '3 1', 'ioi', '4 2', 'icpc', '22 0', 'dokidokiliteratureclub', '19 8', 'imteamshanghaialice', '6 3', 'aaaaaa', ''] Output:['YES', 'NO', 'YES', 'NO', 'YES', 'NO', 'NO', '']
[ 0, 2 ]
Qingshan and Daniel are going to play a card game. But it will be so boring if only two persons play this. So they will make n robots in total to play this game automatically. Robots made by Qingshan belong to the team 1, and robots made by Daniel belong to the team 2. Robot i belongs to team t_i. Before the game starts, a_i cards are given for robot i.The rules for this card game are simple: Before the start, the robots are arranged in a circle in the order or their indices. The robots will discard cards in some order, in each step one robot discards a single card. When the game starts, robot 1 will discard one of its cards. After that, robots will follow the following rules: If robot i discards the card last, the nearest robot whose team is opposite from i's will discard the card next. In another word j will discard a card right after i, if and only if among all j that satisfy t_i\ne t_j, dist(i,j) (definition is below) is minimum. The robot who has no cards should quit the game immediately. This robot won't be considered in the next steps. When no robot can discard the card next, the game ends. We define the distance from robot x to robot y as dist(x,y)=(y-x+n)\bmod n. It is similar to the oriented distance on the circle.For example, when n=5, the distance from 1 to 3 is dist(1,3)=(3-1+5)\bmod 5=2, the distance from 3 to 1 is dist(3,1)=(1-3+5)\bmod 5 =3.Later, Qingshan finds out that it will take so much time to see how robots play. She wants to know the result as quickly as possible. You, as Qingshan's fan, are asked to calculate an array [ans_1,ans_2,...,ans_n] β€” ans_i is equal to the number of cards, that i-th robot will discard during the game. You need to hurry!To avoid the large size of the input, the team and the number of cards of each robot will be generated in your code with some auxiliary arrays.
Input: ['3', '3', '1 5 2 3', '2 7 1 2', '3 2 1 1', ''] Output:['100', '']
[ 0, 2 ]
We define a spanning tree of a graph to be a BFS tree rooted at vertex s if and only if for every node t the shortest distance between s and t in the graph is equal to the shortest distance between s and t in the spanning tree. Given a graph, we define f(x,y) to be the number of spanning trees of that graph that are BFS trees rooted at vertices x and y at the same time.You are given an undirected connected graph with n vertices and m edges. Calculate f(i,j) for all i, j by modulo 998\,244\,353.
Input: ['4 4', '1 2', '2 3', '3 4', '1 4', ''] Output:['2 1 0 1', '1 2 1 0', '0 1 2 1', '1 0 1 2', '']
[ 3 ]
On a weekend, Qingshan suggests that she and her friend Daniel go hiking. Unfortunately, they are busy high school students, so they can only go hiking on scratch paper.A permutation p is written from left to right on the paper. First Qingshan chooses an integer index x (1<= x<= n) and tells it to Daniel. After that, Daniel chooses another integer index y (1<= y<= n, y!=x).The game progresses turn by turn and as usual, Qingshan moves first. The rules follow: If it is Qingshan's turn, Qingshan must change x to such an index x' that 1<= x'<= n, |x'-x|=1, x'\ne y, and p_{x'}<p_x at the same time. If it is Daniel's turn, Daniel must change y to such an index y' that 1<= y'<= n, |y'-y|=1, y'\ne x, and p_{y'}>p_y at the same time. The person who can't make her or his move loses, and the other wins. You, as Qingshan's fan, are asked to calculate the number of possible x to make Qingshan win in the case both players play optimally.
Input: ['5', '1 2 5 4 3', ''] Output:['1', '']
[ 2 ]
Diamond Miner is a game that is similar to Gold Miner, but there are n miners instead of 1 in this game.The mining area can be described as a plane. The n miners can be regarded as n points on the y-axis. There are n diamond mines in the mining area. We can regard them as n points on the x-axis. For some reason, no miners or diamond mines can be at the origin (point (0, 0)). Every miner should mine exactly one diamond mine. Every miner has a hook, which can be used to mine a diamond mine. If a miner at the point (a,b) uses his hook to mine a diamond mine at the point (c,d), he will spend \sqrt{(a-c)^2+(b-d)^2} energy to mine it (the distance between these points). The miners can't move or help each other.The object of this game is to minimize the sum of the energy that miners spend. Can you find this minimum?
Input: ['3', '2', '0 1', '1 0', '0 -1', '-2 0', '4', '1 0', '3 0', '-5 0', '6 0', '0 3', '0 1', '0 2', '0 4', '5', '3 0', '0 4', '0 -3', '4 0', '2 0', '1 0', '-3 0', '0 -10', '0 -2', '0 -10', ''] Output:['3.650281539872885', '18.061819283610362', '32.052255376143336', '']
[ 2, 3 ]
You are given an undirected connected graph consisting of n vertices and m edges. Your goal is to destroy all edges of the given graph.You may choose any vertex as the starting one and begin walking from it along the edges. When you walk along an edge, you destroy it. Obviously, you cannot walk along an edge if it is destroyed.You can perform the mode shift operation at most once during your walk, and this operation can only be performed when you are at some vertex (you cannot perform it while traversing an edge). After the mode shift, the edges you go through are deleted in the following way: the first edge after the mode shift is not destroyed, the second one is destroyed, the third one is not destroyed, the fourth one is destroyed, and so on. You cannot switch back to the original mode, and you don't have to perform this operation if you don't want to.Can you destroy all the edges of the given graph?
Input: ['3 3', '1 2', '2 3', '3 1', ''] Output:['4', '1 2 3 1', '']
[ 0 ]
The Dogeforces company has k employees. Each employee, except for lower-level employees, has at least 2 subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates.The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company.
Input: ['3', '2 5 7', '5 1 7', '7 7 4', ''] Output:['5', '2 1 4 7 5 ', '4', '1 5', '2 5', '5 4', '3 4', '']
[ 2 ]
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
Input: ['5', '5 6', '-1 1 5 11 15', '-4 -3 -2 6 7 15', '2 2', '-1 1', '-1000000000 1000000000', '2 2', '-1000000000 1000000000', '-1 1', '3 5', '-1 1 2', '-2 -1 1 2 5', '2 1', '1 2', '10', ''] Output:['4', '2', '0', '3', '1', '']
[ 2, 4 ]
Berland crossword is a puzzle that is solved on a square grid with n rows and n columns. Initially all the cells are white.To solve the puzzle one has to color some cells on the border of the grid black in such a way that: exactly U cells in the top row are black; exactly R cells in the rightmost column are black; exactly D cells in the bottom row are black; exactly L cells in the leftmost column are black. Note that you can color zero cells black and leave every cell white.Your task is to check if there exists a solution to the given puzzle.
Input: ['4', '5 2 5 3 1', '3 0 0 0 0', '4 4 1 4 0', '2 1 1 1 1', ''] Output:['YES', 'YES', 'NO', 'YES', '']
[ 0, 2 ]
You are given a string a, consisting of n characters, n is even. For each i from 1 to n a_i is one of 'A', 'B' or 'C'.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.You want to find a string b that consists of n characters such that: b is a regular bracket sequence; if for some i and j (1 <= i, j <= n) a_i=a_j, then b_i=b_j. In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.Your task is to determine if such a string b exists.
Input: ['4', 'AABBAC', 'CACA', 'BBBBAC', 'ABCA', ''] Output:['YES', 'YES', 'NO', 'NO', '']
[ 0 ]
You are given two integers l and r in binary representation. Let g(x, y) be equal to the bitwise XOR of all integers from x to y inclusive (that is x \oplus (x+1) \oplus ... \oplus (y-1) \oplus y). Let's define f(l, r) as the maximum of all values of g(x, y) satisfying l <= x <= y <= r.Output f(l, r).
Input: ['7', '0010011', '1111010', ''] Output:['1111111']
[ 2, 3 ]
You are given an array a of length n. You are asked to process q queries of the following format: given integers i and x, multiply a_i by x.After processing each query you need to output the greatest common divisor (GCD) of all elements of the array a.Since the answer can be too large, you are asked to output it modulo 10^9+7.
Input: ['4 3', '1 6 8 12', '1 12', '2 3', '3 3', ''] Output:['2', '2', '6', '']
[ 0, 3 ]
You are given a string s consisting of lowercase English letters and a number k. Let's call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that string is divisible by k. You are asked to find the lexicographically smallest beautiful string of length n, which is lexicographically greater or equal to string s. If such a string does not exist, output -1.A string a is lexicographically smaller than a string b if and only if one of the following holds: a is a prefix of b, but a!=b; in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
Input: ['4', '4 2', 'abcd', '3 1', 'abc', '4 3', 'aaaa', '9 3', 'abaabaaaa', ''] Output:['acac', 'abc', '-1', 'abaabaaab', '']
[ 0, 2, 4 ]
The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts h hours and each hour lasts m minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from 0 to h-1 and minutes are numbered from 0 to m-1. That's how the digits are displayed on the clock. Please note that digit 1 is placed in the middle of its position. A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).The image of the clocks in the mirror is reflected against a vertical axis. The reflection is not a valid time.The reflection is a valid time with h=24, m = 60. However, for example, if h=10, m=60, then the reflection is not a valid time. An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment s and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.It can be shown that with any h, m, s such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.You are asked to solve the problem for several test cases.
Input: ['5', '24 60', '12:21', '24 60', '23:59', '90 80', '52:26', '1 100', '00:01', '10 10', '04:04', ''] Output:['12:21', '00:00', '52:28', '00:00', '00:00', '']
[ 0 ]
You are given two integers n and k. You are asked to choose maximum number of distinct integers from 1 to n so that there is no subset of chosen numbers with sum equal to k.A subset of a set is a set that can be obtained from initial one by removing some (possibly all or none) elements of it.
Input: ['3', '3 2', '5 3', '1 1', ''] Output:['2', '3 1 ', '3', '4 5 2 ', '0', '', '']
[ 2 ]