question_text
stringlengths 2
3.82k
| input_outputs
stringlengths 23
941
| algo_tags
sequence |
---|---|---|
This problem differs from the previous problem only in constraints.Petya decided to visit Byteland during the summer holidays. It turned out that the history of this country is quite unusual.Initially, there were n different countries on the land that is now Berland. Each country had its own territory that was represented as a rectangle on the map. The sides of the rectangle were parallel to the axes, and the corners were located at points with integer coordinates. Territories of no two countries intersected, but it was possible that some territories touched each other. As time passed, sometimes two countries merged into one. It only happened if the union of their territories was also a rectangle. In the end only one country remained β Byteland.Initially, each country had a rectangular castle inside its territory. Its sides were parallel to the axes and its corners had integer coordinates. Some castles might touch the border of the corresponding country and sides or other castles. Miraculously, after all the unions the castles are still intact. Unfortunately, their locations are the only information we have to restore the initial territories of the countries. The possible formation of Byteland. The castles are shown in blue. Petya wonders why no information about the initial countries remained. He suspected that the whole story is a fake. You were recommended to him as a smart person. Please check whether or not there exists a possible set of initial territories that could make the story true. | Input: ['4', '0 0 1 2', '0 2 1 3', '1 0 2 1', '1 1 2 3', ''] Output:['YES', ''] | [
0,
2
] |
This problem differs from the next problem only in constraints.Petya decided to visit Byteland during the summer holidays. It turned out that the history of this country is quite unusual.Initially, there were n different countries on the land that is now Berland. Each country had its own territory that was represented as a rectangle on the map. The sides of the rectangle were parallel to the axes, and the corners were located at points with integer coordinates. Territories of no two countries intersected, but it was possible that some territories touched each other. As time passed, sometimes two countries merged into one. It only happened if the union of their territories was also a rectangle. In the end only one country remained β Byteland.Initially, each country had a rectangular castle inside its territory. Its sides were parallel to the axes and its corners had integer coordinates. Some castles might touch the border of the corresponding country and sides or other castles. Miraculously, after all the unions the castles are still intact. Unfortunately, their locations are the only information we have to restore the initial territories of the countries. The possible formation of Byteland. The castles are shown in blue. Petya wonders why no information about the initial countries remained. He suspected that the whole story is a fake. You were recommended to him as a smart person. Please check whether or not there exists a possible set of initial territories that could make the story true. | Input: ['4', '0 0 1 2', '0 2 1 3', '1 0 2 1', '1 1 2 3', ''] Output:['YES', ''] | [
0
] |
Misha was interested in water delivery from childhood. That's why his mother sent him to the annual Innovative Olympiad in Irrigation (IOI). Pupils from all Berland compete there demonstrating their skills in watering. It is extremely expensive to host such an olympiad, so after the first n olympiads the organizers introduced the following rule of the host city selection.The host cities of the olympiads are selected in the following way. There are m cities in Berland wishing to host the olympiad, they are numbered from 1 to m. The host city of each next olympiad is determined as the city that hosted the olympiad the smallest number of times before. If there are several such cities, the city with the smallest index is selected among them.Misha's mother is interested where the olympiad will be held in some specific years. The only information she knows is the above selection rule and the host cities of the first n olympiads. Help her and if you succeed, she will ask Misha to avoid flooding your house. | Input: ['6 4 10', '3 1 1 1 2 2', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', ''] Output:['4', '3', '4', '2', '3', '4', '1', '2', '3', '4', ''] | [
4
] |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in n \cdot m colored pieces that form a rectangle with n rows and m columns. The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. These subrectangles are flags. These subrectangles are not flags. | Input: ['4 3', 'aaa', 'bbb', 'ccb', 'ddd', ''] Output:['6', ''] | [
0
] |
Dima worked all day and wrote down on a long paper strip his favorite number n consisting of l digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf.To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip.Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. | Input: ['7', '1234567', ''] Output:['1801', ''] | [
2
] |
Soon after the Chunga-Changa island was discovered, it started to acquire some forms of civilization and even market economy. A new currency arose, colloquially called "chizhik". One has to pay in chizhiks to buy a coconut now.Sasha and Masha are about to buy some coconuts which are sold at price z chizhiks per coconut. Sasha has x chizhiks, Masha has y chizhiks. Each girl will buy as many coconuts as she can using only her money. This way each girl will buy an integer non-negative number of coconuts.The girls discussed their plans and found that the total number of coconuts they buy can increase (or decrease) if one of them gives several chizhiks to the other girl. The chizhiks can't be split in parts, so the girls can only exchange with integer number of chizhiks.Consider the following example. Suppose Sasha has 5 chizhiks, Masha has 4 chizhiks, and the price for one coconut be 3 chizhiks. If the girls don't exchange with chizhiks, they will buy 1 + 1 = 2 coconuts. However, if, for example, Masha gives Sasha one chizhik, then Sasha will have 6 chizhiks, Masha will have 3 chizhiks, and the girls will buy 2 + 1 = 3 coconuts. It is not that easy to live on the island now, so Sasha and Mash want to exchange with chizhiks in such a way that they will buy the maximum possible number of coconuts. Nobody wants to have a debt, so among all possible ways to buy the maximum possible number of coconuts find such a way that minimizes the number of chizhiks one girl gives to the other (it is not important who will be the person giving the chizhiks). | Input: ['5 4 3', ''] Output:['3 1', ''] | [
2,
3
] |
Nick had received an awesome array of integers a=[a_1, a_2, ..., a_n] as a gift for his 5 birthday from his mother. He was already going to explore its various properties but after unpacking he was disappointed a lot because the product a_1 \cdot a_2 \cdot ... a_n of its elements seemed to him not large enough.He was ready to throw out the array, but his mother reassured him. She told him, that array would not be spoiled after the following operation: choose any index i (1 <= i <= n) and do a_i := -a_i - 1.For example, he can change array [3, -1, -4, 1] to an array [-4, -1, 3, 1] after applying this operation to elements with indices i=1 and i=3. Kolya had immediately understood that sometimes it's possible to increase the product of integers of the array a lot. Now he has decided that he wants to get an array with the maximal possible product of integers using only this operation with its elements (possibly zero, one or more times, as many as he wants), it is not forbidden to do this operation several times for the same index. Help Kolya and print the array with the maximal possible product of elements a_1 \cdot a_2 \cdot ... a_n which can be received using only this operation in some order.If there are multiple answers, print any of them. | Input: ['4', '2 2 2 2', ''] Output:['-3 -3 -3 -3 '] | [
2
] |
While playing with geometric figures Alex has accidentally invented a concept of a n-th order rhombus in a cell grid.A 1-st order rhombus is just a square 1 * 1 (i.e just a cell).A n-th order rhombus for all n >=q 2 one obtains from a n-1-th order rhombus adding all cells which have a common side with it to it (look at the picture to understand it better). Alex asks you to compute the number of cells in a n-th order rhombus. | Input: ['1', ''] Output:['1'] | [
3
] |
Serge came to the school dining room and discovered that there is a big queue here. There are m pupils in the queue. He's not sure now if he wants to wait until the queue will clear, so he wants to know which dish he will receive if he does. As Serge is very tired, he asks you to compute it instead of him.Initially there are n dishes with costs a_1, a_2, ..., a_n. As you already know, there are the queue of m pupils who have b_1, ..., b_m togrogs respectively (pupils are enumerated by queue order, i.e the first pupil in the queue has b_1 togrogs and the last one has b_m togrogs)Pupils think that the most expensive dish is the most delicious one, so every pupil just buys the most expensive dish for which he has money (every dish has a single copy, so when a pupil has bought it nobody can buy it later), and if a pupil doesn't have money for any dish, he just leaves the queue (so brutal capitalism...)But money isn't a problem at all for Serge, so Serge is buying the most expensive dish if there is at least one remaining.Moreover, Serge's school has a very unstable economic situation and the costs of some dishes or number of togrogs of some pupils can change. More formally, you must process q queries: change a_i to x. It means that the price of the i-th dish becomes x togrogs. change b_i to x. It means that the i-th pupil in the queue has x togrogs now. Nobody leaves the queue during those queries because a saleswoman is late.After every query, you must tell Serge price of the dish which he will buy if he has waited until the queue is clear, or -1 if there are no dishes at this point, according to rules described above. | Input: ['1 1', '1', '1', '1', '1 1 100', ''] Output:['100', ''] | [
2,
3,
4
] |
Warning: This problem has an unusual memory limit!Bob decided that he will not waste his prime years implementing GUI forms for a large corporation and instead will earn his supper on the Stock Exchange Reykjavik. The Stock Exchange Reykjavik is the only actual stock exchange in the world. The only type of transaction is to take a single share of stock x and exchange it for a single share of stock y, provided that the current price of share x is at least the current price of share y. There are 2n stocks listed on the SER that are of interest to Bob, numbered from 1 to 2n. Bob owns a single share of stocks 1 through n and would like to own a single share of each of n+1 through 2n some time in the future.Bob managed to forecast the price of each stock β in time t >=q 0, the stock i will cost a_i \cdot \lfloor t \rfloor + b_i. The time is currently t = 0. Help Bob find the earliest moment in time in which he can own a single share of each of n+1 through 2n, and the minimum number of stock exchanges he has to perform in order to do that.You may assume that the Stock Exchange has an unlimited amount of each stock at any point in time. | Input: ['1', '3 10', '1 16', ''] Output:['3 1', ''] | [
4
] |
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides. Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters. | Input: ['cacbac', ''] Output:['aba', ''] | [
0,
2
] |
Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wanted to give her an affectionate gift but couldn't think of anything inventive. Hence, he will be giving her a graph. How original, Bob! Alice will surely be thrilled!When building the graph, he needs four conditions to be satisfied: It must be a simple undirected graph, i.e. without multiple (parallel) edges and self-loops. The number of vertices must be exactly n β a number he selected. This number is not necessarily prime. The total number of edges must be prime. The degree (i.e. the number of edges connected to the vertex) of each vertex must be prime. Below is an example for n = 4. The first graph (left one) is invalid as the degree of vertex 2 (and 4) equals to 1, which is not prime. The second graph (middle one) is invalid as the total number of edges is 4, which is not a prime number. The third graph (right one) is a valid answer for n = 4. Note that the graph can be disconnected.Please help Bob to find any such graph! | Input: ['4', ''] Output:['5', '1 2', '1 3', '2 3', '2 4', '3 4'] | [
2,
3
] |
Bob is decorating his kitchen, more precisely, the floor. He has found a prime candidate for the tiles he will use. They come in a simple form factor β a square tile that is diagonally split into white and black part as depicted in the figure below. The dimension of this tile is perfect for this kitchen, as he will need exactly w * h tiles without any scraps. That is, the width of the kitchen is w tiles, and the height is h tiles. As each tile can be rotated in one of four ways, he still needs to decide on how exactly he will tile the floor. There is a single aesthetic criterion that he wants to fulfil: two adjacent tiles must not share a colour on the edge β i.e. one of the tiles must have a white colour on the shared border, and the second one must be black. The picture on the left shows one valid tiling of a 3 * 2 kitchen. The picture on the right shows an invalid arrangement, as the bottom two tiles touch with their white parts. Find the number of possible tilings. As this number may be large, output its remainder when divided by 998244353 (a prime number). | Input: ['2 2', ''] Output:['16', ''] | [
2,
3
] |
Alice is the leader of the State Refactoring Party, and she is about to become the prime minister. The elections have just taken place. There are n parties, numbered from 1 to n. The i-th party has received a_i seats in the parliament.Alice's party has number 1. In order to become the prime minister, she needs to build a coalition, consisting of her party and possibly some other parties. There are two conditions she needs to fulfil: The total number of seats of all parties in the coalition must be a strict majority of all the seats, i.e. it must have strictly more than half of the seats. For example, if the parliament has 200 (or 201) seats, then the majority is 101 or more seats. Alice's party must have at least 2 times more seats than any other party in the coalition. For example, to invite a party with 50 seats, Alice's party must have at least 100 seats. For example, if n=4 and a=[51, 25, 99, 25] (note that Alice'a party has 51 seats), then the following set [a_1=51, a_2=25, a_4=25] can create a coalition since both conditions will be satisfied. However, the following sets will not create a coalition: [a_2=25, a_3=99, a_4=25] since Alice's party is not there; [a_1=51, a_2=25] since coalition should have a strict majority; [a_1=51, a_2=25, a_3=99] since Alice's party should have at least 2 times more seats than any other party in the coalition. Alice does not have to minimise the number of parties in a coalition. If she wants, she can invite as many parties as she wants (as long as the conditions are satisfied). If Alice's party has enough people to create a coalition on her own, she can invite no parties.Note that Alice can either invite a party as a whole or not at all. It is not possible to invite only some of the deputies (seats) from another party. In other words, if Alice invites a party, she invites all its deputies.Find and print any suitable coalition. | Input: ['3', '100 50 50', ''] Output:['2', '1 2', ''] | [
2
] |
Let's write all the positive integer numbers one after another from 1 without any delimiters (i.e. as a single string). It will be the infinite sequence starting with 123456789101112131415161718192021222324252627282930313233343536...Your task is to print the k-th digit of this sequence. | Input: ['7', ''] Output:['7', ''] | [
4
] |
Authors guessed an array a consisting of n integers; each integer is not less than 2 and not greater than 2 \cdot 10^5. You don't know the array a, but you know the array b which is formed from it with the following sequence of operations: Firstly, let the array b be equal to the array a; Secondly, for each i from 1 to n: if a_i is a prime number, then one integer p_{a_i} is appended to array b, where p is an infinite sequence of prime numbers (2, 3, 5, ...); otherwise (if a_i is not a prime number), the greatest divisor of a_i which is not equal to a_i is appended to b; Then the obtained array of length 2n is shuffled and given to you in the input. Here p_{a_i} means the a_i-th prime number. The first prime p_1 = 2, the second one is p_2 = 3, and so on.Your task is to recover any suitable array a that forms the given array b. It is guaranteed that the answer exists (so the array b is obtained from some suitable array a). If there are multiple answers, you can print any. | Input: ['3', '3 5 2 3 2 4', ''] Output:['3 4 2 '] | [
2
] |
You are given an array a consisting of n integers. Each a_i is one of the six following numbers: 4, 8, 15, 16, 23, 42.Your task is to remove the minimum number of elements to make this array good.An array of length k is called good if k is divisible by 6 and it is possible to split it into \frac{k}{6} subsequences 4, 8, 15, 16, 23, 42.Examples of good arrays: [4, 8, 15, 16, 23, 42] (the whole array is a required sequence); [4, 8, 4, 15, 16, 8, 23, 15, 16, 42, 23, 42] (the first sequence is formed from first, second, fourth, fifth, seventh and tenth elements and the second one is formed from remaining elements); [] (the empty array is good). Examples of bad arrays: [4, 8, 15, 16, 42, 23] (the order of elements should be exactly 4, 8, 15, 16, 23, 42); [4, 8, 15, 16, 23, 42, 4] (the length of the array is not divisible by 6); [4, 8, 15, 16, 23, 42, 4, 8, 15, 16, 23, 23] (the first sequence can be formed from first six elements but the remaining array cannot form the required sequence). | Input: ['5', '4 8 15 16 23', ''] Output:['5', ''] | [
2
] |
You are given an array a consisting of n integers a_1, a_2, ... , a_n.In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not matter where you insert the new element). For example, from the array [2, 1, 4] you can obtain the following arrays: [3, 4], [1, 6] and [2, 5].Your task is to find the maximum possible number of elements divisible by 3 that are in the array after performing this operation an arbitrary (possibly, zero) number of times.You have to answer t independent queries. | Input: ['2', '5', '3 1 2 3 1', '7', '1 1 1 1 1 2 2', ''] Output:['3', '3', ''] | [
3
] |
You are given an integer n.You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times: Replace n with \frac{n}{2} if n is divisible by 2; Replace n with \frac{2n}{3} if n is divisible by 3; Replace n with \frac{4n}{5} if n is divisible by 5. For example, you can replace 30 with 15 using the first operation, with 20 using the second operation or with 24 using the third operation.Your task is to find the minimum number of moves required to obtain 1 from n or say that it is impossible to do it.You have to answer q independent queries. | Input: ['7', '1', '10', '25', '30', '14', '27', '1000000000000000000', ''] Output:['0', '4', '6', '6', '-1', '6', '72', ''] | [
0,
2
] |
You have an array a_1, a_2, ..., a_n. Let's call some subarray a_l, a_{l + 1}, ... , a_r of this array a subpermutation if it contains all integers from 1 to r-l+1 exactly once. For example, array a = [2, 2, 1, 3, 2, 3, 1] contains 6 subarrays which are subpermutations: [a_2 ... a_3], [a_2 ... a_4], [a_3 ... a_3], [a_3 ... a_5], [a_5 ... a_7], [a_7 ... a_7].You are asked to calculate the number of subpermutations. | Input: ['8', '2 4 1 3 4 2 1 2', ''] Output:['7', ''] | [
0,
3
] |
You are given n intervals in form [l; r] on a number line.You are also given m queries in form [x; y]. What is the minimal number of intervals you have to take so that every point (not necessarily integer) from x to y is covered by at least one of them? If you can't choose intervals so that every point from x to y is covered, then print -1 for that query. | Input: ['2 3', '1 3', '2 4', '1 3', '1 4', '3 4', ''] Output:['1', '2', '1', ''] | [
2
] |
You are given an array a_1, a_2, ..., a_n and an integer k.You are asked to divide this array into k non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let f(i) be the index of subarray the i-th element belongs to. Subarrays are numbered from left to right and from 1 to k.Let the cost of division be equal to \sum\limits_{i=1}^{n} (a_i \cdot f(i)). For example, if a = [1, -2, -3, 4, -5, 6, -7] and we divide it into 3 subbarays in the following way: [1, -2, -3], [4, -5], [6, -7], then the cost of division is equal to 1 \cdot 1 - 2 \cdot 1 - 3 \cdot 1 + 4 \cdot 2 - 5 \cdot 2 + 6 \cdot 3 - 7 \cdot 3 = -9.Calculate the maximum cost you can obtain by dividing the array a into k non-empty consecutive subarrays. | Input: ['5 2', '-1 -2 5 -4 8', ''] Output:['15', ''] | [
2
] |
At first, there was a legend related to the name of the problem, but now it's just a formal statement.You are given n points a_1, a_2, ..., a_n on the OX axis. Now you are asked to find such an integer point x on OX axis that f_k(x) is minimal possible.The function f_k(x) can be described in the following way: form a list of distances d_1, d_2, ..., d_n where d_i = |a_i - x| (distance between a_i and x); sort list d in non-descending order; take d_{k + 1} as a result. If there are multiple optimal answers you can print any of them. | Input: ['3', '3 2', '1 2 5', '2 1', '1 1000000000', '1 0', '4', ''] Output:['3', '500000000', '4', ''] | [
0,
2,
4
] |
You are given an integer n and an integer k.In one step you can do one of the following moves: decrease n by 1; divide n by k if n is divisible by k. For example, if n = 27 and k = 3 you can do the following steps: 27 \rightarrow 26 \rightarrow 25 \rightarrow 24 \rightarrow 8 \rightarrow 7 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0.You are asked to calculate the minimum number of steps to reach 0 from n. | Input: ['2', '59 3', '1000000000000000000 10', ''] Output:['8', '19', ''] | [
3
] |
Let's define a function f(p) on a permutation p as follows. Let g_i be the greatest common divisor (GCD) of elements p_1, p_2, ..., p_i (in other words, it is the GCD of the prefix of length i). Then f(p) is the number of distinct elements among g_1, g_2, ..., g_n.Let f_{max}(n) be the maximum value of f(p) among all permutations p of integers 1, 2, ..., n.Given an integers n, count the number of permutations p of integers 1, 2, ..., n, such that f(p) is equal to f_{max}(n). Since the answer may be large, print the remainder of its division by 1000\,000\,007 = 10^9 + 7. | Input: ['2', ''] Output:['1'] | [
3
] |
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements? | Input: ['3', '1 2 2 1 3 1', ''] Output:['2 1 3 1 1 2'] | [
2
] |
Nauuo is a girl who loves playing chess.One day she invented a game by herself which needs n chess pieces to play on a m* m chessboard. The rows and columns are numbered from 1 to m. We denote a cell on the intersection of the r-th row and c-th column as (r,c).The game's goal is to place n chess pieces numbered from 1 to n on the chessboard, the i-th piece lies on (r_i,\,c_i), while the following rule is satisfied: for all pairs of pieces i and j, |r_i-r_j|+|c_i-c_j|>=|i-j|. Here |x| means the absolute value of x.However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.She wants to find the smallest chessboard on which she can put n pieces according to the rules.She also wonders how to place the pieces on such a chessboard. Can you help her? | Input: ['2', ''] Output:['2', '1 1', '1 2'] | [
2
] |
Nauuo is a girl who loves writing comments.One day, she posted a comment on Codeforces, wondering whether she would get upvotes or downvotes.It's known that there were x persons who would upvote, y persons who would downvote, and there were also another z persons who would vote, but you don't know whether they would upvote or downvote. Note that each of the x+y+z people would vote exactly one time.There are three different results: if there are more people upvote than downvote, the result will be "+"; if there are more people downvote than upvote, the result will be "-"; otherwise the result will be "0".Because of the z unknown persons, the result may be uncertain (i.e. there are more than one possible results). More formally, the result is uncertain if and only if there exist two different situations of how the z persons vote, that the results are different in the two situations.Tell Nauuo the result or report that the result is uncertain. | Input: ['3 7 0', ''] Output:['-'] | [
2
] |
Nauuo is a girl who loves playing cards.One day she was playing cards but found that the cards were mixed with some empty ones.There are n cards numbered from 1 to n, and they were mixed with another n empty cards. She piled up the 2n cards and drew n of them. The n cards in Nauuo's hands are given. The remaining n cards in the pile are also given in the order from top to bottom.In one operation she can choose a card in her hands and play it β put it at the bottom of the pile, then draw the top card from the pile.Nauuo wants to make the n numbered cards piled up in increasing order (the i-th card in the pile from top to bottom is the card i) as quickly as possible. Can you tell her the minimum number of operations? | Input: ['3', '0 2 0', '3 0 1', ''] Output:['2'] | [
2
] |
Polycarp has n wheels and a car with m slots for wheels. The initial pressure in the i-th wheel is a_i.Polycarp's goal is to take exactly m wheels among the given n wheels and equalize the pressure in them (then he can put these wheels in a car and use it for driving). In one minute he can decrease or increase the pressure in any (single) wheel by 1. He can increase the pressure no more than k times in total because it is hard to pump up wheels.Help Polycarp and say what is the minimum number of minutes he needs to spend to equalize the pressure of at least m wheels among the given n wheels. | Input: ['6 6 7', '6 15 16 20 1 5', ''] Output:['39', ''] | [
2,
4
] |
Imagine that you are the CEO of a big old-fashioned company. Unlike any modern and progressive company (such as JetBrains), your company has a dress code. That's why you have already allocated a spacious room for your employees where they can change their clothes. Moreover, you've already purchased an m-compartment wardrobe, so the i-th employee can keep his/her belongings in the i-th cell (of course, all compartments have equal widths).The issue has occurred: the wardrobe has sliding doors! More specifically, the wardrobe has n doors (numbered from left to right) and the j-th door has width equal to a_j wardrobe's cells. The wardrobe has single rails so that no two doors can slide past each other. Extremely schematic example of a wardrobe: m=9, n=2, a_1=2, a_2=3. The problem is as follows: sometimes to open some cells you must close some other cells (since all doors are placed on the single track). For example, if you have a 4-compartment wardrobe (i.e. m=4) with n=2 one-cell doors (i.e. a_1=a_2=1) and you need to open the 1-st and the 3-rd cells, you have to close the 2-nd and the 4-th cells.As CEO, you have a complete schedule for the next q days. Now you are wondering: is it possible that all employees who will come on the k-th day can access their cells simultaneously? | Input: ['3 10', '2 3 2', '6', '1 5', '2 1 10', '2 2 9', '2 5 6', '3 1 7 8', '4 1 2 3 4', ''] Output:['YES', 'YES', 'NO', 'NO', 'YES', 'NO', ''] | [
4
] |
We have three positive integers a, b and c. You don't know their values, but you know some information about them. Consider all three pairwise sums, i.e. the numbers a+b, a+c and b+c. You know exactly two (any) of three pairwise sums.Your task is to find such three positive integers a, b and c which match the given information. It means that if you consider a+b, a+c and b+c, two out of all three of them are given in the input. Among all such triples, you must choose one with the minimum possible sum a+b+c, and among all triples with the minimum sum, you can print any.You have to process q independent queries. | Input: ['3', '123 13', '2 2', '2000000000 2000000000', ''] Output:['111 1 12', '1 1 1', '1999999999 1 1', ''] | [
3
] |
The circle line of the Roflanpolis subway has n stations.There are two parallel routes in the subway. The first one visits stations in order 1 \to 2 \to ... \to n \to 1 \to 2 \to ... (so the next stop after station x is equal to (x+1) if x < n and 1 otherwise). The second route visits stations in order n \to (n-1) \to ... \to 1 \to n \to (n-1) \to ... (so the next stop after station x is equal to (x-1) if x>1 and n otherwise). All trains depart their stations simultaneously, and it takes exactly 1 minute to arrive at the next station.Two toads live in this city, their names are Daniel and Vlad.Daniel is currently in a train of the first route at station a and will exit the subway when his train reaches station x.Coincidentally, Vlad is currently in a train of the second route at station b and he will exit the subway when his train reaches station y.Surprisingly, all numbers a,x,b,y are distinct.Toad Ilya asks you to check if Daniel and Vlad will ever be at the same station at the same time during their journey. In other words, check if there is a moment when their trains stop at the same station. Note that this includes the moments when Daniel or Vlad enter or leave the subway. | Input: ['5 1 4 3 2', ''] Output:['YES', ''] | [
3
] |
Toad Mikhail has an array of 2^k integers a_1, a_2, ..., a_{2^k}.Find two permutations p and q of integers 0, 1, ..., 2^k-1, such that a_i is equal to p_i \oplus q_i for all possible i, or determine there are no such permutations. Here \oplus denotes the bitwise XOR operation. | Input: ['2', '0 1 2 3', ''] Output:['Shi', '2 0 1 3 ', '2 1 3 0 ', ''] | [
3
] |
Toad Rash has a binary string s. A binary string consists only of zeros and ones.Let n be the length of s.Rash needs to find the number of such pairs of integers l, r that 1 <=q l <=q r <=q n and there is at least one pair of integers x, k such that 1 <=q x, k <=q n, l <=q x < x + 2k <=q r, and s_x = s_{x+k} = s_{x+2k}.Find this number of pairs for Rash. | Input: ['010101', ''] Output:['3', ''] | [
0
] |
Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, ..., a_n.In one operation Zitz can choose an integer k and k indices i_1, i_2, ..., i_k such that 1 <=q i_1 < i_2 < ... < i_k <=q n. He should then change a_{i_j} to ((a_{i_j}+1) \bmod m) for each chosen integer i_j. The integer m is fixed for all operations and indices.Here x \bmod y denotes the remainder of the division of x by y.Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. | Input: ['5 3', '0 0 0 1 2', ''] Output:['0', ''] | [
2,
4
] |
So we got bored and decided to take our own guess at how would "Inception" production go if the budget for the film had been terribly low.The first scene we remembered was the one that features the whole city bending onto itself: It feels like it will require high CGI expenses, doesn't it? Luckily, we came up with a similar-looking scene which was a tiny bit cheaper to make.Firstly, forget about 3D, that's hard and expensive! The city is now represented as a number line (infinite to make it easier, of course).Secondly, the city doesn't have to look natural at all. There are n buildings on the line. Each building is a square 1 * 1. Buildings are numbered from 1 to n in ascending order of their positions. Lower corners of building i are at integer points a_i and a_i + 1 of the number line. Also the distance between any two neighbouring buildings i and i + 1 doesn't exceed d (really, this condition is here just to make the city look not that sparse). Distance between some neighbouring buildings i and i + 1 is calculated from the lower right corner of building i to the lower left corner of building i + 1.Finally, curvature of the bend is also really hard to simulate! Let the bend at some integer coordinate x be performed with the following algorithm. Take the ray from x to +\infty and all the buildings which are on this ray and start turning the ray and the buildings counter-clockwise around point x. At some angle some building will touch either another building or a part of the line. You have to stop bending there (implementing buildings crushing is also not worth its money). Let's call the angle between two rays in the final state the terminal angle \alpha_x.The only thing left is to decide what integer point x is the best to start bending around. Fortunately, we've already chosen m candidates to perform the bending.So, can you please help us to calculate terminal angle \alpha_x for each bend x from our list of candidates? | Input: ['3 5', '0 5 7', '9', '0 1 2 3 4 5 6 7 8', ''] Output:['1.570796326794897', '1.570796326794897', '0.785398163397448', '0.927295218001612', '0.785398163397448', '1.570796326794897', '1.570796326794897', '1.570796326794897', '1.570796326794897', ''] | [
0
] |
You are given an array a_1, a_2, ..., a_n. All a_i are pairwise distinct.Let's define function f(l, r) as follows: let's define array b_1, b_2, ..., b_{r - l + 1}, where b_i = a_{l - 1 + i}; sort array b in increasing order; result of the function f(l, r) is \sum\limits_{i = 1}^{r - l + 1}{b_i \cdot i}. Calculate <=ft(\sum\limits_{1 <= l <= r <= n}{f(l, r)}\right) \mod (10^9+7), i.e. total sum of f for all subsegments of a modulo 10^9+7. | Input: ['4', '5 2 4 7', ''] Output:['167', ''] | [
3
] |
You are given an array consisting of n integers a_1, a_2, ... , a_n and an integer x. It is guaranteed that for every i, 1 <= a_i <= x.Let's denote a function f(l, r) which erases all values such that l <= a_i <= r from the array a and returns the resulting array. For example, if a = [4, 1, 1, 4, 5, 2, 4, 3], then f(2, 4) = [1, 1, 5].Your task is to calculate the number of pairs (l, r) such that 1 <= l <= r <= x and f(l, r) is sorted in non-descending order. Note that the empty array is also considered sorted. | Input: ['3 3', '2 3 1', ''] Output:['4', ''] | [
4
] |
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular (shortly, RBS) if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are RBS and ")(" and "(()" are not.We can see that each opening bracket in RBS is paired with some closing bracket, and, using this fact, we can define nesting depth of the RBS as maximum number of bracket pairs, such that the 2-nd pair lies inside the 1-st one, the 3-rd one β inside the 2-nd one and so on. For example, nesting depth of "" is 0, "()()()" is 1 and "()((())())" is 3.Now, you are given RBS s of even length n. You should color each bracket of s into one of two colors: red or blue. Bracket sequence r, consisting only of red brackets, should be RBS, and bracket sequence, consisting only of blue brackets b, should be RBS. Any of them can be empty. You are not allowed to reorder characters in s, r or b. No brackets can be left uncolored.Among all possible variants you should choose one that minimizes maximum of r's and b's nesting depth. If there are multiple solutions you can print any of them. | Input: ['2', '()', ''] Output:['11', ''] | [
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 guessed some array a consisting of 6 integers. There are 6 special numbers β 4, 8, 15, 16, 23, 42 β and each of these numbers occurs in a exactly once (so, a is some permutation of these numbers).You don't know anything about their order, but you are allowed to ask up to 4 queries. In each query, you may choose two indices i and j (1 <= i, j <= 6, i and j are not necessarily distinct), and you will get the value of a_i \cdot a_j in return.Can you guess the array a?The array a is fixed beforehand in each test, the interaction program doesn't try to adapt to your queries. | Input: ['16', '64', '345', '672'] Output:['? 1 1', '? 2 2', '? 3 5', '? 4 6', '! 4 8 15 16 23 42'] | [
0,
3
] |
A telephone number is a sequence of exactly 11 digits, where the first digit is 8. For example, the sequence 80011223388 is a telephone number, but the sequences 70011223388 and 80000011223388 are not.You are given a string s of length n, consisting of digits.In one operation you can delete any character from string s. For example, it is possible to obtain strings 112, 111 or 121 from string 1121.You need to determine whether there is such a sequence of operations (possibly empty), after which the string s becomes a telephone number. | Input: ['2', '13', '7818005553535', '11', '31415926535', ''] Output:['YES', 'NO', ''] | [
0,
2
] |
Dora the explorer has decided to use her money after several years of juicy royalties to go shopping. What better place to shop than Nlogonia?There are n stores numbered from 1 to n in Nlogonia. The i-th of these stores offers a positive integer a_i.Each day among the last m days Dora bought a single integer from some of the stores. The same day, Swiper the fox bought a single integer from all the stores that Dora did not buy an integer from on that day.Dora considers Swiper to be her rival, and she considers that she beat Swiper on day i if and only if the least common multiple of the numbers she bought on day i is strictly greater than the least common multiple of the numbers that Swiper bought on day i.The least common multiple (LCM) of a collection of integers is the smallest positive integer that is divisible by all the integers in the collection.However, Dora forgot the values of a_i. Help Dora find out if there are positive integer values of a_i such that she beat Swiper on every day. You don't need to find what are the possible values of a_i though.Note that it is possible for some values of a_i to coincide in a solution. | Input: ['2 5', '3 1 2 3', '3 3 4 5', ''] Output:['possible', ''] | [
0,
3
] |
Given a positive integer m, we say that a sequence x_1, x_2, ..., x_n of positive integers is m-cute if for every index i such that 2 <= i <= n it holds that x_i = x_{i - 1} + x_{i - 2} + ... + x_1 + r_i for some positive integer r_i satisfying 1 <= r_i <= m.You will be given q queries consisting of three positive integers a, b and m. For each query you must determine whether or not there exists an m-cute sequence whose first term is a and whose last term is b. If such a sequence exists, you must additionally find an example of it. | Input: ['2', '5 26 2', '3 9 1', ''] Output:['4 5 6 13 26', '-1', ''] | [
0,
2,
3,
4
] |
The legend of the foundation of Vectorland talks of two integers x and y. Centuries ago, the array king placed two markers at points |x| and |y| on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland. Many years later, the vector king placed markers at points |x - y| and |x + y| and conquered all the land in between (including the endpoints), which he declared to be Vectorland. He did so in such a way that the land of Arrayland was completely inside (including the endpoints) the land of Vectorland.Here |z| denotes the absolute value of z.Now, Jose is stuck on a question of his history exam: "What are the values of x and y?" Jose doesn't know the answer, but he believes he has narrowed the possible answers down to n integers a_1, a_2, ..., a_n. Now, he wants to know the number of unordered pairs formed by two different elements from these n integers such that the legend could be true if x and y were equal to these two values. Note that it is possible that Jose is wrong, and that no pairs could possibly make the legend true. | Input: ['3', '2 5 -3', ''] Output:['2', ''] | [
4
] |
Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length k is vowelly if there are positive integers n and m such that n\cdot m = k and when the word is written by using n rows and m columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column.You are given an integer k and you must either print a vowelly word of length k or print -1 if no such word exists.In this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'. | Input: ['7', ''] Output:['-1', ''] | [
3
] |
There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a, b) and (b, a) are the same and counted only once.For example, if there are 6 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then: splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom), splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom). You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty. | Input: ['4', 'jorge', 'jose', 'oscar', 'jerry', ''] Output:['1', ''] | [
2
] |
The only difference between easy and hard versions is constraints.Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β and he won't start playing until he gets all of them.Each day (during the morning) Ivan earns exactly one burle.There are n types of microtransactions in the game. Each microtransaction costs 2 burles usually and 1 burle if it is on sale. Ivan has to order exactly k_i microtransactions of the i-th type (he orders microtransactions during the evening).Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for 1 burle and otherwise he can buy it for 2 burles.There are also m special offers in the game shop. The j-th offer (d_j, t_j) means that microtransactions of the t_j-th type are on sale during the d_j-th day.Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. | Input: ['5 6', '1 2 0 2 0', '2 4', '3 3', '1 5', '1 2', '1 5', '2 3', ''] Output:['8', ''] | [
2,
4
] |
The only difference between easy and hard versions is constraints.Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β and he won't start playing until he gets all of them.Each day (during the morning) Ivan earns exactly one burle.There are n types of microtransactions in the game. Each microtransaction costs 2 burles usually and 1 burle if it is on sale. Ivan has to order exactly k_i microtransactions of the i-th type (he orders microtransactions during the evening).Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for 1 burle and otherwise he can buy it for 2 burles.There are also m special offers in the game shop. The j-th offer (d_j, t_j) means that microtransactions of the t_j-th type are on sale during the d_j-th day.Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. | Input: ['5 6', '1 2 0 2 0', '2 4', '3 3', '1 5', '1 2', '1 5', '2 3', ''] Output:['8', ''] | [
2,
4
] |
You are given two arrays a and b, both of length n.Let's define a function f(l, r) = \sum\limits_{l <= i <= r} a_i \cdot b_i.Your task is to reorder the elements (choose an arbitrary order of elements) of the array b to minimize the value of \sum\limits_{1 <= l <= r <= n} f(l, r). Since the answer can be very large, you have to print it modulo 998244353. Note that you should minimize the answer but not its remainder. | Input: ['5', '1 8 7 2 4', '9 7 2 9 3', ''] Output:['646', ''] | [
2,
3
] |
We guessed some integer number x. You are given a list of almost all its divisors. Almost all means that there are all divisors except 1 and x in the list.Your task is to find the minimum possible integer x that can be the guessed number, or say that the input data is contradictory and it is impossible to find such number.You have to answer t independent queries. | Input: ['2', '8', '8 2 12 6 4 24 16 3', '1', '2', ''] Output:['48', '4', ''] | [
3
] |
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.You are given a string s, you have to delete minimum number of characters from this string so that it becomes good. | Input: ['4', 'good', ''] Output:['0', 'good', ''] | [
2
] |
Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β exactly 2 problems, during the third day β exactly 3 problems, and so on. During the k-th day he should solve k problems.Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training.How many days Polycarp can train if he chooses the contests optimally? | Input: ['4', '3 1 4 1', ''] Output:['3', ''] | [
2
] |
You are given a huge decimal number consisting of n digits. It is guaranteed that this number has no leading zeros. Each digit of this number is either 0 or 1.You may perform several (possibly zero) operations with this number. During each operation you are allowed to change any digit of your number; you may change 0 to 1 or 1 to 0. It is possible that after some operation you can obtain a number with leading zeroes, but it does not matter for this problem.You are also given two integers 0 <= y < x < n. Your task is to calculate the minimum number of operations you should perform to obtain the number that has remainder 10^y modulo 10^x. In other words, the obtained number should have remainder 10^y when divided by 10^x. | Input: ['11 5 2', '11010100101', ''] Output:['1', ''] | [
3
] |
Kuro has just learned about permutations and he is really excited to create a new permutation type. He has chosen n distinct positive integers and put all of them in a set S. Now he defines a magical permutation to be: A permutation of integers from 0 to 2^x - 1, where x is a non-negative integer. The bitwise xor of any two consecutive elements in the permutation is an element in S.Since Kuro is really excited about magical permutations, he wants to create the longest magical permutation possible. In other words, he wants to find the largest non-negative integer x such that there is a magical permutation of integers from 0 to 2^x - 1. Since he is a newbie in the subject, he wants you to help him find this value of x and also the magical permutation for that x. | Input: ['3', '1 2 3', ''] Output:['2', '0 1 3 2 '] | [
0,
3
] |
This problem is same as the previous one, but has larger constraints.It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem? | Input: ['4', '0 0', '1 1', '0 3', '1 2', ''] Output:['14', ''] | [
3
] |
This problem is same as the next one, but has smaller constraints.It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem? | Input: ['4', '0 0', '1 1', '0 3', '1 2', ''] Output:['14', ''] | [
0
] |
The three friends, Kuro, Shiro, and Katie, met up again! It's time for a party...What the cats do when they unite? Right, they have a party. Since they wanted to have as much fun as possible, they invited all their friends. Now n cats are at the party, sitting in a circle and eating soup. The rules are simple: anyone having finished their soup leaves the circle.Katie suddenly notices that whenever a cat leaves, the place where she was sitting becomes an empty space, which means the circle is divided into smaller continuous groups of cats sitting next to each other. At the moment Katie observes, there are m cats who left the circle. This raises a question for Katie: what is the maximum possible number of groups the circle is divided into at the moment?Could you help her with this curiosity?You can see the examples and their descriptions with pictures in the "Note" section. | Input: ['7 4', ''] Output:['3', ''] | [
2,
3
] |
You are given two n * m matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix \begin{bmatrix} 9&10&11\\ 11&12&14\\ \end{bmatrix} is increasing because each individual row and column is strictly increasing. On the other hand, the matrix \begin{bmatrix} 1&1\\ 2&3\\ \end{bmatrix} is not increasing because the first row is not strictly increasing.Let a position in the i-th row (from top) and j-th column (from left) in a matrix be denoted as (i, j). In one operation, you can choose any two numbers i and j and swap the number located in (i, j) in the first matrix with the number in (i, j) in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible". | Input: ['2 2', '2 10', '11 5', '9 4', '3 12', ''] Output:['Possible', ''] | [
0,
2
] |
Let's call an array of non-negative integers a_1, a_2, ..., a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 <=q i, j <=q n the inequality k \cdot |i - j| <=q min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists.You are given an array of non-negative integers a_1, a_2, ..., a_n. Find its expansion coefficient. | Input: ['4', '6 4 5 5', ''] Output:['1'] | [
3
] |
Vasya has a pile, that consists of some number of stones. n times he either took one stone from the pile or added one stone to the pile. The pile was non-empty before each operation of taking one stone from the pile.You are given n operations which Vasya has made. Find the minimal possible number of stones that can be in the pile after making these operations. | Input: ['3', '---', ''] Output:['0'] | [
3
] |
Let c be some positive integer. Let's call an array a_1, a_2, ..., a_n of positive integers c-array, if for all i condition 1 <=q a_i <=q c is satisfied. Let's call c-array b_1, b_2, ..., b_k a subarray of c-array a_1, a_2, ..., a_n, if there exists such set of k indices 1 <=q i_1 < i_2 < ... < i_k <=q n that b_j = a_{i_j} for all 1 <=q j <=q k. Let's define density of c-array a_1, a_2, ..., a_n as maximal non-negative integer p, such that any c-array, that contains p numbers is a subarray of a_1, a_2, ..., a_n.You are given a number c and some c-array a_1, a_2, ..., a_n. For all 0 <=q p <=q n find the number of sequences of indices 1 <=q i_1 < i_2 < ... < i_k <=q n for all 1 <=q k <=q n, such that density of array a_{i_1}, a_{i_2}, ..., a_{i_k} is equal to p. Find these numbers by modulo 998\,244\,353, because they can be too large. | Input: ['4 1', '1 1 1 1', ''] Output:['0 4 6 4 1 '] | [
3
] |
It is an interactive problem.Vasya enjoys solving quizzes. He found a strange device and wants to know how it works.This device encrypted with the tree (connected undirected graph without cycles) with n vertices, numbered with integers from 1 to n. To solve this quiz you should guess this tree.Fortunately, this device can make one operation, using which you should guess the cipher. You can give the device an array d_1, d_2, ..., d_n of non-negative integers. On the device, there are n lamps, i-th of them is connected with i-th vertex of the tree. For all i the light will turn on the i-th lamp, if there exist such vertex of the tree with number j \neq i that dist(i, j) <=q d_j. Let's define dist(i, j) as the distance between vertices i and j in tree or number of edges on the simple path between vertices i and j.Vasya wants to solve this quiz using <=q 80 operations with the device and guess the tree. Help him! | Input: ['5', '00000', '11011', '11100', '10010', ''] Output:['? 0 0 0 0 0', '? 1 1 2 0 2', '? 0 0 0 1 0', '? 0 1 0 0 1', '!', '4 2', '1 5', '3 4', '4 1', ''] | [
3,
4
] |
Vasya has n different points A_1, A_2, ... A_n on the plane. No three of them lie on the same line He wants to place them in some order A_{p_1}, A_{p_2}, ..., A_{p_n}, where p_1, p_2, ..., p_n β some permutation of integers from 1 to n.After doing so, he will draw oriented polygonal line on these points, drawing oriented segments from each point to the next in the chosen order. So, for all 1 <=q i <=q n-1 he will draw oriented segment from point A_{p_i} to point A_{p_{i+1}}. He wants to make this polygonal line satisfying 2 conditions: it will be non-self-intersecting, so any 2 segments which are not neighbors don't have common points. it will be winding. Vasya has a string s, consisting of (n-2) symbols "L" or "R". Let's call an oriented polygonal line winding, if its i-th turn left, if s_i = "L" and right, if s_i = "R". More formally: i-th turn will be in point A_{p_{i+1}}, where oriented segment from point A_{p_i} to point A_{p_{i+1}} changes to oriented segment from point A_{p_{i+1}} to point A_{p_{i+2}}. Let's define vectors \overrightarrow{v_1} = \overrightarrow{A_{p_i} A_{p_{i+1}}} and \overrightarrow{v_2} = \overrightarrow{A_{p_{i+1}} A_{p_{i+2}}}. Then if in order to rotate the vector \overrightarrow{v_1} by the smallest possible angle, so that its direction coincides with the direction of the vector \overrightarrow{v_2} we need to make a turn counterclockwise, then we say that i-th turn is to the left, and otherwise to the right. For better understanding look at this pictures with some examples of turns: There are left turns on this picture There are right turns on this picture You are given coordinates of the points A_1, A_2, ... A_n on the plane and string s. Find a permutation p_1, p_2, ..., p_n of the integers from 1 to n, such that the polygonal line, drawn by Vasya satisfy two necessary conditions. | Input: ['3', '1 1', '3 1', '1 3', 'L', ''] Output:['1 2 3'] | [
2,
3
] |
Vasya has written some permutation p_1, p_2, ..., p_n of integers from 1 to n, so for all 1 <=q i <=q n it is true that 1 <=q p_i <=q n and all p_1, p_2, ..., p_n are different. After that he wrote n numbers next_1, next_2, ..., next_n. The number next_i is equal to the minimal index i < j <=q n, such that p_j > p_i. If there is no such j let's let's define as next_i = n + 1.In the evening Vasya went home from school and due to rain, his notebook got wet. Now it is impossible to read some written numbers. Permutation and some values next_i are completely lost! If for some i the value next_i is lost, let's say that next_i = -1.You are given numbers next_1, next_2, ..., next_n (maybe some of them are equal to -1). Help Vasya to find such permutation p_1, p_2, ..., p_n of integers from 1 to n, that he can write it to the notebook and all numbers next_i, which are not equal to -1, will be correct. | Input: ['6', '3', '2 3 4', '2', '3 3', '3', '-1 -1 -1', '3', '3 4 -1', '1', '2', '4', '4 -1 4 5', ''] Output:['1 2 3', '2 1', '2 1 3', '-1', '1', '3 2 1 4', ''] | [
2,
3
] |
Let s be some string consisting of symbols "0" or "1". Let's call a string t a substring of string s, if there exists such number 1 <=q l <=q |s| - |t| + 1 that t = s_l s_{l+1} ... s_{l + |t| - 1}. Let's call a substring t of string s unique, if there exist only one such l. For example, let s = "1010111". A string t = "010" is an unique substring of s, because l = 2 is the only one suitable number. But, for example t = "10" isn't a unique substring of s, because l = 1 and l = 3 are suitable. And for example t ="00" at all isn't a substring of s, because there is no suitable l.Today Vasya solved the following problem at the informatics lesson: given a string consisting of symbols "0" and "1", the task is to find the length of its minimal unique substring. He has written a solution to this problem and wants to test it. He is asking you to help him.You are given 2 positive integers n and k, such that (n \bmod 2) = (k \bmod 2), where (x \bmod 2) is operation of taking remainder of x by dividing on 2. Find any string s consisting of n symbols "0" or "1", such that the length of its minimal unique substring is equal to k. | Input: ['4 4', ''] Output:['1111'] | [
3
] |
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 <=q i <=q n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 <=q j <=q m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, ..., a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, ..., b_{n,j}.You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 <=q i <=q n and 1 <=q j <=q m. You are given the numbers b_1, ..., b_n and g_1, ..., g_m, determine this number. | Input: ['3 2', '1 2 1', '3 4', ''] Output:['12'] | [
2,
3,
4
] |
You are given a binary matrix a of size n * m. A binary matrix is a matrix where each element is either 0 or 1.You may perform some (possibly zero) operations with this matrix. During each operation you can inverse the row of this matrix or a column of this matrix. Formally, inverting a row is changing all values in this row to the opposite (0 to 1, 1 to 0). Inverting a column is changing all values in this column to the opposite.Your task is to sort the initial matrix by some sequence of such operations. The matrix is considered sorted if the array [a_{1, 1}, a_{1, 2}, ..., a_{1, m}, a_{2, 1}, a_{2, 2}, ..., a_{2, m}, ..., a_{n, m - 1}, a_{n, m}] is sorted in non-descending order. | Input: ['2 2', '1 1', '0 1', ''] Output:['YES', '00', '10', ''] | [
0
] |
There are n people in a row. The height of the i-th person is a_i. You can choose any subset of these people and try to arrange them into a balanced circle.A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than 1. For example, let heights of chosen people be [a_{i_1}, a_{i_2}, ..., a_{i_k}], where k is the number of people you choose. Then the condition |a_{i_j} - a_{i_{j + 1}}| <= 1 should be satisfied for all j from 1 to k-1 and the condition |a_{i_1} - a_{i_k}| <= 1 should be also satisfied. |x| means the absolute value of x. It is obvious that the circle consisting of one person is balanced.Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists. | Input: ['7', '4 3 5 1 2 2 1', ''] Output:['5', '2 1 1 2 3', ''] | [
2
] |
You are given two arrays a and b, both of length n. All elements of both arrays are from 0 to n-1.You can reorder elements of the array b (if you want, you may leave the order of elements as it is). After that, let array c be the array of length n, the i-th element of this array is c_i = (a_i + b_i) \% n, where x \% y is x modulo y.Your task is to reorder elements of the array b to obtain the lexicographically minimum possible array c.Array x of length n is lexicographically less than array y of length n, if there exists such i (1 <= i <= n), that x_i < y_i, and for any j (1 <= j < i) x_j = y_j. | Input: ['4', '0 1 2 1', '3 2 1 1', ''] Output:['1 0 0 2 ', ''] | [
2,
4
] |
Polycarp has to solve exactly n problems to improve his programming skill before an important programming competition. But this competition will be held very soon, most precisely, it will start in k days. It means that Polycarp has exactly k days for training!Polycarp doesn't want to procrastinate, so he wants to solve at least one problem during each of k days. He also doesn't want to overwork, so if he solves x problems during some day, he should solve no more than 2x problems during the next day. And, at last, he wants to improve his skill, so if he solves x problems during some day, he should solve at least x+1 problem during the next day.More formally: let [a_1, a_2, ..., a_k] be the array of numbers of problems solved by Polycarp. The i-th element of this array is the number of problems Polycarp solves during the i-th day of his training. Then the following conditions must be satisfied: sum of all a_i for i from 1 to k should be n; a_i should be greater than zero for each i from 1 to k; the condition a_i < a_{i + 1} <= 2 a_i should be satisfied for each i from 1 to k-1. Your problem is to find any array a of length k satisfying the conditions above or say that it is impossible to do it. | Input: ['26 6', ''] Output:['YES', '1 2 4 5 6 8 ', ''] | [
2,
3
] |
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence a consisting of n integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence [1, 2, 4, 3, 2] the answer is 4 (you take 1 and the sequence becomes [2, 4, 3, 2], then you take the rightmost element 2 and the sequence becomes [2, 4, 3], then you take 3 and the sequence becomes [2, 4] and then you take 4 and the sequence becomes [2], the obtained increasing sequence is [1, 2, 3, 4]). | Input: ['5', '1 2 4 3 2', ''] Output:['4', 'LRRR', ''] | [
2
] |
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]). | Input: ['5', '2 1 5 4 3', ''] Output:['4', 'LRRR', ''] | [
2
] |
You are given a long decimal number a consisting of n digits from 1 to 9. You also have a function f that maps every digit from 1 to 9 to some (possibly the same) digit from 1 to 9.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in a, and replace each digit x from this segment with f(x). For example, if a = 1337, f(1) = 1, f(3) = 5, f(7) = 3, and you choose the segment consisting of three rightmost digits, you get 1553 as the result.What is the maximum possible number you can obtain applying this operation no more than once? | Input: ['4', '1337', '1 2 5 4 6 6 3 1 9', ''] Output:['1557', ''] | [
2
] |
Let's analyze a program written on some strange programming language. The variables in this language have names consisting of 1 to 4 characters, and each character is a lowercase or an uppercase Latin letter, or a digit. There is an extra constraint that the first character should not be a digit.There are four types of operations in the program, each denoted by one of the characters: , ^, # or &.Each line of the program has one of the following formats: <lvalue>=<rvalue>, where <lvalue> and <rvalue> are valid variable names; <lvalue>=<arg1><op><arg2>, where <lvalue>, <arg1> and <arg2> are valid variable names, and <op> is an operation character. The program is executed line-by-line, and the result of execution is stored in a variable having the name res. If res is never assigned in the program, then the result will be equal to the value of res before running the program.Two programs are called equivalent if no matter which operations do characters , ^, # and & denote (but, obviously, performing the same operation on the same arguments gives the same result) and which values do variables have before execution of program, the value of res after running the first program is equal to the value of res after running the second program (the programs are executed independently).You are given a program consisting of n lines. Your task is to write a program consisting of minimum possible number of lines that is equivalent to the program you are given. | Input: ['4', 'c=aa#bb', 'd12=c', 'res=c^d12', 'tmp=aa$c', ''] Output:['2', 'aaaa=aa#bb', 'res=aaaa^aaaa', ''] | [
2
] |
You have a bag which contains n cards. There is a number written on each card; the number on i-th card is a_i.You are playing the following game. During each turn, you choose and remove a random card from the bag (all cards that are still left inside the bag are chosen equiprobably). Nothing else happens during the first turn β but during the next turns, after removing a card (let the number on it be x), you compare it with the card that was removed during the previous turn (let the number on it be y). Possible outcomes are: if x < y, the game ends and you lose; if x = y, the game ends and you win; if x > y, the game continues. If there are no cards left in the bag, you lose. Cards are not returned into the bag after you remove them.You have to calculate the probability of winning in this game. It can be shown that it is in the form of \frac{P}{Q} where P and Q are non-negative integers and Q \neq 0, P <= Q. Output the value of P \cdot Q^{β1} ~(mod ~~ 998244353). | Input: ['5', '1 1 4 2 3', ''] Output:['299473306', ''] | [
3
] |
You are given a set of points x_1, x_2, ..., x_n on the number line.Two points i and j can be matched with each other if the following conditions hold: neither i nor j is matched with any other point; |x_i - x_j| >= z. What is the maximum number of pairs of points you can match with each other? | Input: ['4 2', '1 3 3 7', ''] Output:['2', ''] | [
2,
4,
4
] |
You are given a string, consisting of lowercase Latin letters.A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions (1, 2) β "ab" and (2, 3) β "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet.Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same.If there are multiple answers, print any of them.You also have to answer T separate queries. | Input: ['4', 'abcd', 'gg', 'codeforces', 'abaca', ''] Output:['cadb', 'gg', 'codfoerces', 'No answer', ''] | [
2
] |
The whole delivery market of Berland is controlled by two rival companies: BerEx and BerPS. They both provide fast and reliable delivery services across all the cities of Berland.The map of Berland can be represented as an undirected graph. The cities are vertices and the roads are edges between them. Each pair of cities has no more than one road between them. Each road connects different cities.BerEx and BerPS are so competitive that for each pair of cities (v, u) they have set up their paths from v to u in such a way that these two paths don't share a single road. It is guaranteed that it was possible.Now Berland government decided to cut down the road maintenance cost by abandoning some roads. Obviously, they want to maintain as little roads as possible. However, they don't want to break the entire delivery system. So BerEx and BerPS should still be able to have their paths between every pair of cities non-intersecting.What is the minimal number of roads Berland government can maintain?More formally, given a 2-edge connected undirected graph, what is the minimum number of edges that can be left in it so that the resulting graph is also 2-edge connected? | Input: ['3 3', '1 2', '2 3', '3 1', ''] Output:['3', '1 3', '3 2', '1 2', ''] | [
0
] |
Jury picked a polynomial f(x) = a_0 + a_1 \cdot x + a_2 \cdot x^2 + ... + a_k \cdot x^k. k <= 10 and all a_i are integer numbers and 0 <= a_i < 10^6 + 3. It's guaranteed that there is at least one i such that a_i > 0.Now jury wants you to find such an integer x_0 that f(x_0) \equiv 0 \mod (10^6 + 3) or report that there is not such x_0.You can ask no more than 50 queries: you ask value x_q and jury tells you value f(x_q) \mod (10^6 + 3).Note that printing the answer doesn't count as a query. | Input: [' ', '1000002', '', '0', ''] Output:['? 0', '', '? 1', '', '! 1'] | [
0,
3
] |
You are given an array a consisting of n integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0.You may choose at most one consecutive subarray of a and multiply all values contained in this subarray by x. You want to maximize the beauty of array after applying at most one such operation. | Input: ['5 -2', '-3 8 -2 1 -6', ''] Output:['22', ''] | [
0,
2
] |
Ivan is going to sleep now and wants to set his alarm clock. There will be many necessary events tomorrow, the i-th of them will start during the x_i-th minute. Ivan doesn't want to skip any of the events, so he has to set his alarm clock in such a way that it rings during minutes x_1, x_2, ..., x_n, so he will be awake during each of these minutes (note that it does not matter if his alarm clock will ring during any other minute).Ivan can choose two properties for the alarm clock β the first minute it will ring (let's denote it as y) and the interval between two consecutive signals (let's denote it by p). After the clock is set, it will ring during minutes y, y + p, y + 2p, y + 3p and so on.Ivan can choose any minute as the first one, but he cannot choose any arbitrary value of p. He has to pick it among the given values p_1, p_2, ..., p_m (his phone does not support any other options for this setting).So Ivan has to choose the first minute y when the alarm clock should start ringing and the interval between two consecutive signals p_j in such a way that it will ring during all given minutes x_1, x_2, ..., x_n (and it does not matter if his alarm clock will ring in any other minutes).Your task is to tell the first minute y and the index j such that if Ivan sets his alarm clock with properties y and p_j it will ring during all given minutes x_1, x_2, ..., x_n or say that it is impossible to choose such values of the given properties. If there are multiple answers, you can print any. | Input: ['3 5', '3 12 18', '2 6 5 3 3', ''] Output:['YES', '3 4', ''] | [
3
] |
A telephone number is a sequence of exactly 11 digits such that its first digit is 8.Vasya and Petya are playing a game. Initially they have a string s of length n (n is odd) consisting of digits. Vasya makes the first move, then players alternate turns. In one move the player must choose a character and erase it from the current string. For example, if the current string 1121, after the player's move it may be 112, 111 or 121. The game ends when the length of string s becomes 11. If the resulting string is a telephone number, Vasya wins, otherwise Petya wins.You have to determine if Vasya has a winning strategy (that is, if Vasya can win the game no matter which characters Petya chooses during his moves). | Input: ['13', '8380011223344', ''] Output:['YES', ''] | [
2
] |
You are given an array a consisting of n integers a_1, a_2, ..., a_n.Your problem is to find such pair of indices i, j (1 <= i < j <= n) that lcm(a_i, a_j) is minimum possible.lcm(x, y) is the least common multiple of x and y (minimum positive number such that both x and y are divisors of this number). | Input: ['5', '2 4 8 3 6', ''] Output:['1 2', ''] | [
0,
2,
3
] |
There are n shovels in the nearby shop. The i-th shovel costs a_i bourles.Misha has to buy exactly k shovels. Each shovel can be bought no more than once.Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset.There are also m special offers in the shop. The j-th of them is given as a pair (x_j, y_j), and it means that if Misha buys exactly x_j shovels during one purchase then y_j most cheapest of them are for free (i.e. he will not pay for y_j most cheapest shovels during the current purchase).Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers).Your task is to calculate the minimum cost of buying k shovels, if Misha buys them optimally. | Input: ['7 4 5', '2 5 4 2 6 3 1', '2 1', '6 5', '2 1', '3 1', ''] Output:['7', ''] | [
2
] |
There is a robot staying at X=0 on the Ox axis. He has to walk to X=n. You are controlling this robot and controlling how he goes. The robot has a battery and an accumulator with a solar panel.The i-th segment of the path (from X=i-1 to X=i) can be exposed to sunlight or not. The array s denotes which segments are exposed to sunlight: if segment i is exposed, then s_i = 1, otherwise s_i = 0.The robot has one battery of capacity b and one accumulator of capacity a. For each segment, you should choose which type of energy storage robot will use to go to the next point (it can be either battery or accumulator). If the robot goes using the battery, the current charge of the battery is decreased by one (the robot can't use the battery if its charge is zero). And if the robot goes using the accumulator, the current charge of the accumulator is decreased by one (and the robot also can't use the accumulator if its charge is zero).If the current segment is exposed to sunlight and the robot goes through it using the battery, the charge of the accumulator increases by one (of course, its charge can't become higher than it's maximum capacity).If accumulator is used to pass some segment, its charge decreases by 1 no matter if the segment is exposed or not.You understand that it is not always possible to walk to X=n. You want your robot to go as far as possible. Find the maximum number of segments of distance the robot can pass if you control him optimally. | Input: ['5 2 1', '0 1 0 1 0', ''] Output:['5', ''] | [
2
] |
Polycarp has a cat and his cat is a real gourmet! Dependent on a day of the week he eats certain type of food: on Mondays, Thursdays and Sundays he eats fish food; on Tuesdays and Saturdays he eats rabbit stew; on other days of week he eats chicken stake. Polycarp plans to go on a trip and already packed his backpack. His backpack contains: a daily rations of fish food; b daily rations of rabbit stew; c daily rations of chicken stakes. Polycarp has to choose such day of the week to start his trip that his cat can eat without additional food purchases as long as possible. Print the maximum number of days the cat can eat in a trip without additional food purchases, if Polycarp chooses the day of the week to start his trip optimally. | Input: ['2 1 1', ''] Output:['4', ''] | [
3
] |
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.You can choose any non-negative integer D (i.e. D >= 0), and for each a_i you can: add D (only once), i.βe. perform a_i := a_i + D, or subtract D (only once), i.βe. perform a_i := a_i - D, or leave the value of a_i unchanged. It is possible that after an operation the value a_i becomes negative.Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n).Print the required D or, if it is impossible to choose such value D, print -1.For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4]. | Input: ['6', '1 4 4 7 4 1', ''] Output:['3', ''] | [
3
] |
Polycarp has guessed three positive integers a, b and c. He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order β their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: a+b, a+c, b+c and a+b+c.You have to guess three numbers a, b and c using given numbers. Print three guessed integers in any order.Pay attention that some given numbers a, b and c can be equal (it is also possible that a=b=c). | Input: ['3 6 5 4', ''] Output:['2 1 3', ''] | [
3
] |
Getting closer and closer to a mathematician, Serval becomes a university student on math major in Japari University. On the Calculus class, his teacher taught him how to calculate the expected length of a random subsegment of a given segment. Then he left a bonus problem as homework, with the award of a garage kit from IOI. The bonus is to extend this problem to the general case as follows.You are given a segment with length l. We randomly choose n segments by choosing two points (maybe with non-integer coordinates) from the given segment equiprobably and the interval between the two points forms a segment. You are given the number of random segments n, and another integer k. The 2n endpoints of the chosen segments split the segment into (2n+1) intervals. Your task is to calculate the expected total length of those intervals that are covered by at least k segments of the n random segments.You should find the answer modulo 998244353. | Input: ['1 1 1', ''] Output:['332748118', ''] | [
3
] |
This is an interactive problem.Now Serval is a senior high school student in Japari Middle School. However, on the way to the school, he must go across a pond, in which there is a dangerous snake. The pond can be represented as a n * n grid. The snake has a head and a tail in different cells, and its body is a series of adjacent cells connecting the head and the tail without self-intersecting. If Serval hits its head or tail, the snake will bite him and he will die.Luckily, he has a special device which can answer the following question: you can pick a rectangle, it will tell you the number of times one needs to cross the border of the rectangle walking cell by cell along the snake from the head to the tail. The pictures below show a possible snake and a possible query to it, which will get an answer of 4. Today Serval got up too late and only have time to make 2019 queries. As his best friend, can you help him find the positions of the head and the tail?Note that two cells are adjacent if and only if they have a common edge in the grid, and a snake can have a body of length 0, that means it only has adjacent head and tail.Also note that the snake is sleeping, so it won't move while Serval using his device. And what's obvious is that the snake position does not depend on your queries. | Input: ['2', '', '1', '', '0', '', '0', ''] Output:['', '? 1 1 1 1', '', '? 1 2 1 2', '', '? 2 2 2 2', '', '! 1 1 2 1'] | [
0,
4
] |
Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before. As a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree.A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a node v is the last different from v vertex on the path from the root to the vertex v. Children of vertex v are all nodes for which v is the parent. A vertex is a leaf if it has no children.The rooted tree Serval owns has n nodes, node 1 is the root. Serval will write some numbers into all nodes of the tree. However, there are some restrictions. Each of the nodes except leaves has an operation \max or \min written in it, indicating that the number in this node should be equal to the maximum or minimum of all the numbers in its sons, respectively. Assume that there are k leaves in the tree. Serval wants to put integers 1, 2, ..., k to the k leaves (each number should be used exactly once). He loves large numbers, so he wants to maximize the number in the root. As his best friend, can you help him? | Input: ['6', '1 0 1 1 0 1', '1 2 2 2 2', ''] Output:['1', ''] | [
2,
4
] |
Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School.In his favorite math class, the teacher taught him the following interesting definitions.A parenthesis sequence is a string, containing only characters "(" and ")".A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition.We define that |s| as the length of string s. A strict prefix s[1... l] (1<=q l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition.Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence.After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. | Input: ['6', '(?????', ''] Output:['(()())'] | [
2
] |
Luckily, Serval got onto the right bus, and he came to the kindergarten on time. After coming to kindergarten, he found the toy bricks very funny.He has a special interest to create difficult problems for others to solve. This time, with many 1 * 1 * 1 toy bricks, he builds up a 3-dimensional object. We can describe this object with a n * m matrix, such that in each cell (i,j), there are h_{i,j} bricks standing on the top of each other.However, Serval doesn't give you any h_{i,j}, and just give you the front view, left view, and the top view of this object, and he is now asking you to restore the object. Note that in the front view, there are m columns, and in the i-th of them, the height is the maximum of h_{1,i},h_{2,i},...,h_{n,i}. It is similar for the left view, where there are n columns. And in the top view, there is an n * m matrix t_{i,j}, where t_{i,j} is 0 or 1. If t_{i,j} equals 1, that means h_{i,j}>0, otherwise, h_{i,j}=0.However, Serval is very lonely because others are bored about his unsolvable problems before, and refused to solve this one, although this time he promises there will be at least one object satisfying all the views. As his best friend, can you have a try? | Input: ['3 7 3', '2 3 0 0 2 0 1', '2 1 3', '1 0 0 0 1 0 0', '0 0 0 0 0 0 1', '1 1 0 0 0 0 0', ''] Output:['1 0 0 0 2 0 0', '0 0 0 0 0 0 1', '2 3 0 0 0 0 0', ''] | [
2
] |
It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there. The only choice for this poor little boy is to wait for a bus on this rainy day. Under such circumstances, the poor boy will use the first bus he sees no matter where it goes. If several buses come at the same time, he will choose one randomly.Serval will go to the bus station at time t, and there are n bus routes which stop at this station. For the i-th bus route, the first bus arrives at time s_i minutes, and each bus of this route comes d_i minutes later than the previous one.As Serval's best friend, you wonder which bus route will he get on. If several buses arrive at the same time, you can print any of them. | Input: ['2 2', '6 4', '9 5', ''] Output:['1', ''] | [
0,
3
] |
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a trie of all correct bracket sequences of length 2n.The definition of correct bracket sequence is as follows: The empty sequence is a correct bracket sequence, If s is a correct bracket sequence, then (\,s\,) is a correct bracket sequence, If s and t are a correct bracket sequence, then st is also a correct bracket sequence. For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7. | Input: ['1', ''] Output:['1', ''] | [
2
] |
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher.Neko has two integers a and b. His goal is to find a non-negative integer k such that the least common multiple of a+k and b+k is the smallest possible. If there are multiple optimal integers k, he needs to choose the smallest one.Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? | Input: ['6 10', ''] Output:['2'] | [
0,
3
] |
Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.Assume that we have a cat with a number x. A perfect longcat is a cat with a number equal 2^m - 1 for some non-negative integer m. For example, the numbers 0, 1, 3, 7, 15 and so on are suitable for the perfect longcats.In the Cat Furrier Transform, the following operations can be performed on x: (Operation A): you select any non-negative integer n and replace x with x \oplus (2^n - 1), with \oplus being a bitwise XOR operator. (Operation B): replace x with x + 1. The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most 40 operations. Can you help Neko writing a transformation plan?Note that it is not required to minimize the number of operations. You just need to use no more than 40 operations. | Input: ['39', ''] Output:['4', '5 3 '] | [
3
] |
On a random day, Neko found n treasure chests and m keys. The i-th chest has an integer a_i written on it and the j-th key has an integer b_j on it. Neko knows those chests contain the powerful mysterious green Grapes, thus Neko wants to open as many treasure chests as possible.The j-th key can be used to unlock the i-th chest if and only if the sum of the key number and the chest number is an odd number. Formally, a_i + b_j \equiv 1 \pmod{2}. One key can be used to open at most one chest, and one chest can be opened at most once.Find the maximum number of chests Neko can open. | Input: ['5 4', '9 14 6 2 11', '8 4 7 20', ''] Output:['3'] | [
2,
3
] |
The Kingdom of Kremland is a tree (a connected undirected graph without cycles) consisting of n vertices. Each vertex i has its own value a_i. All vertices are connected in series by edges. Formally, for every 1 <=q i < n there is an edge between the vertices of i and i+1.Denote the function f(l, r), which takes two integers l and r (l <=q r): We leave in the tree only vertices whose values ββrange from l to r. The value of the function will be the number of connected components in the new graph. Your task is to calculate the following sum: \sum_{l=1}^{n} \sum_{r=l}^{n} f(l, r) | Input: ['3', '2 1 3', ''] Output:['7'] | [
3
] |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.