Problem ID stringlengths 2 6 | Problem Description stringlengths 0 7.52k | Rating float64 800 3.5k | math bool 2 classes | greedy bool 2 classes | implementation bool 2 classes | dp bool 2 classes | data structures bool 2 classes | constructive algorithms bool 2 classes | brute force bool 2 classes | binary search bool 2 classes | sortings bool 2 classes | graphs bool 2 classes | __index_level_0__ int64 3 9.98k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
413E | The last product of the R2 company in the 2D games' field is a new revolutionary algorithm of searching for the shortest path in a 2u2009Γu2009_n_ maze. Imagine a maze that looks like a 2u2009Γu2009_n_ rectangle, divided into unit squares. Each unit square is either an empty cell or an obstacle. In one unit of time, a person can move from an empty cell of the maze to any side-adjacent empty cell. The shortest path problem is formulated as follows. Given two free maze cells, you need to determine the minimum time required to go from one cell to the other. Unfortunately, the developed algorithm works well for only one request for finding the shortest path, in practice such requests occur quite often. You, as the chief R2 programmer, are commissioned to optimize the algorithm to find the shortest path. Write a program that will effectively respond to multiple requests to find the shortest path in a 2u2009Γu2009_n_ maze. Input The first line contains two integers, _n_ and _m_ (1u2009β€u2009_n_u2009β€u20092Β·105;xa01u2009β€u2009_m_u2009β€u20092Β·105) β the width of the maze and the number of queries, correspondingly. Next two lines contain the maze. Each line contains _n_ characters, each character equals either '.' (empty cell), or 'X' (obstacle). Each of the next _m_ lines contains two integers _v__i_ and _u__i_ (1u2009β€u2009_v__i_,u2009_u__i_u2009β€u20092_n_) β the description of the _i_-th request. Numbers _v__i_, _u__i_ mean that you need to print the value of the shortest path from the cell of the maze number _v__i_ to the cell number _u__i_. We assume that the cells of the first line of the maze are numbered from 1 to _n_, from left to right, and the cells of the second line are numbered from _n_u2009+u20091 to 2_n_ from left to right. It is guaranteed that both given cells are empty. Output Print _m_ lines. In the _i_-th line print the answer to the _i_-th request β either the size of the shortest path or -1, if we can't reach the second cell from the first one. Examples Input 4 7 .X.. ...X 5 1 1 3 7 7 1 4 6 1 4 7 5 7 Input 10 3 X...X..X.. ..X...X..X 11 7 7 18 18 10 | 2,200 | false | false | false | false | true | false | false | false | false | false | 8,170 |
449B | Jzzhu is the president of country A. There are _n_ cities numbered from 1 to _n_ in his country. City 1 is the capital of A. Also there are _m_ roads connecting the cities. One can go from city _u__i_ to _v__i_ (and vise versa) using the _i_-th road, the length of this road is _x__i_. Finally, there are _k_ train routes in the country. One can use the _i_-th train route to go from capital of the country to city _s__i_ (and vise versa), the length of this route is _y__i_. Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change. Input The first line contains three integers _n_,u2009_m_,u2009_k_ (2u2009β€u2009_n_u2009β€u2009105;xa01u2009β€u2009_m_u2009β€u20093Β·105;xa01u2009β€u2009_k_u2009β€u2009105). Each of the next _m_ lines contains three integers _u__i_,u2009_v__i_,u2009_x__i_ (1u2009β€u2009_u__i_,u2009_v__i_u2009β€u2009_n_;xa0_u__i_u2009β u2009_v__i_;xa01u2009β€u2009_x__i_u2009β€u2009109). Each of the next _k_ lines contains two integers _s__i_ and _y__i_ (2u2009β€u2009_s__i_u2009β€u2009_n_;xa01u2009β€u2009_y__i_u2009β€u2009109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital. Output Output a single integer representing the maximum number of the train routes which can be closed. Examples Input 5 5 3 1 2 1 2 3 2 1 3 3 3 4 4 1 5 5 3 5 4 5 5 5 Input 2 2 3 1 2 2 2 1 3 2 1 2 2 2 3 | 2,000 | false | true | false | false | false | false | false | false | false | true | 8,043 |
1630B | Given an array $$$a$$$ of $$$n$$$ integers, find a range of values $$$[x, y]$$$ ($$$x le y$$$), and split $$$a$$$ into exactly $$$k$$$ ($$$1 le k le n$$$) subarrays in such a way that: Each subarray is formed by several continuous elements of $$$a$$$, that is, it is equal to $$$a_l, a_{l+1}, ldots, a_r$$$ for some $$$l$$$ and $$$r$$$ ($$$1 leq l leq r leq n$$$). Each element from $$$a$$$ belongs to exactly one subarray. In each subarray the number of elements inside the range $$$[x, y]$$$ (inclusive) is strictly greater than the number of elements outside the range. An element with index $$$i$$$ is inside the range $$$[x, y]$$$ if and only if $$$x le a_i le y$$$. Print any solution that minimizes $$$y - x$$$. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 3 cdot 10^4$$$) β the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2 cdot 10^5$$$) β the length of the array $$$a$$$ and the number of subarrays required in the partition. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$) where $$$a_i$$$ is the $$$i$$$-th element of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output For each test case, print $$$k+1$$$ lines. In the first line, print $$$x$$$ and $$$y$$$ β the limits of the found range. Then print $$$k$$$ lines, the $$$i$$$-th should contain $$$l_i$$$ and $$$r_i$$$ ($$$1leq l_i leq r_i leq n$$$) β the limits of the $$$i$$$-th subarray. You can print the subarrays in any order. Note In the first test, there should be only one subarray, which must be equal to the whole array. There are $$$2$$$ elements inside the range $$$[1, 2]$$$ and $$$0$$$ elements outside, if the chosen range is $$$[1, 1]$$$, there will be $$$1$$$ element inside ($$$a_1$$$) and $$$1$$$ element outside ($$$a_2$$$), and the answer will be invalid. In the second test, it is possible to choose the range $$$[2, 2]$$$, and split the array in subarrays $$$(1, 3)$$$ and $$$(4, 4)$$$, in subarray $$$(1, 3)$$$ there are $$$2$$$ elements inside the range ($$$a_2$$$ and $$$a_3$$$) and $$$1$$$ element outside ($$$a_1$$$), in subarray $$$(4, 4)$$$ there is only $$$1$$$ element ($$$a_4$$$), and it is inside the range. In the third test, it is possible to choose the range $$$[5, 5]$$$, and split the array in subarrays $$$(1, 4)$$$, $$$(5, 7)$$$ and $$$(8, 11)$$$, in the subarray $$$(1, 4)$$$ there are $$$3$$$ elements inside the range and $$$1$$$ element outside, in the subarray $$$(5, 7)$$$ there are $$$2$$$ elements inside and $$$1$$$ element outside and in the subarray $$$(8, 11)$$$ there are $$$3$$$ elements inside and $$$1$$$ element outside. | 1,800 | false | true | false | false | true | true | false | true | false | false | 2,465 |
1151C | Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for his outstanding mathematical abilities. Today a math teacher gave him a very difficult task. Consider two infinite sets of numbers. The first set consists of odd positive numbers ($$$1, 3, 5, 7, ldots$$$), and the second set consists of even positive numbers ($$$2, 4, 6, 8, ldots$$$). At the first stage, the teacher writes the first number on the endless blackboard from the first set, in the second stagexa0β the first two numbers from the second set, on the third stagexa0β the next four numbers from the first set, on the fourthxa0β the next eight numbers from the second set and so on. In other words, at each stage, starting from the second, he writes out two times more numbers than at the previous one, and also changes the set from which these numbers are written out to another. The ten first written numbers: $$$1, 2, 4, 3, 5, 7, 9, 6, 8, 10$$$. Let's number the numbers written, starting with one. The task is to find the sum of numbers with numbers from $$$l$$$ to $$$r$$$ for given integers $$$l$$$ and $$$r$$$. The answer may be big, so you need to find the remainder of the division by $$$1000000007$$$ ($$$10^9+7$$$). Nazar thought about this problem for a long time, but didn't come up with a solution. Help him solve this problem. Note In the first example, the answer is the sum of the first three numbers written out ($$$1 + 2 + 4 = 7$$$). In the second example, the numbers with numbers from $$$5$$$ to $$$14$$$: $$$5, 7, 9, 6, 8, 10, 12, 14, 16, 18$$$. Their sum is $$$105$$$. | 1,800 | true | false | false | false | false | true | false | false | false | false | 4,960 |
1777E | You will be given a weighted directed graph of $$$n$$$ nodes and $$$m$$$ directed edges, where the $$$i$$$-th edge has a weight of $$$w_i$$$ ($$$1 le i le m$$$). You need to reverse some edges of this graph so that there is at least one node in the graph from which every other node is reachable. The cost of these reversals is equal to the maximum weight of all reversed edges. If no edge reversal is required, assume the cost to be $$$0$$$. It is guaranteed that no self-loop or duplicate edge exists. Find the minimum cost required for completing the task. If there is no solution, print a single integer $$$-1$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. Each test case begins with a line containing two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le m le 2 cdot 10^5$$$) β the number of nodes in the graph and the number of edges in the graph. The next $$$m$$$ lines of each test case contain $$$3$$$ integers each β $$$u$$$, $$$v$$$, $$$w$$$ ($$$1 le u, v le n$$$, $$$1 le w le 10^9$$$), indicating an edge from $$$u$$$ to $$$v$$$ with a weight of $$$w$$$. It is guaranteed that no edge connects a vertex to itself, and no pair of edges share the same origin and destination simultaneously. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2 cdot 10^5$$$. Output For each test case, output the minimum cost. If there is no solution, print $$$-1$$$. Example Input 4 2 1 1 2 3 5 4 1 2 10 2 3 10 3 1 10 4 5 10 4 5 1 2 10000 2 3 20000 1 3 30000 4 2 500 4 3 20 4 5 1 2 10000 2 3 20000 1 3 30000 4 2 5 4 3 20 Note In the first test case, an edge exists from $$$1$$$ to $$$2$$$, so all nodes are reachable (from $$$1$$$). In the second test case, no nodes are reachable from any node no matter what edges we reverse, so the answer is $$$-1$$$. In the third test case, reversing the $$$4$$$-th or $$$5$$$-th edge allows all nodes to be reachable from $$$1$$$. We choose the $$$5$$$-th edge here because its weight is smaller. | 2,200 | false | false | false | false | false | false | false | true | false | true | 1,610 |
2004B | There are $$$100$$$ rooms arranged in a row and $$$99$$$ doors between them; the $$$i$$$-th door connects rooms $$$i$$$ and $$$i+1$$$. Each door can be either locked or unlocked. Initially, all doors are unlocked. We say that room $$$x$$$ is reachable from room $$$y$$$ if all doors between them are unlocked. You know that: Alice is in some room from the segment $$$[l, r]$$$; Bob is in some room from the segment $$$[L, R]$$$; Alice and Bob are in different rooms. However, you don't know the exact rooms they are in. You don't want Alice and Bob to be able to reach each other, so you are going to lock some doors to prevent that. What's the smallest number of doors you have to lock so that Alice and Bob cannot meet, regardless of their starting positions inside the given segments? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The first line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l < r le 100$$$)xa0β the bounds of the segment of rooms where Alice is located. The second line of each test case contains two integers $$$L$$$ and $$$R$$$ ($$$1 le L < R le 100$$$) β the bounds of the segment of rooms where Bob is located. Output For each test case, print a single integerxa0β the smallest number of doors you have to lock so that Alice and Bob cannot meet, regardless of their starting positions inside the given segments. Example Input 4 1 2 3 4 2 5 2 5 3 7 6 7 4 5 2 8 Note In the first test case, it is sufficient to lock the door between rooms $$$2$$$ and $$$3$$$. In the second test case, the following doors have to be locked: $$$(2,3)$$$, $$$(3,4)$$$, $$$(4,5)$$$. In the third test case, the following doors have to be locked: $$$(5, 6)$$$ and $$$(6,7)$$$. | 1,000 | false | true | false | false | false | false | true | false | false | false | 230 |
215B | The World Programming Olympics Medal is a metal disk, consisting of two parts: the first part is a ring with outer radius of _r_1 cm, inner radius of _r_2 cm, (0u2009<u2009_r_2u2009<u2009_r_1) made of metal with density _p_1 g/cm3. The second part is an inner disk with radius _r_2 cm, it is made of metal with density _p_2 g/cm3. The disk is nested inside the ring. The Olympic jury decided that _r_1 will take one of possible values of _x_1,u2009_x_2,u2009...,u2009_x__n_. It is up to jury to decide which particular value _r_1 will take. Similarly, the Olympic jury decided that _p_1 will take one of possible value of _y_1,u2009_y_2,u2009...,u2009_y__m_, and _p_2 will take a value from list _z_1,u2009_z_2,u2009...,u2009_z__k_. According to most ancient traditions the ratio between the outer ring mass _m__out_ and the inner disk mass _m__in_ must equal , where _A_,u2009_B_ are constants taken from ancient books. Now, to start making medals, the jury needs to take values for _r_1, _p_1, _p_2 and calculate the suitable value of _r_2. The jury wants to choose the value that would maximize radius _r_2. Help the jury find the sought value of _r_2. Value _r_2 doesn't have to be an integer. Medal has a uniform thickness throughout the area, the thickness of the inner disk is the same as the thickness of the outer ring. Input The first input line contains an integer _n_ and a sequence of integers _x_1,u2009_x_2,u2009...,u2009_x__n_. The second input line contains an integer _m_ and a sequence of integers _y_1,u2009_y_2,u2009...,u2009_y__m_. The third input line contains an integer _k_ and a sequence of integers _z_1,u2009_z_2,u2009...,u2009_z__k_. The last line contains two integers _A_ and _B_. All numbers given in the input are positive and do not exceed 5000. Each of the three sequences contains distinct numbers. The numbers in the lines are separated by spaces. | 1,300 | true | true | false | false | false | false | false | false | false | false | 8,980 |
1644D | There is a sheet of paper that can be represented with a grid of size $$$n imes m$$$: $$$n$$$ rows and $$$m$$$ columns of cells. All cells are colored in white initially. $$$q$$$ operations have been applied to the sheet. The $$$i$$$-th of them can be described as follows: $$$x_i$$$ $$$y_i$$$xa0β choose one of $$$k$$$ non-white colors and color the entire row $$$x_i$$$ and the entire column $$$y_i$$$ in it. The new color is applied to each cell, regardless of whether the cell was colored before the operation. The sheet after applying all $$$q$$$ operations is called a coloring. Two colorings are different if there exists at least one cell that is colored in different colors. How many different colorings are there? Print the number modulo $$$998,244,353$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of testcases. The first line of the testcase contains four integers $$$n, m, k$$$ and $$$q$$$ ($$$1 le n, m, k, q le 2 cdot 10^5$$$)xa0β the size of the sheet, the number of non-white colors and the number of operations. The $$$i$$$-th of the following $$$q$$$ lines contains a description of the $$$i$$$-th operationxa0β two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i le n$$$; $$$1 le y_i le m$$$)xa0β the row and the column the operation is applied to. The sum of $$$q$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0β the number of different colorings modulo $$$998,244,353$$$. Example Input 2 1 1 3 2 1 1 1 1 2 2 2 3 2 1 1 1 2 2 | 1,700 | true | false | true | false | true | false | false | false | false | false | 2,398 |
1428E | There are some rabbits in Singapore Zoo. To feed them, Zookeeper bought $$$n$$$ carrots with lengths $$$a_1, a_2, a_3, ldots, a_n$$$. However, rabbits are very fertile and multiply very quickly. Zookeeper now has $$$k$$$ rabbits and does not have enough carrots to feed all of them. To solve this problem, Zookeeper decided to cut the carrots into $$$k$$$ pieces. For some reason, all resulting carrot lengths must be positive integers. Big carrots are very difficult for rabbits to handle and eat, so the time needed to eat a carrot of size $$$x$$$ is $$$x^2$$$. Help Zookeeper split his carrots while minimizing the sum of time taken for rabbits to eat the carrots. Input The first line contains two integers $$$n$$$ and $$$k$$$ $$$(1 leq n leq k leq 10^5)$$$: the initial number of carrots and the number of rabbits. The next line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ $$$(1 leq a_i leq 10^6)$$$: lengths of carrots. It is guaranteed that the sum of $$$a_i$$$ is at least $$$k$$$. Output Output one integer: the minimum sum of time taken for rabbits to eat carrots. Note For the first test, the optimal sizes of carrots are $$${1,1,1,2,2,2}$$$. The time taken is $$$1^2+1^2+1^2+2^2+2^2+2^2=15$$$ For the second test, the optimal sizes of carrots are $$${4,5,5,5}$$$. The time taken is $$$4^2+5^2+5^2+5^2=91$$$. | 2,200 | true | true | false | false | true | false | false | true | true | false | 3,521 |
593E | Gosha's universe is a table consisting of _n_ rows and _m_ columns. Both the rows and columns are numbered with consecutive integers starting with 1. We will use (_r_,u2009_c_) to denote a cell located in the row _r_ and column _c_. Gosha is often invited somewhere. Every time he gets an invitation, he first calculates the number of ways to get to this place, and only then he goes. Gosha's house is located in the cell (1,u20091). At any moment of time, Gosha moves from the cell he is currently located in to a cell adjacent to it (two cells are adjacent if they share a common side). Of course, the movement is possible only if such a cell exists, i.e. Gosha will not go beyond the boundaries of the table. Thus, from the cell (_r_,u2009_c_) he is able to make a move to one of the cells (_r_u2009-u20091,u2009_c_), (_r_,u2009_c_u2009-u20091), (_r_u2009+u20091,u2009_c_), (_r_,u2009_c_u2009+u20091). Also, Ghosha can skip a move and stay in the current cell (_r_,u2009_c_). Besides the love of strange calculations, Gosha is allergic to cats, so he never goes to the cell that has a cat in it. Gosha knows exactly where and when he will be invited and the schedule of cats travelling along the table. Formally, he has _q_ records, the _i_-th of them has one of the following forms: 1, _x__i_, _y__i_, _t__i_xa0β Gosha is invited to come to cell (_x__i_,u2009_y__i_) at the moment of time _t__i_. It is guaranteed that there is no cat inside cell (_x__i_,u2009_y__i_) at this moment of time. 2, _x__i_, _y__i_, _t__i_xa0β at the moment _t__i_ a cat appears in cell (_x__i_,u2009_y__i_). It is guaranteed that no other cat is located in this cell (_x__i_,u2009_y__i_) at that moment of time. 3, _x__i_, _y__i_, _t__i_xa0β at the moment _t__i_ a cat leaves cell (_x__i_,u2009_y__i_). It is guaranteed that there is cat located in the cell (_x__i_,u2009_y__i_). Gosha plans to accept only one invitation, but he has not yet decided, which particular one. In order to make this decision, he asks you to calculate for each of the invitations _i_ the number of ways to get to the cell (_x__i_,u2009_y__i_) at the moment _t__i_. For every invitation, assume that Gosha he starts moving from cell (1,u20091) at the moment 1. Moving between two neighboring cells takes Gosha exactly one unit of tim. In particular, this means that Gosha can come into the cell only if a cat sitting in it leaves the moment when Gosha begins his movement from the neighboring cell, and if none of the cats comes to the cell at the time when Gosha is in it. Two ways to go from cell (1,u20091) to cell (_x_,u2009_y_) at time _t_ are considered distinct if for at least one moment of time from 1 to _t_ Gosha's positions are distinct for the two ways at this moment. Note, that during this travel Gosha is allowed to visit both (1,u20091) and (_x_,u2009_y_) multiple times. Since the number of ways can be quite large, print it modulo 109u2009+u20097. Input The first line of the input contains three positive integers _n_, _m_ and _q_ (1u2009β€u2009_n_Β·_m_u2009β€u200920,u20091u2009β€u2009_q_u2009β€u200910u2009000) β the number of rows and columns in the table and the number of events respectively. Next _q_ lines describe the events, each description contains four integers _tp__i_, _x__i_, _y__i_ and _t__i_ (1u2009β€u2009_tp_u2009β€u20093,u20091u2009β€u2009_x_u2009β€u2009_n_,u20091u2009β€u2009_y_u2009β€u2009_m_,u20092u2009β€u2009_t_u2009β€u2009109)xa0β the type of the event (1 if Gosha gets an invitation, 2 if a cat comes to the cell and 3 if a cat leaves the cell), the coordinates of the cell where the action takes place and the moment of time at which the action takes place respectively. It is guaranteed that the queries are given in the chronological order, i.e. _t__i_u2009<u2009_t__i_u2009+u20091. | 2,400 | false | false | false | true | false | false | false | false | false | false | 7,473 |
909F | Problem - 909F - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags constructive algorithms *2500 No tag edit access β Contest materials ") Editorial") . Output For each subtask, if the required permutation doesn't exist, output a single line containing the word "NO"; otherwise output the word "YES" in the first line and _N_ elements of the permutation, separated by spaces, in the second line. If there are several possible permutations in a subtask, output any of them. Examples Input 3 Output NO NO Input 6 Output YES 6 5 4 3 2 1 YES 3 6 2 5 1 4 | 2,500 | false | false | false | false | false | true | false | false | false | false | 6,107 |
1889C1 | The only differences between the two versions of this problem are the constraint on $$$k$$$, the time limit and the memory limit. You can make hacks only if all versions of the problem are solved. Doremy lives in a rainy country consisting of $$$n$$$ cities numbered from $$$1$$$ to $$$n$$$. The weather broadcast predicted the distribution of rain in the next $$$m$$$ days. In the $$$i$$$-th day, it will rain in the cities in the interval $$$[l_i, r_i]$$$. A city is called dry if it will never rain in that city in the next $$$m$$$ days. It turns out that Doremy has a special power. She can choose $$$k$$$ days (in the easy version, $$$k = 2$$$), and during these days it will not rain. Doremy wants to calculate the maximum number of dry cities after using the special power. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0β the number of test cases. The description of the test cases follows. The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1le nle 2cdot 10^5$$$, $$$2 le m le 2cdot 10^5$$$, $$$k = 2$$$)xa0β the number of cities, the number of days, and the number of days of rain that Doremy can prevent. Then, $$$m$$$ lines follow. The $$$i$$$-th line contains two integers $$$l_i$$$, $$$r_i$$$ ($$$1le l_ile r_ile n$$$)xa0β the rain coverage on day $$$i$$$. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2cdot 10^5$$$. Output For each test case, output one integerxa0β the maximum number of dry cities. Example Input 6 2 3 2 1 2 1 2 1 1 5 3 2 1 3 2 4 3 5 10 6 2 1 5 6 10 2 2 3 7 5 8 1 4 100 6 2 1 100 1 100 1 100 1 100 1 100 1 100 1000 2 2 1 1 1 1 20 5 2 9 20 3 3 10 11 11 13 6 18 Note In the first test case, if Doremy prevents rain $$$1,2$$$, then city $$$2$$$ will be dry; rain $$$2,3$$$, then no city will be dry; rain $$$1,3$$$, then no city will be dry; So there is at most $$$1$$$ dry city. In the second test case, if Doremy prevents rain $$$1,2$$$, then city $$$1,2$$$ will be dry; rain $$$2,3$$$, then city $$$4,5$$$ will be dry; rain $$$1,3$$$, then city $$$1,5$$$ will be dry. So there are at most $$$2$$$ dry cities. In the third test case, it is optimal to prevent rain $$$2,5$$$. In the forth test case, there is always $$$4$$$ days of rain that wets all the cities and cannot be prevented. | 2,000 | false | true | false | true | true | false | true | false | true | false | 964 |
1495C | There are many sunflowers in the Garden of the Sun. Garden of the Sun is a rectangular table with $$$n$$$ rows and $$$m$$$ columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners). Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals: When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected. There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells. You can walk from an empty cell to another if they share a common edge. Could you please give the owner a solution that meets all her requirements? Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0β the number of test cases. The description of the test cases follows. The first line contains two integers $$$n$$$, $$$m$$$ ($$$1 le n,m le 500$$$)xa0β the number of rows and columns. Each of the next $$$n$$$ lines contains $$$m$$$ characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively. It is guaranteed that the sum of $$$n cdot m$$$ for all test cases does not exceed $$$250,000$$$. Output For each test case, print $$$n$$$ lines. Each should contain $$$m$$$ characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively. If there are multiple answers, you can print any. It can be shown that the answer always exists. Example Input 5 3 3 X.X ... X.X 4 4 .... .X.X .... .X.X 5 5 .X... ....X .X... ..... X.X.X 1 10 ....X.X.X. 2 2 .. .. Output XXX ..X XXX XXXX .X.X .X.. .XXX .X... .XXXX .X... .X... XXXXX XXXXXXXXXX .. .. Note Let's use $$$(x,y)$$$ to describe the cell on $$$x$$$-th row and $$$y$$$-th column. In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively. In the first test case, one possible solution is to remove sunflowers on $$$(1,2)$$$, $$$(2,3)$$$ and $$$(3 ,2)$$$. Another acceptable solution is to remove sunflowers on $$$(1,2)$$$, $$$(2,2)$$$ and $$$(3,2)$$$. This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between $$$(1,1)$$$ and $$$(3,3)$$$. 1. $$$(1,1) o (1,2) o (1,3) o (2,3) o (3,3)$$$ 2. $$$(1,1) o (2,1) o (3,1) o (3,2) o (3,3)$$$ This output is considered wrong because you can't walk from $$$(1,1)$$$ to $$$(3,3)$$$. | 2,300 | false | false | false | false | false | true | false | false | false | true | 3,194 |
1721E | You are given a string $$$s$$$, consisting of lowercase Latin letters. You are asked $$$q$$$ queries about it: given another string $$$t$$$, consisting of lowercase Latin letters, perform the following steps: 1. concatenate $$$s$$$ and $$$t$$$; 2. calculate the prefix function of the resulting string $$$s+t$$$; 3. print the values of the prefix function on positions $$$s+1, s+2, dots, s+t$$$ ($$$s$$$ and $$$t$$$ denote the lengths of strings $$$s$$$ and $$$t$$$, respectively); 4. revert the string back to $$$s$$$. The prefix function of a string $$$a$$$ is a sequence $$$p_1, p_2, dots, p_{a}$$$, where $$$p_i$$$ is the maximum value of $$$k$$$ such that $$$k < i$$$ and $$$a[1..k]=a[i-k+1..i]$$$ ($$$a[l..r]$$$ denotes a contiguous substring of a string $$$a$$$ from a position $$$l$$$ to a position $$$r$$$, inclusive). In other words, it's the longest proper prefix of the string $$$a[1..i]$$$ that is equal to its suffix of the same length. Input The first line contains a non-empty string $$$s$$$ ($$$1 le s le 10^6$$$), consisting of lowercase Latin letters. The second line contains a single integer $$$q$$$ ($$$1 le q le 10^5$$$)xa0β the number of queries. Each of the next $$$q$$$ lines contains a query: a non-empty string $$$t$$$ ($$$1 le t le 10$$$), consisting of lowercase Latin letters. Output For each query, print the values of the prefix function of a string $$$s+t$$$ on positions $$$s+1, s+2, dots, s+t$$$. Examples Input aba 6 caba aba bababa aaaa b forces Output 0 1 2 3 1 2 3 2 3 4 5 6 7 1 1 1 1 2 0 0 0 0 0 0 Input aacba 4 aaca cbbb aab ccaca Output 2 2 3 1 0 0 0 0 2 2 0 0 0 1 0 1 | 2,200 | false | false | false | true | false | false | false | false | false | false | 1,955 |
65C | Brothers Fred and George Weasley once got into the sporting goods store and opened a box of Quidditch balls. After long and painful experiments they found out that the Golden Snitch is not enchanted at all. It is simply a programmed device. It always moves along the same trajectory, which is a polyline with vertices at the points (_x_0,u2009_y_0,u2009_z_0), (_x_1,u2009_y_1,u2009_z_1), ..., (_x__n_,u2009_y__n_,u2009_z__n_). At the beginning of the game the snitch is positioned at the point (_x_0,u2009_y_0,u2009_z_0), and then moves along the polyline at the constant speed _v__s_. The twins have not yet found out how the snitch behaves then. Nevertheless, they hope that the retrieved information will help Harry Potter and his team in the upcoming match against Slytherin. Harry Potter learned that at the beginning the game he will be at the point (_P__x_,u2009_P__y_,u2009_P__z_) and his super fast Nimbus 2011 broom allows him to move at the constant speed _v__p_ in any direction or remain idle. _v__p_ is not less than the speed of the snitch _v__s_. Harry Potter, of course, wants to catch the snitch as soon as possible. Or, if catching the snitch while it is moving along the polyline is impossible, he wants to hurry the Weasley brothers with their experiments. Harry Potter catches the snitch at the time when they are at the same point. Help Harry. Input The first line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u200910000). The following _n_u2009+u20091 lines contain the coordinates _x__i_, _y__i_, _z__i_, separated by single spaces. The coordinates of any two consecutive points do not coincide. The next line contains the velocities _v__p_ and _v__s_, the last line contains _P__x_, _P__y_, _P__z_, separated by single spaces. All the numbers in the input are integers, their absolute value does not exceed 104. The speeds are strictly positive. It is guaranteed that _v__s_u2009β€u2009_v__p_. Output If Harry Potter can catch the snitch while it is moving along the polyline (including the end (_x__n_,u2009_y__n_,u2009_z__n_)), print "YES" in the first line (without the quotes). Print in the second line _t_, which is the earliest moment of time, when Harry will be able to catch the snitch. On the third line print three numbers _X_, _Y_, _Z_, the coordinates of the point at which this happens. The absolute or relative error in the answer should not exceed 10u2009-u20096. If Harry is not able to catch the snitch during its moving along the described polyline, print "NO". Examples Input 4 0 0 0 0 10 0 10 10 0 10 0 0 0 0 0 1 1 5 5 25 Output YES 25.5000000000 10.0000000000 4.5000000000 0.0000000000 Input 4 0 0 0 0 10 0 10 10 0 10 0 0 0 0 0 1 1 5 5 50 Input 1 1 2 3 4 5 6 20 10 1 2 3 Output YES 0.0000000000 1.0000000000 2.0000000000 3.0000000000 | 2,100 | false | false | false | false | false | false | false | true | false | false | 9,644 |
1385C | You are given an array $$$a$$$ consisting of $$$n$$$ integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from $$$a$$$ to make it a good array. Recall that the prefix of the array $$$a=[a_1, a_2, dots, a_n]$$$ is a subarray consisting several first elements: the prefix of the array $$$a$$$ of length $$$k$$$ is the array $$$[a_1, a_2, dots, a_k]$$$ ($$$0 le k le n$$$). The array $$$b$$$ of length $$$m$$$ is called good, if you can obtain a non-decreasing array $$$c$$$ ($$$c_1 le c_2 le dots le c_{m}$$$) from it, repeating the following operation $$$m$$$ times (initially, $$$c$$$ is empty): select either the first or the last element of $$$b$$$, remove it from $$$b$$$, and append it to the end of the array $$$c$$$. For example, if we do $$$4$$$ operations: take $$$b_1$$$, then $$$b_{m}$$$, then $$$b_{m-1}$$$ and at last $$$b_2$$$, then $$$b$$$ becomes $$$[b_3, b_4, dots, b_{m-3}]$$$ and $$$c =[b_1, b_{m}, b_{m-1}, b_2]$$$. Consider the following example: $$$b = [1, 2, 3, 4, 4, 2, 1]$$$. This array is good because we can obtain non-decreasing array $$$c$$$ from it by the following sequence of operations: 1. take the first element of $$$b$$$, so $$$b = [2, 3, 4, 4, 2, 1]$$$, $$$c = [1]$$$; 2. take the last element of $$$b$$$, so $$$b = [2, 3, 4, 4, 2]$$$, $$$c = [1, 1]$$$; 3. take the last element of $$$b$$$, so $$$b = [2, 3, 4, 4]$$$, $$$c = [1, 1, 2]$$$; 4. take the first element of $$$b$$$, so $$$b = [3, 4, 4]$$$, $$$c = [1, 1, 2, 2]$$$; 5. take the first element of $$$b$$$, so $$$b = [4, 4]$$$, $$$c = [1, 1, 2, 2, 3]$$$; 6. take the last element of $$$b$$$, so $$$b = [4]$$$, $$$c = [1, 1, 2, 2, 3, 4]$$$; 7. take the only element of $$$b$$$, so $$$b = []$$$, $$$c = [1, 1, 2, 2, 3, 4, 4]$$$xa0β $$$c$$$ is non-decreasing. Note that the array consisting of one element is good. Print the length of the shortest prefix of $$$a$$$ to delete (erase), to make $$$a$$$ to be a good array. Note that the required length can be $$$0$$$. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) β the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 2 cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Note In the first test case of the example, the array $$$a$$$ is already good, so we don't need to erase any prefix. In the second test case of the example, the initial array $$$a$$$ is not good. Let's erase first $$$4$$$ elements of $$$a$$$, the result is $$$[4, 5, 2]$$$. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good. | 1,200 | false | true | false | false | false | false | false | false | false | false | 3,745 |
1989C | A movie company has released $$$2$$$ movies. These $$$2$$$ movies were watched by $$$n$$$ people. For each person, we know their attitude towards the first movie (liked it, neutral, or disliked it) and towards the second movie. If a person is asked to leave a review for the movie, then: if that person liked the movie, they will leave a positive review, and the movie's rating will increase by $$$1$$$; if that person disliked the movie, they will leave a negative review, and the movie's rating will decrease by $$$1$$$; otherwise, they will leave a neutral review, and the movie's rating will not change. Every person will review exactly one movie β and for every person, you can choose which movie they will review. The company's rating is the minimum of the ratings of the two movies. Your task is to calculate the maximum possible rating of the company. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-1 le a_i le 1$$$), where $$$a_i$$$ is equal to $$$-1$$$ if the first movie was disliked by the $$$i$$$-th viewer; equal to $$$1$$$ if the first movie was liked; and $$$0$$$ if the attitude is neutral. The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$-1 le b_i le 1$$$), where $$$b_i$$$ is equal to $$$-1$$$ if the second movie was disliked by the $$$i$$$-th viewer; equal to $$$1$$$ if the second movie was liked; and $$$0$$$ if the attitude is neutral. Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0β the maximum possible rating of the company, if for each person, choose which movie to leave a review on. Example Input 4 2 -1 1 -1 -1 1 -1 -1 5 0 -1 1 0 1 -1 1 0 0 1 4 -1 -1 -1 1 -1 1 1 1 | 1,400 | true | true | false | false | false | false | false | false | false | false | 339 |
1832E | Recall that the binomial coefficient $$$binom{x}{y}$$$ is calculated as follows ($$$x$$$ and $$$y$$$ are non-negative integers): if $$$x < y$$$, then $$$binom{x}{y} = 0$$$; otherwise, $$$binom{x}{y} = frac{x!}{y! cdot (x-y)!}$$$. You are given an array $$$a_1, a_2, dots, a_n$$$ and an integer $$$k$$$. You have to calculate a new array $$$b_1, b_2, dots, b_n$$$, where $$$b_1 = (binom{1}{k} cdot a_1) bmod 998244353$$$; $$$b_2 = (binom{2}{k} cdot a_1 + binom{1}{k} cdot a_2) bmod 998244353$$$; $$$b_3 = (binom{3}{k} cdot a_1 + binom{2}{k} cdot a_2 + binom{1}{k} cdot a_3) bmod 998244353$$$, and so on. Formally, $$$b_i = (sumlimits_{j=1}^{i} binom{i - j + 1}{k} cdot a_j) bmod 998244353$$$. Note that the array is given in a modified way, and you have to output it in a modified way as well. Input The only line of the input contains six integers $$$n$$$, $$$a_1$$$, $$$x$$$, $$$y$$$, $$$m$$$ and $$$k$$$ ($$$1 le n le 10^7$$$; $$$0 le a_1, x, y < m$$$; $$$2 le m le 998244353$$$; $$$1 le k le 5$$$). The array $$$[a_1, a_2, dots, a_n]$$$ is generated as follows: $$$a_1$$$ is given in the input; for $$$2 le i le n$$$, $$$a_i = (a_{i-1} cdot x + y) bmod m$$$. Output Since outputting up to $$$10^7$$$ integers might be too slow, you have to do the following: Let $$$c_i = b_i cdot i$$$ (without taking modulo $$$998244353$$$ after the multiplication). Print the integer $$$c_1 oplus c_2 oplus dots oplus c_n$$$, where $$$oplus$$$ denotes the bitwise XOR operator. | 2,200 | false | false | false | true | false | false | true | false | false | false | 1,293 |
774B | Stepan is a very experienced olympiad participant. He has _n_ cups for Physics olympiads and _m_ cups for Informatics olympiads. Each cup is characterized by two parameters β its significance _c__i_ and width _w__i_. Stepan decided to expose some of his cups on a shelf with width _d_ in such a way, that: there is at least one Physics cup and at least one Informatics cup on the shelf, the total width of the exposed cups does not exceed _d_, from each subjects (Physics and Informatics) some of the most significant cups are exposed (i. e. if a cup for some subject with significance _x_ is exposed, then all the cups for this subject with significance greater than _x_ must be exposed too). Your task is to determine the maximum possible total significance, which Stepan can get when he exposes cups on the shelf with width _d_, considering all the rules described above. The total significance is the sum of significances of all the exposed cups. Input The first line contains three integers _n_, _m_ and _d_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009100u2009000, 1u2009β€u2009_d_u2009β€u2009109) β the number of cups for Physics olympiads, the number of cups for Informatics olympiads and the width of the shelf. Each of the following _n_ lines contains two integers _c__i_ and _w__i_ (1u2009β€u2009_c__i_,u2009_w__i_u2009β€u2009109) β significance and width of the _i_-th cup for Physics olympiads. Each of the following _m_ lines contains two integers _c__j_ and _w__j_ (1u2009β€u2009_c__j_,u2009_w__j_u2009β€u2009109) β significance and width of the _j_-th cup for Informatics olympiads. Output Print the maximum possible total significance, which Stepan can get exposing cups on the shelf with width _d_, considering all the rules described in the statement. If there is no way to expose cups on the shelf, then print 0. Examples Input 4 3 12 3 4 2 4 3 5 3 4 3 5 5 2 3 4 Note In the first example Stepan has only one Informatics cup which must be exposed on the shelf. Its significance equals 3 and width equals 2, so after Stepan exposes it, the width of free space on the shelf becomes equal to 6. Also, Stepan must expose the second Physics cup (which has width 5), because it is the most significant cup for Physics (its significance equals 5). After that Stepan can not expose more cups on the shelf, because there is no enough free space. Thus, the maximum total significance of exposed cups equals to 8. | 2,100 | false | false | false | false | true | false | false | true | false | false | 6,680 |
1047B | Problem - 1047B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags geometry math *900 No tag edit access β Contest materials , (x_2,y_2), ldots, (x_n,y_n)$$$. You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle. Input First line contains one integer $$$n$$$ ($$$1 leq n leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 leq x_i,y_i leq 10^9$$$). Output Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer. Examples Input 3 1 1 1 2 2 1 Output 3 Input 4 1 1 1 2 2 1 2 2 Output 4 Note Illustration for the first example: Illustration for the second example: | 900 | true | false | false | false | false | false | false | false | false | false | 5,456 |
1846D | Rudolph drew a beautiful Christmas tree and decided to print the picture. However, the ink in the cartridge often runs out at the most inconvenient moment. Therefore, Rudolph wants to calculate in advance how much green ink he will need. The tree is a vertical trunk with identical triangular branches at different heights. The thickness of the trunk is negligible. Each branch is an isosceles triangle with base $$$d$$$ and height $$$h$$$, whose base is perpendicular to the trunk. The triangles are arranged upward at an angle, and the trunk passes exactly in the middle. The base of the $$$i$$$-th triangle is located at a height of $$$y_i$$$. The figure below shows an example of a tree with $$$d = 4, h = 2$$$ and three branches with bases at heights $$$[1, 4, 5]$$$. Help Rudolph calculate the total area of the tree branches. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. Then follow the descriptions of the test cases. The first line of each test case contains three integers $$$n, d, h$$$ ($$$1 le n, d, h le 2 cdot 10^5$$$)xa0β the number of branches, the length of the base, and the height of the branches, respectively. The second line of each test case contains $$$n$$$ integers $$$y_i$$$ $$$(1 le y_i le 10^9, y_1 < y_2 < ... < y_n)$$$ β the heights of the bases of the branches. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single real number on a separate line β the total area of the tree branches. The answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Example Input 5 3 4 2 1 4 5 1 5 1 3 4 6 6 1 2 3 4 2 1 200000 1 200000 2 4 3 9 11 Output 11 2.5 34.5 199999.9999975 11.333333 | 1,200 | true | false | false | false | false | true | false | false | false | false | 1,205 |
235C | Some days ago, WJMZBMR learned how to answer the query "how many times does a string _x_ occur in a string _s_" quickly by preprocessing the string _s_. But now he wants to make it harder. So he wants to ask "how many consecutive substrings of _s_ are cyclical isomorphic to a given string _x_". You are given string _s_ and _n_ strings _x__i_, for each string _x__i_ find, how many consecutive substrings of _s_ are cyclical isomorphic to _x__i_. Two strings are called cyclical isomorphic if one can rotate one string to get the other one. 'Rotate' here means 'to take some consecutive chars (maybe none) from the beginning of a string and put them back at the end of the string in the same order'. For example, string "abcde" can be rotated to string "deabc". We can take characters "abc" from the beginning and put them at the end of "de". Input The first line contains a non-empty string _s_. The length of string _s_ is not greater than 106 characters. The second line contains an integer _n_ (1u2009β€u2009_n_u2009β€u2009105) β the number of queries. Then _n_ lines follow: the _i_-th line contains the string _x__i_ β the string for the _i_-th query. The total length of _x__i_ is less than or equal to 106 characters. In this problem, strings only consist of lowercase English letters. Output For each query _x__i_ print a single integer that shows how many consecutive substrings of _s_ are cyclical isomorphic to _x__i_. Print the answers to the queries in the order they are given in the input. Examples Input baabaabaaa 5 a ba baa aabaa aaba | 2,700 | false | false | false | false | true | false | false | false | false | false | 8,894 |
1185A | Polycarp decided to relax on his weekend and visited to the performance of famous ropewalkers: Agafon, Boniface and Konrad. The rope is straight and infinite in both directions. At the beginning of the performance, Agafon, Boniface and Konrad are located in positions $$$a$$$, $$$b$$$ and $$$c$$$ respectively. At the end of the performance, the distance between each pair of ropewalkers was at least $$$d$$$. Ropewalkers can walk on the rope. In one second, only one ropewalker can change his position. Every ropewalker can change his position exactly by $$$1$$$ (i. e. shift by $$$1$$$ to the left or right direction on the rope). Agafon, Boniface and Konrad can not move at the same time (Only one of them can move at each moment). Ropewalkers can be at the same positions at the same time and can "walk past each other". You should find the minimum duration (in seconds) of the performance. In other words, find the minimum number of seconds needed so that the distance between each pair of ropewalkers can be greater or equal to $$$d$$$. Ropewalkers can walk to negative coordinates, due to the rope is infinite to both sides. Input The only line of the input contains four integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$1 le a, b, c, d le 10^9$$$). It is possible that any two (or all three) ropewalkers are in the same position at the beginning of the performance. Output Output one integer β the minimum duration (in seconds) of the performance. Note In the first example: in the first two seconds Konrad moves for 2 positions to the right (to the position $$$8$$$), while Agafon and Boniface stay at their positions. Thus, the distance between Agafon and Boniface will be $$$5 - 2 = 3$$$, the distance between Boniface and Konrad will be $$$2 - 8 = 6$$$ and the distance between Agafon and Konrad will be $$$5 - 8 = 3$$$. Therefore, all three pairwise distances will be at least $$$d=3$$$, so the performance could be finished within 2 seconds. | 800 | true | false | false | false | false | false | false | false | false | false | 4,780 |
1294E | You are given a rectangular matrix of size $$$n imes m$$$ consisting of integers from $$$1$$$ to $$$2 cdot 10^5$$$. In one move, you can: choose any element of the matrix and change its value to any integer between $$$1$$$ and $$$n cdot m$$$, inclusive; take any column and shift it one cell up cyclically (see the example of such cyclic shift below). A cyclic shift is an operation such that you choose some $$$j$$$ ($$$1 le j le m$$$) and set $$$a_{1, j} := a_{2, j}, a_{2, j} := a_{3, j}, dots, a_{n, j} := a_{1, j}$$$ simultaneously. Example of cyclic shift of the first column You want to perform the minimum number of moves to make this matrix look like this: In other words, the goal is to obtain the matrix, where $$$a_{1, 1} = 1, a_{1, 2} = 2, dots, a_{1, m} = m, a_{2, 1} = m + 1, a_{2, 2} = m + 2, dots, a_{n, m} = n cdot m$$$ (i.e. $$$a_{i, j} = (i - 1) cdot m + j$$$) with the minimum number of moves performed. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5, n cdot m le 2 cdot 10^5$$$) β the size of the matrix. The next $$$n$$$ lines contain $$$m$$$ integers each. The number at the line $$$i$$$ and position $$$j$$$ is $$$a_{i, j}$$$ ($$$1 le a_{i, j} le 2 cdot 10^5$$$). Output Print one integer β the minimum number of moves required to obtain the matrix, where $$$a_{1, 1} = 1, a_{1, 2} = 2, dots, a_{1, m} = m, a_{2, 1} = m + 1, a_{2, 2} = m + 2, dots, a_{n, m} = n cdot m$$$ ($$$a_{i, j} = (i - 1)m + j$$$). Examples Input 3 3 3 2 1 1 2 3 4 5 6 Input 4 3 1 2 3 4 5 6 7 8 9 10 11 12 Input 3 4 1 6 3 4 5 10 7 8 9 2 11 12 Note In the first example, you can set $$$a_{1, 1} := 7, a_{1, 2} := 8$$$ and $$$a_{1, 3} := 9$$$ then shift the first, the second and the third columns cyclically, so the answer is $$$6$$$. It can be shown that you cannot achieve a better answer. In the second example, the matrix is already good so the answer is $$$0$$$. In the third example, it is enough to shift the second column cyclically twice to obtain a good matrix, so the answer is $$$2$$$. | 1,900 | true | true | true | false | false | false | false | false | false | false | 4,225 |
2005B2 | This is the hard version of the problem. The only differences between the two versions are the constraints on $$$m$$$ and $$$q$$$. In this version, $$$m, q le 10^5$$$. You can make hacks only if both versions of the problem are solved. Narek and Tsovak were busy preparing this round, so they have not managed to do their homework and decided to steal David's homework. Their strict teacher noticed that David has no homework and now wants to punish him. She hires other teachers to help her catch David. And now $$$m$$$ teachers together are chasing him. Luckily, the classroom is big, so David has many places to hide. The classroom can be represented as a one-dimensional line with cells from $$$1$$$ to $$$n$$$, inclusive. At the start, all $$$m$$$ teachers and David are in distinct cells. Then they make moves. During each move David goes to an adjacent cell or stays at the current one. Then, each of the $$$m$$$ teachers simultaneously goes to an adjacent cell or stays at the current one. This continues until David is caught. David is caught if any of the teachers (possibly more than one) is located in the same cell as David. Everyone sees others' moves, so they all act optimally. Your task is to find how many moves it will take for the teachers to catch David if they all act optimally. Acting optimally means the student makes his moves in a way that maximizes the number of moves the teachers need to catch him; and the teachers coordinate with each other to make their moves in a way that minimizes the number of moves they need to catch the student. Also, as Narek and Tsovak think this task is easy, they decided to give you $$$q$$$ queries on David's position. Input In the first line of the input, you are given a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0β the number of test cases. The description of each test case follows. In the first line of each test case, you are given three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$3 le n le 10^9$$$, $$$1 le m, q le 10^5$$$)xa0β the number of cells on the line, the number of teachers, and the number of queries. In the second line of each test case, you are given $$$m$$$ distinct integers $$$b_1, b_2, ldots, b_m$$$ ($$$1 le b_i le n$$$)xa0β the cell numbers of the teachers. In the third line of each test case, you are given $$$q$$$ integers $$$a_1, a_2, ldots, a_q$$$ ($$$1 le a_i le n$$$)xa0β David's cell number for every query. It is guaranteed that for any $$$i$$$, $$$j$$$ such that $$$1 le i le m$$$ and $$$1 le j le q$$$, $$$b_i eq a_j$$$. It is guaranteed that the sum of values of $$$m$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. It is guaranteed that the sum of values of $$$q$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output $$$q$$$ lines, the $$$i$$$-th of them containing the answer of the $$$i$$$-th query. Example Input 2 8 1 1 6 3 10 3 3 1 4 8 2 3 10 Note In the only query of the first example, the student can run to cell $$$1$$$. It will take the teacher five moves to reach from cell $$$6$$$ to cell $$$1$$$, so the answer is $$$5$$$. In the second query of the second example, the student can just stay at cell $$$3$$$. The teacher, initially located in cell $$$4$$$, can reach cell $$$3$$$ in one move. Therefore, the answer is $$$1$$$. | 1,200 | true | true | false | false | false | false | false | true | true | false | 222 |
1579E1 | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 744 (Div. 3) Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags constructive algorithms greedy math *1000 No tag edit access β Contest materials Announcement (ru) Tutorial PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST E1. Permutation Minimization by Deque time limit per test2 seconds memory limit per test256 megabytes In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. A permutation $$$p$$$ of size $$$n$$$ is given. A permutation of size $$$n$$$ is an array of size $$$n$$$ in which each integer from $$$1$$$ to $$$n$$$ occurs exactly once. For example, $$$[1, 4, 3, 2]$$$ and $$$[4, 2, 1, 3]$$$ are correct permutations while $$$[1, 2, 4]$$$ and $$$[1, 2, 2]$$$ are not. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $$$[1, 5, 2]$$$ currently in the deque, adding an element $$$4$$$ to the beginning will produce the sequence $$$[color{red}{4}, 1, 5, 2]$$$, and adding same element to the end will produce $$$[1, 5, 2, color{red}{4}]$$$. The elements of the permutation are sequentially added to the initially empty deque, starting with $$$p_1$$$ and finishing with $$$p_n$$$. Before adding each element to the deque, you may choose whether to add it to the beginning or the end. For example, if we consider a permutation $$$p = [3, 1, 2, 4]$$$, one of the possible sequences of actions looks like this: $$$quad$$$ 1. add $$$3$$$ to the end of the deque: deque has a sequence $$$[color{red}{3}]$$$ in it; $$$quad$$$ 2. add $$$1$$$ to the beginning of the deque: deque has a sequence $$$[color{red}{1}, 3]$$$ in it; $$$quad$$$ 3. add $$$2$$$ to the end of the deque: deque has a sequence $$$[1, 3, color{red}{2}]$$$ in it; $$$quad$$$ 4. add $$$4$$$ to the end of the deque: deque has a sequence $$$[1, 3, 2, color{red}{4}]$$$ in it; Find the lexicographically smallest possible sequence of elements in the deque after the entire permutation has been processed. A sequence $$$[x_1, x_2, ldots, x_n]$$$ is lexicographically smaller than the sequence $$$[y_1, y_2, ldots, y_n]$$$ if there exists such $$$i leq n$$$ that $$$x_1 = y_1$$$, $$$x_2 = y_2$$$, $$$ldots$$$, $$$x_{i - 1} = y_{i - 1}$$$ and $$$x_i < y_i$$$. In other words, if the sequences $$$x$$$ and $$$y$$$ have some (possibly empty) matching prefix, and the next element of the sequence $$$x$$$ is strictly smaller than the corresponding element of the sequence $$$y$$$. For example, the sequence $$$[1, 3, 2, 4]$$$ is smaller than the sequence $$$[1, 3, 4, 2]$$$ because after the two matching elements $$$[1, 3]$$$ in the start the first sequence has an element $$$2$$$ which is smaller than the corresponding element $$$4$$$ in the second sequence. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0β the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β permutation size. The second line of the description contains $$$n$$$ space-separated integers $$$p_i$$$ ($$$1 le p_i le n$$$; all $$$p_i$$$ are all unique)xa0β elements of the permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to a test case should contain $$$n$$$ space-separated integer numbersxa0β the elements of the lexicographically smallest permutation that is possible to find in the deque after executing the described algorithm. Example input 5 4 3 1 2 4 3 3 2 1 3 3 1 2 2 1 2 2 2 1 output 1 3 2 4 1 2 3 1 3 2 1 2 1 2 Note One of the ways to get a lexicographically smallest permutation $$$[1, 3, 2, 4]$$$ from the permutation $$$[3, 1, 2, 4]$$$ (the first sample test case) is described in the problem statement. Codeforces (c) | 1,000 | true | true | false | false | false | true | false | false | false | false | 2,737 |
1906L | Construct a parentheses sequence consisting of $$$N$$$ characters such that it is balanced and the length of its longest palindromic subsequence (LPS) is exactly $$$K$$$. Determine whether such a construction is possible. If there are several possible sequences, construct any of them. A parentheses sequence consists of only character ( and ). A parentheses sequence is balanced if each character ( has a corresponding character ) and the pairs of parentheses are properly nested. For example, (), (()), (())(), and ((())()) are balanced. However, )(, ((), and ()) are not balanced. A sequence is palindromic if it reads the same backwards as forwards. For example, ((, ), ())(, and (()(( are palindromic. However, (), )(, and (()) are not palindromic. A subsequence can be derived from another sequence by removing zero or more characters without changing the order of the remaining characters. For example, (, ))), ())(, and (())() are subsequence of (())(). However, )(( and ((())) are not subsequence of (())(). The longest palindromic subsequence (LPS) of a sequence is a subsequence with the maximum number of characters, derived from that sequence and it is palindromic. For example, the LPS of sequence (())() is ())(, which can be obtained by removing the second and sixth characters. Therefore, the length of the LPS of (())() is $$$4$$$. Output If there is no such parentheses sequence such that it is balanced and the length of its LPS is exactly $$$K$$$, then output -1. Otherwise, output a string of $$$N$$$ characters, representing the parentheses sequence. If there are several possible answers, output any of them. Note Explanation for the sample input/output #2 The LPS of (()()) is either ((( by removing all ) characters, or ))) by removing all ( characters. The output ((())) also satisfies the requirements. Explanation for the sample input/output #3 The only possible balanced parentheses sequences are (()) and ()(). The length of the LPS of (()) and ()() are $$$2$$$ and $$$3$$$, respectively. Explanation for the sample input/output #4 The LPS of ()((())()())() is )())()())(), which can be obtained by removing the first, fourth, and fifth characters. | 2,500 | false | false | false | false | false | true | false | false | false | false | 866 |
201B | A widely known among some people Belarusian sport programmer Yura possesses lots of information about cars. That is why he has been invited to participate in a game show called "Guess That Car!". The game show takes place on a giant parking lot, which is 4_n_ meters long from north to south and 4_m_ meters wide from west to east. The lot has _n_u2009+u20091 dividing lines drawn from west to east and _m_u2009+u20091 dividing lines drawn from north to south, which divide the parking lot into _n_Β·_m_ 4 by 4 meter squares. There is a car parked strictly inside each square. The dividing lines are numbered from 0 to _n_ from north to south and from 0 to _m_ from west to east. Each square has coordinates (_i_,u2009_j_) so that the square in the north-west corner has coordinates (1,u20091) and the square in the south-east corner has coordinates (_n_,u2009_m_). See the picture in the notes for clarifications. Before the game show the organizers offer Yura to occupy any of the (_n_u2009+u20091)Β·(_m_u2009+u20091) intersection points of the dividing lines. After that he can start guessing the cars. After Yura chooses a point, he will be prohibited to move along the parking lot before the end of the game show. As Yura is a car expert, he will always guess all cars he is offered, it's just a matter of time. Yura knows that to guess each car he needs to spend time equal to the square of the euclidean distance between his point and the center of the square with this car, multiplied by some coefficient characterizing the machine's "rarity" (the rarer the car is, the harder it is to guess it). More formally, guessing a car with "rarity" _c_ placed in a square whose center is at distance _d_ from Yura takes _c_Β·_d_2 seconds. The time Yura spends on turning his head can be neglected. It just so happened that Yura knows the "rarity" of each car on the parking lot in advance. Help him choose his point so that the total time of guessing all cars is the smallest possible. Input The first line contains two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u20091000) β the sizes of the parking lot. Each of the next _n_ lines contains _m_ integers: the _j_-th number in the _i_-th line describes the "rarity" _c__ij_ (0u2009β€u2009_c__ij_u2009β€u2009100000) of the car that is located in the square with coordinates (_i_,u2009_j_). Output In the first line print the minimum total time Yura needs to guess all offered cars. In the second line print two numbers _l__i_ and _l__j_ (0u2009β€u2009_l__i_u2009β€u2009_n_,u20090u2009β€u2009_l__j_u2009β€u2009_m_) β the numbers of dividing lines that form a junction that Yura should choose to stand on at the beginning of the game show. If there are multiple optimal starting points, print the point with smaller _l__i_. If there are still multiple such points, print the point with smaller _l__j_. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 4 1 0 0 0 0 0 3 0 0 0 5 5 Note In the first test case the total time of guessing all cars is equal to 3Β·8u2009+u20093Β·8u2009+u20094Β·8u2009+u20099Β·8u2009+u20095Β·40u2009+u20091Β·40u2009=u2009392. The coordinate system of the field: | 1,800 | true | false | false | false | false | false | false | false | false | false | 9,038 |
1916H1 | Problem - 1916H1 - Codeforces =============== xa0 time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output This is the easy version of the problem. The only differences between the two versions of this problem are the constraints on $$$k$$$. You can make hacks only if all versions of the problem are solved. You are given integers $$$n$$$, $$$p$$$ and $$$k$$$. $$$p$$$ is guaranteed to be a prime number. For each $$$r$$$ from $$$0$$$ to $$$k$$$, find the number of $$$n imes n$$$ matrices $$$A$$$ of the field$$$^dagger$$$ of integers modulo $$$p$$$ such that the rank$$$^ddagger$$$ of $$$A$$$ is exactly $$$r$$$. Since these values are big, you are only required to output them modulo $$$998,244,353$$$. $$$^dagger$$$ | 2,700 | true | false | false | true | false | false | true | false | false | false | 796 |
125A | Problem - 125A - Codeforces =============== xa0 . Output Print two non-negative space-separated integers _a_ and _b_, where _a_ is the numbers of feet and _b_ is the number of inches. Examples Input 42 Output 1 2 Input 5 Output 0 2 | 1,400 | true | false | false | false | false | false | false | false | false | false | 9,382 |
1525E | Monocarp is playing a game "Assimilation IV". In this game he manages a great empire: builds cities and conquers new lands. Monocarp's empire has $$$n$$$ cities. In order to conquer new lands he plans to build one Monument in each city. The game is turn-based and, since Monocarp is still amateur, he builds exactly one Monument per turn. Monocarp has $$$m$$$ points on the map he'd like to control using the constructed Monuments. For each point he knows the distance between it and each city. Monuments work in the following way: when built in some city, a Monument controls all points at distance at most $$$1$$$ to this city. Next turn, the Monument controls all points at distance at most $$$2$$$, the turn afterxa0β at distance at most $$$3$$$, and so on. Monocarp will build $$$n$$$ Monuments in $$$n$$$ turns and his empire will conquer all points that are controlled by at least one Monument. Monocarp can't figure out any strategy, so during each turn he will choose a city for a Monument randomly among all remaining cities (cities without Monuments). Monocarp wants to know how many points (among $$$m$$$ of them) he will conquer at the end of turn number $$$n$$$. Help him to calculate the expected number of conquered points! Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 20$$$; $$$1 le m le 5 cdot 10^4$$$)xa0β the number of cities and the number of points. Next $$$n$$$ lines contains $$$m$$$ integers each: the $$$j$$$-th integer of the $$$i$$$-th line $$$d_{i, j}$$$ ($$$1 le d_{i, j} le n + 1$$$) is the distance between the $$$i$$$-th city and the $$$j$$$-th point. Output It can be shown that the expected number of points Monocarp conquers at the end of the $$$n$$$-th turn can be represented as an irreducible fraction $$$frac{x}{y}$$$. Print this fraction modulo $$$998,244,353$$$, i.xa0e. value $$$x cdot y^{-1} bmod 998244353$$$ where $$$y^{-1}$$$ is such number that $$$y cdot y^{-1} bmod 998244353 = 1$$$. Example Input 3 5 1 4 4 3 4 1 4 1 4 2 1 4 4 4 3 Note Let's look at all possible orders of cities Monuments will be build in: $$$[1, 2, 3]$$$: the first city controls all points at distance at most $$$3$$$, in other words, points $$$1$$$ and $$$4$$$; the second city controls all points at distance at most $$$2$$$, or points $$$1$$$, $$$3$$$ and $$$5$$$; the third city controls all points at distance at most $$$1$$$, or point $$$1$$$.In total, $$$4$$$ points are controlled. $$$[1, 3, 2]$$$: the first city controls points $$$1$$$ and $$$4$$$; the second cityxa0β points $$$1$$$ and $$$3$$$; the third cityxa0β point $$$1$$$. In total, $$$3$$$ points. $$$[2, 1, 3]$$$: the first city controls point $$$1$$$; the second cityxa0β points $$$1$$$, $$$3$$$ and $$$5$$$; the third cityxa0β point $$$1$$$. In total, $$$3$$$ points. $$$[2, 3, 1]$$$: the first city controls point $$$1$$$; the second cityxa0β points $$$1$$$, $$$3$$$ and $$$5$$$; the third cityxa0β point $$$1$$$. In total, $$$3$$$ points. $$$[3, 1, 2]$$$: the first city controls point $$$1$$$; the second cityxa0β points $$$1$$$ and $$$3$$$; the third cityxa0β points $$$1$$$ and $$$5$$$. In total, $$$3$$$ points. $$$[3, 2, 1]$$$: the first city controls point $$$1$$$; the second cityxa0β points $$$1$$$, $$$3$$$ and $$$5$$$; the third cityxa0β points $$$1$$$ and $$$5$$$. In total, $$$3$$$ points. The expected number of controlled points is $$$frac{4 + 3 + 3 + 3 + 3 + 3}{6}$$$ $$$=$$$ $$$frac{19}{6}$$$ or $$$19 cdot 6^{-1}$$$ $$$equiv$$$ $$$19 cdot 166374059$$$ $$$equiv$$$ $$$166374062$$$ $$$pmod{998244353}$$$ | 2,100 | true | false | false | true | false | false | false | false | false | false | 3,038 |
1840C | Dima Vatrushin is a math teacher at school. He was sent on vacation for $$$n$$$ days for his good work. Dima has long dreamed of going to a ski resort, so he wants to allocate several consecutive days and go skiing. Since the vacation requires careful preparation, he will only go for at least $$$k$$$ days. You are given an array $$$a$$$ containing the weather forecast at the resort. That is, on the $$$i$$$-th day, the temperature will be $$$a_i$$$ degrees. Dima was born in Siberia, so he can go on vacation only if the temperature does not rise above $$$q$$$ degrees throughout the vacation. Unfortunately, Dima was so absorbed in abstract algebra that he forgot how to count. He asks you to help him and count the number of ways to choose vacation dates at the resort. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. Then follow the descriptions of the test cases. The first line of each test case contains three integers $$$n$$$, $$$k$$$, $$$q$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le k le n$$$, $$$-10^9 le q le 10^9$$$)xa0β the length of the array $$$a$$$, the minimum number of days at the resort, and the maximum comfortable temperature for Dima. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, a_3, dots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$)xa0β the temperature at the ski resort. The sum of all $$$n$$$ values over all test cases does not exceed $$$2 cdot 10^5$$$. Output Output $$$t$$$ integers, each of which is the answer to the corresponding test casexa0β the number of ways for Dima to choose vacation dates at the resort. Example Input 7 3 1 15 -5 0 -10 5 3 -33 8 12 9 0 5 4 3 12 12 12 10 15 4 1 -5 0 -1 2 5 5 5 0 3 -1 4 -5 -3 1 1 5 5 6 1 3 0 3 -2 5 -4 -4 Note In the first test case of the example, Dima can go on any day, so the suitable dates for him are [1], [2], [3], [1, 2], [2, 3], [1, 2, 3]. In the second and fourth test cases of the example, Dima cannot go on any day due to the high temperature, so there are no suitable dates. In the third test case of the example, Dima can only go on the dates [1, 2, 3]. | 1,000 | true | false | false | false | false | false | false | false | false | false | 1,251 |
1517G | At the foot of Liyushan Mountain, $$$n$$$ tents will be carefully arranged to provide accommodation for those who are willing to experience the joy of approaching nature, the tranquility of the night, and the bright starry sky. The $$$i$$$-th tent is located at the point of $$$(x_i, y_i)$$$ and has a weight of $$$w_i$$$. A tent is important if and only if both $$$x_i$$$ and $$$y_i$$$ are even. You need to remove some tents such that for each remaining important tent $$$(x, y)$$$, there do not exist $$$3$$$ other tents $$$(x'_1, y'_1)$$$, $$$(x'_2, y'_2)$$$ and $$$(x'_3, y'_3)$$$ such that both conditions are true: 1. $$$x'_j-x, y'_j - yleq 1$$$ for all $$$j in {1, 2, 3}$$$, and 2. these four tents form a parallelogram (or a rectangle) and one of its sides is parallel to the $$$x$$$-axis. Please maximize the sum of the weights of the tents that are not removed. Print the maximum value. Input The first line contains a single integer $$$n$$$ ($$$1leq nleq 1,000$$$), representing the number of tents. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$ and $$$w_i$$$ ($$$-10^9leq x_i,y_i leq 10^9$$$, $$$1leq w_ileq 10^9$$$), representing the coordinate of the $$$i$$$-th tent and its weight. No two tents are located at the same point. Output A single integer β the maximum sum of the weights of the remaining tents. Examples Input 5 0 0 4 0 1 5 1 0 3 1 1 1 -1 1 2 Input 32 2 2 1 2 3 1 3 2 1 3 3 1 2 6 1 2 5 1 3 6 1 3 5 1 2 8 1 2 9 1 1 8 1 1 9 1 2 12 1 2 11 1 1 12 1 1 11 1 6 2 1 7 2 1 6 3 1 5 3 1 6 6 1 7 6 1 5 5 1 6 5 1 6 8 1 5 8 1 6 9 1 7 9 1 6 12 1 5 12 1 6 11 1 7 11 1 Note Here is an illustration of the second example. Black triangles indicate the important tents. This example also indicates all $$$8$$$ forbidden patterns. | 3,300 | false | false | false | false | false | true | false | false | false | true | 3,071 |
1108D | You have a garland consisting of $$$n$$$ lamps. Each lamp is colored red, green or blue. The color of the $$$i$$$-th lamp is $$$s_i$$$ ('R', 'G' and 'B' β colors of lamps in the garland). You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse. A garland is called diverse if any two adjacent (consecutive) lamps (i.u2009e. such lamps that the distance between their positions is $$$1$$$) have distinct colors. In other words, if the obtained garland is $$$t$$$ then for each $$$i$$$ from $$$1$$$ to $$$n-1$$$ the condition $$$t_i e t_{i + 1}$$$ should be satisfied. Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β the number of lamps. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ characters 'R', 'G' and 'B' β colors of lamps in the garland. Output In the first line of the output print one integer $$$r$$$ β the minimum number of recolors needed to obtain a diverse garland from the given one. In the second line of the output print one string $$$t$$$ of length $$$n$$$ β a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them. | 1,400 | false | true | false | true | false | true | false | false | false | false | 5,159 |
1725L | Pak Chanek is participating in a lemper cooking competition. In the competition, Pak Chanek has to cook lempers with $$$N$$$ stoves that are arranged sequentially from stove $$$1$$$ to stove $$$N$$$. Initially, stove $$$i$$$ has a temperature of $$$A_i$$$ degrees. A stove can have a negative temperature. Pak Chanek realises that, in order for his lempers to be cooked, he needs to keep the temperature of each stove at a non-negative value. To make it happen, Pak Chanek can do zero or more operations. In one operation, Pak Chanek chooses one stove $$$i$$$ with $$$2 leq i leq N-1$$$, then: 1. changes the temperature of stove $$$i-1$$$ into $$$A_{i-1} := A_{i-1} + A_{i}$$$, 2. changes the temperature of stove $$$i+1$$$ into $$$A_{i+1} := A_{i+1} + A_{i}$$$, and 3. changes the temperature of stove $$$i$$$ into $$$A_i := -A_i$$$. Pak Chanek wants to know the minimum number of operations he needs to do such that the temperatures of all stoves are at non-negative values. Help Pak Chanek by telling him the minimum number of operations needed or by reporting if it is not possible to do. Input The first line contains a single integer $$$N$$$ ($$$1 le N le 10^5$$$) β the number of stoves. The second line contains $$$N$$$ integers $$$A_1, A_2, ldots, A_N$$$ ($$$-10^9 leq A_i leq 10^9$$$) β the initial temperatures of the stoves. Output Output an integer representing the minimum number of operations needed to make the temperatures of all stoves at non-negative values or output $$$-1$$$ if it is not possible. Note For the first example, a sequence of operations that can be done is as follows: Pak Chanek does an operation to stove $$$3$$$, $$$A = [2, -2, 1, 4, 2, -2, 9]$$$. Pak Chanek does an operation to stove $$$2$$$, $$$A = [0, 2, -1, 4, 2, -2, 9]$$$. Pak Chanek does an operation to stove $$$3$$$, $$$A = [0, 1, 1, 3, 2, -2, 9]$$$. Pak Chanek does an operation to stove $$$6$$$, $$$A = [0, 1, 1, 3, 0, 2, 7]$$$. There is no other sequence of operations such that the number of operations needed is fewer than $$$4$$$. | 2,400 | false | false | false | false | true | false | false | false | false | false | 1,935 |
677A | Vanya and his friends are walking along the fence of height _h_ and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed _h_. If the height of some person is greater than _h_ he can bend down and then he surely won't be noticed by the guard. The height of the _i_-th person is equal to _a__i_. Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard? Input The first line of the input contains two integers _n_ and _h_ (1u2009β€u2009_n_u2009β€u20091000, 1u2009β€u2009_h_u2009β€u20091000)xa0β the number of friends and the height of the fence, respectively. The second line contains _n_ integers _a__i_ (1u2009β€u2009_a__i_u2009β€u20092_h_), the _i_-th of them is equal to the height of the _i_-th person. Output Print a single integerxa0β the minimum possible valid width of the road. Note In the first sample, only person number 3 must bend down, so the required width is equal to 1u2009+u20091u2009+u20092u2009=u20094. In the second sample, all friends are short enough and no one has to bend, so the width 1u2009+u20091u2009+u20091u2009+u20091u2009+u20091u2009+u20091u2009=u20096 is enough. In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2u2009+u20092u2009+u20092u2009+u20092u2009+u20092u2009+u20091u2009=u200911. | 800 | false | false | true | false | false | false | false | false | false | false | 7,121 |
1494B | Berland crossword is a puzzle that is solved on a square grid with $$$n$$$ rows and $$$n$$$ columns. Initially all the cells are white. To solve the puzzle one has to color some cells on the border of the grid black in such a way that: exactly $$$U$$$ cells in the top row are black; exactly $$$R$$$ cells in the rightmost column are black; exactly $$$D$$$ cells in the bottom row are black; exactly $$$L$$$ cells in the leftmost column are black. Note that you can color zero cells black and leave every cell white. Your task is to check if there exists a solution to the given puzzle. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The only line of each testcase contains $$$5$$$ integers $$$n, U, R, D, L$$$ ($$$2 le n le 100$$$; $$$0 le U, R, D, L le n$$$). Output For each testcase print "YES" if the solution exists and "NO" otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer). Example Input 4 5 2 5 3 1 3 0 0 0 0 4 4 1 4 0 2 1 1 1 1 Note Here are possible solutions to testcases $$$1$$$, $$$2$$$ and $$$4$$$: | 1,400 | false | true | true | false | false | false | true | false | false | false | 3,201 |
449A | Jzzhu has a big rectangular chocolate bar that consists of _n_u2009Γu2009_m_ unit squares. He wants to cut this bar exactly _k_ times. Each cut must meet the following requirements: each cut should be straight (horizontal or vertical); each cut should go along edges of unit squares (it is prohibited to divide any unit chocolate square with cut); each cut should go inside the whole chocolate bar, and all cuts must be distinct. The picture below shows a possible way to cut a 5u2009Γu20096 chocolate for 5 times. Imagine Jzzhu have made _k_ cuts and the big chocolate is splitted into several pieces. Consider the smallest (by area) piece of the chocolate, Jzzhu wants this piece to be as large as possible. What is the maximum possible area of smallest piece he can get with exactly _k_ cuts? The area of a chocolate piece is the number of unit squares in it. Input A single line contains three integers _n_,u2009_m_,u2009_k_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009109;xa01u2009β€u2009_k_u2009β€u20092Β·109). Output Output a single integer representing the answer. If it is impossible to cut the big chocolate _k_ times, print -1. Note In the first sample, Jzzhu can cut the chocolate following the picture below: In the second sample the optimal division looks like this: In the third sample, it's impossible to cut a 2u2009Γu20093 chocolate 4 times. | 1,700 | true | true | false | false | false | false | false | false | false | false | 8,044 |
833E | It is well-known that the best decoration for a flower bed in Sweetland are vanilla muffins. Seedlings of this plant need sun to grow up. Slastyona has _m_ seedlings, and the _j_-th seedling needs at least _k__j_ minutes of sunlight to grow up. Most of the time it's sunny in Sweetland, but sometimes some caramel clouds come, the _i_-th of which will appear at time moment (minute) _l__i_ and disappear at time moment _r__i_. Of course, the clouds make shadows, and the seedlings can't grow when there is at least one cloud veiling the sun. Slastyona wants to grow up her muffins as fast as possible. She has exactly _C_ candies, which is the main currency in Sweetland. One can dispel any cloud by paying _c__i_ candies. However, in order to comply with Sweetland's Department of Meteorology regulations, one can't dispel more than two clouds. Slastyona hasn't decided yet which of the _m_ seedlings will be planted at the princess' garden, so she needs your help. For each seedling determine the earliest moment it can grow up if Slastyona won't break the law and won't spend more candies than she has. Note that each of the seedlings is considered independently. The seedlings start to grow at time moment 0. Input The first line contains two integers _n_ and _C_ (0u2009β€u2009_n_u2009β€u20093Β·105,u20090u2009β€u2009_C_u2009β€u2009109)xa0β the number of caramel clouds and the number of candies Slastyona has. The next _n_ lines contain three integers each: _l__i_,u2009_r__i_,u2009_c__i_ (0u2009β€u2009_l__i_u2009<u2009_r__i_u2009β€u2009109,u20090u2009β€u2009_c__i_u2009β€u2009109), describing one caramel cloud. The next line contains single integer _m_ (1u2009β€u2009_m_u2009β€u20093Β·105)xa0β the number of seedlings. Each of the seedlings is described with one integer _k__j_ (1u2009β€u2009_k__j_u2009β€u2009109)xa0β the required number of sunny minutes. Output For each seedling print one integerxa0β the minimum minute Slastyona can grow it up. Examples Input 3 5 1 7 1 1 6 2 1 7 1 3 7 2 5 Input 3 15 1 4 17 2 8 6 4 8 9 2 5 1 Input 2 10 3 7 9 10 90 10 2 10 100 Note Consider the first example. For each _k_ it is optimal to dispel clouds 1 and 3. Then the remaining cloud will give shadow on time segment [1..6]. So, intervals [0..1] and [6.._inf_) are sunny. In the second example for _k_u2009=u20091 it is not necessary to dispel anything, and for _k_u2009=u20095 the best strategy is to dispel clouds 2 and 3. This adds an additional sunny segment [4..8], which together with [0..1] allows to grow up the muffin at the eight minute. If the third example the two seedlings are completely different. For the first one it is necessary to dispel cloud 1 and obtain a sunny segment [0..10]. However, the same strategy gives answer 180 for the second seedling. Instead, we can dispel cloud 2, to make segments [0..3] and [7.._inf_) sunny, and this allows up to shorten the time to 104. | 3,400 | false | false | false | true | true | false | false | false | true | false | 6,430 |
2006F | Sadly, Dora poured the paint when painting the class mural. Dora considers the mural as the matrix $$$b$$$ of size $$$n imes n$$$. Initially, $$$b_{i,j} = 0$$$ for all $$$1 le i, j le n$$$. Dora has only two brushes which have two different colors. In one operation, she can paint the matrix with one of two brushes: The first brush has color $$$1$$$ on it and can paint one column of the matrix. That is, Dora chooses $$$1 leq j leq n$$$ and makes $$$b_{i,j} := 1$$$ for all $$$1 leq i leq n$$$; The second brush has color $$$2$$$ on it and can paint one row of the matrix. That is, Dora chooses $$$1 leq i leq n$$$ and makes $$$b_{i,j} := 2$$$ for all $$$1 leq j leq n$$$. Dora paints the matrix so that the resulting matrix $$$b$$$ contains only $$$1$$$ and $$$2$$$. For a matrix $$$b$$$, let $$$f(b)$$$ denote the minimum number of operations needed to turn the initial matrix (containing only $$$0$$$) into $$$b$$$. The beauty of a matrix $$$b$$$ is the number of ways to paint the initial matrix in exactly $$$f(b)$$$ operations to turn it into $$$b$$$. If there's no way to turn the initial matrix into $$$b$$$, the beauty of $$$b$$$ is $$$0$$$. However, Dora made a uniformly random mistake; there's exactly one element different in the matrix $$$a$$$ given to you from the real matrix $$$b$$$. That is, there is exactly one pair $$$(i, j)$$$ such that $$$a_{i, j} = 3 - b_{i, j}$$$. Please help Dora compute the expected beauty of the real matrix $$$b$$$ modulo $$$998,244,353$$$ (all possible $$$n^2$$$ mistakes have equal probability). Since the size of the matrix is too large, Dora will only tell you the positions of $$$m$$$ elements of color $$$1$$$, and the remaining $$$n^2-m$$$ elements have color $$$2$$$. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 2 cdot 10^5$$$, $$$0 leq m leq min(10^6, n^2)$$$)xa0β the size of the matrix and the number of elements of color $$$1$$$. Then $$$m$$$ lines follow, each containing two positive integers $$$x_i$$$ and $$$y_i$$$ ($$$1 leq x_i, y_i leq n$$$)xa0β denoting that $$$a_{x_i, y_i} = 1$$$. It is guaranteed that if $$$i eq j$$$, then $$$(x_i, y_i) eq (x_j, y_j)$$$. It is also guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$4cdot10^5$$$, and the sum of $$$m$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output a single integer β the expected beauty of the real matrix $$$b$$$, modulo $$$998,244,353$$$. Example Input 7 2 2 1 1 1 2 2 1 1 1 3 2 1 1 3 3 6 0 5 10 1 1 1 2 1 3 2 1 2 3 5 1 5 2 5 3 5 4 5 5 3 5 1 1 1 3 2 2 3 1 3 3 4 3 1 1 2 3 2 4 Output 1 499122178 665496236 120 79859554 776412275 1 Note In the first test case, the matrix $$$a = left[begin{matrix}1&12&2end{matrix} ight]$$$. Let's consider changing the element $$$(1,1)$$$ to calculate the answer. It can be proved that the minimum steps to paint the initial matrix into $$$left[begin{matrix}2&12&2end{matrix} ight]$$$ is $$$3$$$. We can first paint the first row into color $$$2$$$, then paint the second column into color $$$1$$$, and finally paint the second row into color $$$2$$$. The process is listed below: $$$$$$left[begin{matrix}0&00&0end{matrix} ight]Rightarrowleft[begin{matrix}2&20&0end{matrix} ight]Rightarrowleft[begin{matrix}2&10&1end{matrix} ight]Rightarrowleft[begin{matrix}2&12&2end{matrix} ight]$$$$$$ It can be proved that this is the only way to paint the matrix in $$$3$$$ steps. So the beauty of the matrix $$$left[begin{matrix}2&12&2end{matrix} ight]$$$ is $$$1$$$. Similarly, if any other element of the matrix is changed, the beauty is always $$$1$$$, so the expected beauty of the real matrix $$$b$$$ is $$$1$$$. In the second test case, the matrix $$$a = left[begin{matrix}1&22&2end{matrix} ight]$$$. Let's consider changing the element $$$(2, 2)$$$ to calculate the answer. It can be proven that it's impossible to paint the initial matrix into $$$left[begin{matrix}1&22&1end{matrix} ight]$$$, so its beauty is $$$0$$$. If any other element of the matrix is changed, the beauty is always $$$2$$$, so the expected beauty is $$$frac{0 + 2 + 2 + 2}{4} = frac{6}{4} equiv 499,122,178 pmod {998,244,353}$$$. | 3,500 | false | false | true | false | false | true | true | false | false | true | 212 |
1284B | A sequence $$$a = [a_1, a_2, ldots, a_l]$$$ of length $$$l$$$ has an ascent if there exists a pair of indices $$$(i, j)$$$ such that $$$1 le i < j le l$$$ and $$$a_i < a_j$$$. For example, the sequence $$$[0, 2, 0, 2, 0]$$$ has an ascent because of the pair $$$(1, 4)$$$, but the sequence $$$[4, 3, 3, 3, 1]$$$ doesn't have an ascent. Let's call a concatenation of sequences $$$p$$$ and $$$q$$$ the sequence that is obtained by writing down sequences $$$p$$$ and $$$q$$$ one right after another without changing the order. For example, the concatenation of the $$$[0, 2, 0, 2, 0]$$$ and $$$[4, 3, 3, 3, 1]$$$ is the sequence $$$[0, 2, 0, 2, 0, 4, 3, 3, 3, 1]$$$. The concatenation of sequences $$$p$$$ and $$$q$$$ is denoted as $$$p+q$$$. Gyeonggeun thinks that sequences with ascents bring luck. Therefore, he wants to make many such sequences for the new year. Gyeonggeun has $$$n$$$ sequences $$$s_1, s_2, ldots, s_n$$$ which may have different lengths. Gyeonggeun will consider all $$$n^2$$$ pairs of sequences $$$s_x$$$ and $$$s_y$$$ ($$$1 le x, y le n$$$), and will check if its concatenation $$$s_x + s_y$$$ has an ascent. Note that he may select the same sequence twice, and the order of selection matters. Please count the number of pairs ($$$x, y$$$) of sequences $$$s_1, s_2, ldots, s_n$$$ whose concatenation $$$s_x + s_y$$$ contains an ascent. Input The first line contains the number $$$n$$$ ($$$1 le n le 100,000$$$) denoting the number of sequences. The next $$$n$$$ lines contain the number $$$l_i$$$ ($$$1 le l_i$$$) denoting the length of $$$s_i$$$, followed by $$$l_i$$$ integers $$$s_{i, 1}, s_{i, 2}, ldots, s_{i, l_i}$$$ ($$$0 le s_{i, j} le 10^6$$$) denoting the sequence $$$s_i$$$. It is guaranteed that the sum of all $$$l_i$$$ does not exceed $$$100,000$$$. Note For the first example, the following $$$9$$$ arrays have an ascent: $$$[1, 2], [1, 2], [1, 3], [1, 3], [1, 4], [1, 4], [2, 3], [2, 4], [3, 4]$$$. Arrays with the same contents are counted as their occurences. | 1,400 | false | false | true | true | true | false | false | true | true | false | 4,273 |
272B | Problem - 272B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation math *1400 No tag edit access β Contest materials , which can be defined with the following recurrence: _f_(0)u2009=u20090; _f_(2Β·_x_)u2009=u2009_f_(_x_); _f_(2Β·_x_u2009+u20091)u2009=u2009_f_(_x_)u2009+u20091. Dima wonders, how many pairs of indexes (_i_,u2009_j_) (1u2009β€u2009_i_u2009<u2009_j_u2009β€u2009_n_) are there, such that _f_(_a__i_)u2009=u2009_f_(_a__j_). Help him, count the number of such pairs. Input The first line contains integer _n_ (1u2009β€u2009_n_u2009β€u2009105). The second line contains _n_ positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009109). The numbers in the lines are separated by single spaces. Output In a single line print the answer to the problem. Please, don't use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 4 Output 3 Input 3 5 3 1 Output 1 Note In the first sample any pair (_i_,u2009_j_) will do, so the answer is 3. In the second sample only pair (1,u20092) will do. | 1,400 | true | false | true | false | false | false | false | false | false | false | 8,745 |
1851B | You have an array of integers $$$a$$$ of length $$$n$$$. You can apply the following operation to the given array: Swap two elements $$$a_i$$$ and $$$a_j$$$ such that $$$i eq j$$$, $$$a_i$$$ and $$$a_j$$$ are either both even or both odd. Determine whether it is possible to sort the array in non-decreasing order by performing the operation any number of times (possibly zero). For example, let $$$a$$$ = [$$$7, 10, 1, 3, 2$$$]. Then we can perform $$$3$$$ operations to sort the array: 1. Swap $$$a_3 = 1$$$ and $$$a_1 = 7$$$, since $$$1$$$ and $$$7$$$ are odd. We get $$$a$$$ = [$$$1, 10, 7, 3, 2$$$]; 2. Swap $$$a_2 = 10$$$ and $$$a_5 = 2$$$, since $$$10$$$ and $$$2$$$ are even. We get $$$a$$$ = [$$$1, 2, 7, 3, 10$$$]; 3. Swap $$$a_4 = 3$$$ and $$$a_3 = 7$$$, since $$$3$$$ and $$$7$$$ are odd. We get $$$a$$$ = [$$$1, 2, 3, 7, 10$$$]. Input The first line of input data contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β the length of array $$$a$$$. The second line of each test case contains exactly $$$n$$$ positive integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β the elements of array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output on a separate line: YES if the array can be sorted by applying the operation to it some number of times; NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response). Example Input 6 5 7 10 1 3 2 4 11 9 3 5 5 11 3 15 3 2 6 10 7 8 1 2 3 1 10 5 6 6 4 1 6 Output YES YES NO NO YES NO Note The first test case is explained in the problem statement. | 800 | false | true | false | false | false | false | false | false | true | false | 1,173 |
1949I | You are given $$$n$$$ disks in the plane. The center of each disk has integer coordinates, and the radius of each disk is a positive integer. No two disks overlap in a region of positive area, but it is possible for disks to be tangent to each other. Your task is to determine whether it is possible to change the radii of the disks in such a way that: Disks that were tangent to each other remain tangent to each other. No two disks overlap in a region of positive area. The sum of all radii strictly decreases. The new radii are allowed to be arbitrary positive real numbers. The centers of the disks cannot be changed. Input The first line contains an integer $$$n$$$ ($$$1le n le 1000$$$) β the number of disks. The next $$$n$$$ lines contain three integers each. The $$$i$$$-th of such lines contains $$$x_i$$$, $$$y_i$$$ ($$$-10^9 leq x_i, y_i leq 10^9$$$), and $$$r_i$$$ ($$$1 leq r_i leq 10^9$$$) β the coordinates of the center, and the radius, of the $$$i$$$-th disk. Output Print $$$ exttt{YES}$$$ if it is possible to change the radii in the desired manner. Otherwise, print $$$ exttt{NO}$$$. Examples Input 5 0 2 1 0 0 1 4 -3 4 11 0 3 11 5 2 Input 4 2 2 2 7 2 3 7 7 2 2 7 3 Note In the first sample, one can decrease the radii of the first and third disk by $$$0.5$$$, and increase the radius of the second disk by $$$0.5$$$. This way, the sum of all radii decreases by $$$0.5$$$. The situation before and after changing the radii is depicted below. First sample (left) and a valid way to change the radii of the disks (right). In the second sample, depicted below, there is no way to change the radii of the disks in the desired manner. Second sample. | 1,800 | false | false | false | false | false | false | false | false | false | true | 584 |
260A | Problem - 260A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation math *1400 No tag edit access β Contest materials ") Tutorial") to the right provided that the resulting number is divisible by Vasya's number _b_. If it is impossible to obtain the number which is divisible by _b_, then the lengthening operation cannot be performed. Your task is to help Vasya and print the number he can get after applying the lengthening operation to number _a_ _n_ times. Input The first line contains three integers: _a_,u2009_b_,u2009_n_ (1u2009β€u2009_a_,u2009_b_,u2009_n_u2009β€u2009105). Output In a single line print the integer without leading zeros, which Vasya can get when he applies the lengthening operations to number _a_ _n_ times. If no such number exists, then print number -1. If there are multiple possible answers, print any of them. Examples Input 5 4 5 Output 524848 Input 12 11 1 Output 121 Input 260 150 10 Output -1 | 1,400 | true | false | true | false | false | false | false | false | false | false | 8,795 |
1488G | Problem - 1488G - Codeforces =============== xa0 . The cost of painting is the number of pairs $$$(x, y)$$$ such that $$$y bmod x = 0$$$, $$$y$$$ is red and $$$x$$$ is blue. For each $$$k in | 2,500 | false | true | false | false | true | false | false | false | false | false | 3,233 |
470A | Problem - 470A - Codeforces =============== xa0 points on each side. The formula is _H__n_u2009=u20093Β·_n_Β·(_n_u2009+u20091)u2009+u20091. You are given _n_; calculate _n_-th element of the sequence. Input The only line of input contains an integer _n_ (0u2009β€u2009_n_u2009β€u20099). Output Output the _n_-th element of crystal ball sequence. Examples Input 1 Output 7 Input 3 Output 37 | 1,400 | false | false | true | false | false | false | false | false | false | false | 7,960 |
547B | Problem - 547B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags binary search data structures dp dsu *1900 No tag edit access β Contest materials , the number of bears. The second line contains _n_ integers separated by space, _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009109), heights of bears. Output Print _n_ integers in one line. For each _x_ from 1 to _n_, print the maximum strength among all groups of size _x_. Examples Input 10 1 2 3 4 5 4 3 2 1 6 Output 6 4 4 3 3 2 2 1 1 1 | 1,900 | false | false | false | true | true | false | false | true | false | false | 7,651 |
1552B | The Olympic Games have just started and Federico is eager to watch the marathon race. There will be $$$n$$$ athletes, numbered from $$$1$$$ to $$$n$$$, competing in the marathon, and all of them have taken part in $$$5$$$ important marathons, numbered from $$$1$$$ to $$$5$$$, in the past. For each $$$1le ile n$$$ and $$$1le jle 5$$$, Federico remembers that athlete $$$i$$$ ranked $$$r_{i,j}$$$-th in marathon $$$j$$$ (e.g., $$$r_{2,4}=3$$$ means that athlete $$$2$$$ was third in marathon $$$4$$$). Federico considers athlete $$$x$$$ superior to athlete $$$y$$$ if athlete $$$x$$$ ranked better than athlete $$$y$$$ in at least $$$3$$$ past marathons, i.e., $$$r_{x,j}<r_{y,j}$$$ for at least $$$3$$$ distinct values of $$$j$$$. Federico believes that an athlete is likely to get the gold medal at the Olympics if he is superior to all other athletes. Find any athlete who is likely to get the gold medal (that is, an athlete who is superior to all other athletes), or determine that there is no such athlete. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) β the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 50,000$$$) β the number of athletes. Then $$$n$$$ lines follow, each describing the ranking positions of one athlete. The $$$i$$$-th of these lines contains the $$$5$$$ integers $$$r_{i,1},,r_{i,2},,r_{i,3},,r_{i,4},, r_{i,5}$$$ ($$$1le r_{i,j}le 50,000$$$) β the ranking positions of athlete $$$i$$$ in the past $$$5$$$ marathons. It is guaranteed that, in each of the $$$5$$$ past marathons, the $$$n$$$ athletes have distinct ranking positions, i.e., for each $$$1le jle 5$$$, the $$$n$$$ values $$$r_{1,j},, r_{2, j},, dots,, r_{n, j}$$$ are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$50,000$$$. Output For each test case, print a single integer β the number of an athlete who is likely to get the gold medal (that is, an athlete who is superior to all other athletes). If there are no such athletes, print $$$-1$$$. If there is more than such one athlete, print any of them. Example Input 4 1 50000 1 50000 50000 50000 3 10 10 20 30 30 20 20 30 10 10 30 30 10 20 20 3 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 6 9 5 3 7 1 7 4 1 6 8 5 6 7 3 2 6 7 8 8 6 4 2 2 4 5 8 3 6 9 4 Note Explanation of the first test case: There is only one athlete, therefore he is superior to everyone else (since there is no one else), and thus he is likely to get the gold medal. Explanation of the second test case: There are $$$n=3$$$ athletes. Athlete $$$1$$$ is superior to athlete $$$2$$$. Indeed athlete $$$1$$$ ranks better than athlete $$$2$$$ in the marathons $$$1$$$, $$$2$$$ and $$$3$$$. Athlete $$$2$$$ is superior to athlete $$$3$$$. Indeed athlete $$$2$$$ ranks better than athlete $$$3$$$ in the marathons $$$1$$$, $$$2$$$, $$$4$$$ and $$$5$$$. Athlete $$$3$$$ is superior to athlete $$$1$$$. Indeed athlete $$$3$$$ ranks better than athlete $$$1$$$ in the marathons $$$3$$$, $$$4$$$ and $$$5$$$. Explanation of the third test case: There are $$$n=3$$$ athletes. Athlete $$$1$$$ is superior to athletes $$$2$$$ and $$$3$$$. Since he is superior to all other athletes, he is likely to get the gold medal. Athlete $$$2$$$ is superior to athlete $$$3$$$. Athlete $$$3$$$ is not superior to any other athlete. Explanation of the fourth test case: There are $$$n=6$$$ athletes. Athlete $$$1$$$ is superior to athletes $$$3$$$, $$$4$$$, $$$6$$$. Athlete $$$2$$$ is superior to athletes $$$1$$$, $$$4$$$, $$$6$$$. Athlete $$$3$$$ is superior to athletes $$$2$$$, $$$4$$$, $$$6$$$. Athlete $$$4$$$ is not superior to any other athlete. Athlete $$$5$$$ is superior to athletes $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$6$$$. Since he is superior to all other athletes, he is likely to get the gold medal. Athlete $$$6$$$ is only superior to athlete $$$4$$$. | 1,500 | false | true | false | false | false | false | false | false | true | true | 2,883 |
1630E | Given a cyclic array $$$a$$$ of size $$$n$$$, where $$$a_i$$$ is the value of $$$a$$$ in the $$$i$$$-th position, there may be repeated values. Let us define that a permutation of $$$a$$$ is equal to another permutation of $$$a$$$ if and only if their values are the same for each position $$$i$$$ or we can transform them to each other by performing some cyclic rotation. Let us define for a cyclic array $$$b$$$ its number of components as the number of connected components in a graph, where the vertices are the positions of $$$b$$$ and we add an edge between each pair of adjacent positions of $$$b$$$ with equal values (note that in a cyclic array the first and last position are also adjacents). Find the expected value of components of a permutation of $$$a$$$ if we select it equiprobably over the set of all the different permutations of $$$a$$$. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$) β the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^6$$$) β the size of the cyclic array $$$a$$$. The second line of each test case contains $$$n$$$ integers, the $$$i$$$-th of them is the value $$$a_i$$$ ($$$1 le a_i le n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. It is guaranteed that the total number of different permutations of $$$a$$$ is not divisible by $$$M$$$ Output For each test case print a single integer β the expected value of components of a permutation of $$$a$$$ if we select it equiprobably over the set of all the different permutations of $$$a$$$ modulo $$$998,244,353$$$. Formally, let $$$M = 998,244,353$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Example Input 5 4 1 1 1 1 4 1 1 2 1 4 1 2 1 2 5 4 3 2 5 1 12 1 3 2 3 2 1 3 3 1 3 3 2 Note In the first test case there is only $$$1$$$ different permutation of $$$a$$$: $$$[1, 1, 1, 1]$$$ has $$$1$$$ component. Therefore the expected value of components is $$$frac{1}{1} = 1$$$ In the second test case there are $$$4$$$ ways to permute the cyclic array $$$a$$$, but there is only $$$1$$$ different permutation of $$$a$$$: $$$[1, 1, 1, 2]$$$, $$$[1, 1, 2, 1]$$$, $$$[1, 2, 1, 1]$$$ and $$$[2, 1, 1, 1]$$$ are the same permutation and have $$$2$$$ components. Therefore the expected value of components is $$$frac{2}{1} = 2$$$ In the third test case there are $$$6$$$ ways to permute the cyclic array $$$a$$$, but there are only $$$2$$$ different permutations of $$$a$$$: $$$[1, 1, 2, 2]$$$, $$$[2, 1, 1, 2]$$$, $$$[2, 2, 1, 1]$$$ and $$$[1, 2, 2, 1]$$$ are the same permutation and have $$$2$$$ components. $$$[1, 2, 1, 2]$$$ and $$$[2, 1, 2, 1]$$$ are the same permutation and have $$$4$$$ components. Therefore the expected value of components is $$$frac{2+4}{2} = frac{6}{2} = 3$$$ In the fourth test case there are $$$120$$$ ways to permute the cyclic array $$$a$$$, but there are only $$$24$$$ different permutations of $$$a$$$: Any permutation of $$$a$$$ has $$$5$$$ components. Therefore the expected value of components is $$$frac{24cdot 5}{24} = frac{120}{24} = 5$$$ | 2,900 | true | false | false | false | false | false | false | false | false | false | 2,462 |
720C | Today Peter has got an additional homework for tomorrow. The teacher has given three integers to him: _n_, _m_ and _k_, and asked him to mark one or more squares on a square grid of size _n_u2009Γu2009_m_. The marked squares must form a connected figure, and there must be exactly _k_ triples of marked squares that form an L-shaped trominoxa0β all three squares are inside a 2u2009Γu20092 square. The set of squares forms a connected figure if it is possible to get from any square to any other one if you are allowed to move from a square to any adjacent by a common side square. Peter cannot fulfill the task, so he asks you for help. Help him to create such figure. Input Input data contains one or more test cases. The first line contains the number of test cases _t_ (1u2009β€u2009_t_u2009β€u2009100). Each of the following _t_ test cases is described by a line that contains three integers: _n_, _m_ and _k_ (3u2009β€u2009_n_,u2009_m_, _n_u2009Γu2009_m_u2009β€u2009105, 0u2009β€u2009_k_u2009β€u2009109). The sum of values of _n_u2009Γu2009_m_ for all tests in one input data doesn't exceed 105. Output For each test case print the answer. If it is possible to create such figure, print _n_ lines, _m_ characters each, use asterisk '*' to denote the marked square, and dot '.' to denote the unmarked one. If there is no solution, print -1. Print empty line between test cases. Example Output .*. ** .*.**. **. *.. .*. ** *.. | 3,100 | false | false | false | false | false | true | false | false | false | false | 6,935 |
903B | Vova is again playing some computer game, now an RPG. In the game Vova's character received a quest: to slay the fearsome monster called Modcrab. After two hours of playing the game Vova has tracked the monster and analyzed its tactics. The Modcrab has _h_2 health points and an attack power of _a_2. Knowing that, Vova has decided to buy a lot of strong healing potions and to prepare for battle. Vova's character has _h_1 health points and an attack power of _a_1. Also he has a large supply of healing potions, each of which increases his current amount of health points by _c_1 when Vova drinks a potion. All potions are identical to each other. It is guaranteed that _c_1u2009>u2009_a_2. The battle consists of multiple phases. In the beginning of each phase, Vova can either attack the monster (thus reducing its health by _a_1) or drink a healing potion (it increases Vova's health by _c_1; Vova's health can exceed _h_1). Then, if the battle is not over yet, the Modcrab attacks Vova, reducing his health by _a_2. The battle ends when Vova's (or Modcrab's) health drops to 0 or lower. It is possible that the battle ends in a middle of a phase after Vova's attack. Of course, Vova wants to win the fight. But also he wants to do it as fast as possible. So he wants to make up a strategy that will allow him to win the fight after the minimum possible number of phases. Help Vova to make up a strategy! You may assume that Vova never runs out of healing potions, and that he can always win. Input The first line contains three integers _h_1, _a_1, _c_1 (1u2009β€u2009_h_1,u2009_a_1u2009β€u2009100, 2u2009β€u2009_c_1u2009β€u2009100) β Vova's health, Vova's attack power and the healing power of a potion. The second line contains two integers _h_2, _a_2 (1u2009β€u2009_h_2u2009β€u2009100, 1u2009β€u2009_a_2u2009<u2009_c_1) β the Modcrab's health and his attack power. Output In the first line print one integer _n_ denoting the minimum number of phases required to win the battle. Then print _n_ lines. _i_-th line must be equal to HEAL if Vova drinks a potion in _i_-th phase, or STRIKE if he attacks the Modcrab. The strategy must be valid: Vova's character must not be defeated before slaying the Modcrab, and the monster's health must be 0 or lower after Vova's last action. If there are multiple optimal solutions, print any of them. Examples Output 4 STRIKE HEAL STRIKE STRIKE Note In the first example Vova's character must heal before or after his first attack. Otherwise his health will drop to zero in 2 phases while he needs 3 strikes to win. In the second example no healing needed, two strikes are enough to get monster to zero health and win with 6 health left. | 1,200 | false | true | true | false | false | false | false | false | false | false | 6,133 |
110A | Problem - 110A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation *800 No tag edit access β Contest materials . Please do not use the %lld specificator to read or write 64-bit numbers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Output Print on the single line "YES" if _n_ is a nearly lucky number. Otherwise, print "NO" (without the quotes). Examples Input 40047 Output NO Input 7747774 Output YES Input 1000000000000000000 Output NO Note In the first sample there are 3 lucky digits (first one and last two), so the answer is "NO". In the second sample there are 7 lucky digits, 7 is lucky number, so the answer is "YES". In the third sample there are no lucky digits, so the answer is "NO". | 800 | false | false | true | false | false | false | false | false | false | false | 9,444 |
285B | Petya and Vasya are playing a game. Petya's got _n_ non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to _n_ from left to right. Note that the positions are indexed but the glasses are not. First Petya puts a marble under the glass in position _s_. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position _p_1, the glass from the second position to position _p_2 and so on. That is, a glass goes from position _i_ to position _p__i_. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in. After all shuffling operations Petya shows Vasya that the ball has moved to position _t_. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position _s_ to position _t_. Input The first line contains three integers: _n_,u2009_s_,u2009_t_ (1u2009β€u2009_n_u2009β€u2009105;xa01u2009β€u2009_s_,u2009_t_u2009β€u2009_n_) β the number of glasses, the ball's initial and final position. The second line contains _n_ space-separated integers: _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009β€u2009_p__i_u2009β€u2009_n_) β the shuffling operation parameters. It is guaranteed that all _p__i_'s are distinct. Note that _s_ can equal _t_. Output If the marble can move from position _s_ to position _t_, then print on a single line a non-negative integer β the minimum number of shuffling operations, needed to get the marble to position _t_. If it is impossible, print number -1. | 1,200 | false | false | true | false | false | false | false | false | false | false | 8,695 |
629E | Famil Doorβs City map looks like a tree (undirected connected acyclic graph) so other people call it Treeland. There are _n_ intersections in the city connected by _n_u2009-u20091 bidirectional roads. There are _m_ friends of Famil Door living in the city. The _i_-th friend lives at the intersection _u__i_ and works at the intersection _v__i_. Everyone in the city is unhappy because there is exactly one simple path between their home and work. Famil Door plans to construct exactly one new road and he will randomly choose one among _n_Β·(_n_u2009-u20091)u2009/u20092 possibilities. Note, that he may even build a new road between two cities that are already connected by one. He knows, that each of his friends will become happy, if after Famil Door constructs a new road there is a path from this friend home to work and back that doesn't visit the same road twice. Formally, there is a simple cycle containing both _u__i_ and _v__i_. Moreover, if the friend becomes happy, his pleasure is equal to the length of such path (it's easy to see that it's unique). For each of his friends Famil Door wants to know his expected pleasure, that is the expected length of the cycle containing both _u__i_ and _v__i_ if we consider only cases when such a cycle exists. Input The first line of the input contains integers _n_ and _m_ (2u2009β€u2009_n_,u2009 _m_u2009β€u2009100u2009000)xa0β the number of the intersections in the Treeland and the number of Famil Door's friends. Then follow _n_u2009-u20091 lines describing bidirectional roads. Each of them contains two integers _a__i_ and _b__i_ (1u2009β€u2009_a__i_,u2009_b__i_u2009β€u2009_n_)xa0β the indices of intersections connected by the _i_-th road. Last _m_ lines of the input describe Famil Door's friends. The _i_-th of these lines contain two integers _u__i_ and _v__i_ (1u2009β€u2009_u__i_,u2009_v__i_u2009β€u2009_n_,u2009_u__i_u2009β u2009_v__i_)xa0β indices of intersections where the _i_-th friend lives and works. Output For each friend you should print the expected value of pleasure if he will be happy. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if . Examples Input 4 3 2 4 4 1 3 2 3 1 2 3 4 1 Output 4.00000000 3.00000000 3.00000000 Output 2.50000000 2.50000000 3.00000000 Note Consider the second sample. 1. Both roads (1,u20092) and (2,u20093) work, so the expected length if 2. Roads (1,u20093) and (2,u20093) make the second friend happy. Same as for friend 1 the answer is 2.5 3. The only way to make the third friend happy is to add road (2,u20093), so the answer is 3 | 2,300 | false | false | false | true | true | false | false | false | false | false | 7,305 |
711E | ZS the Coder has recently found an interesting concept called the Birthday Paradox. It states that given a random set of 23 people, there is around 50% chance that some two of them share the same birthday. ZS the Coder finds this very interesting, and decides to test this with the inhabitants of Udayland. In Udayland, there are 2_n_ days in a year. ZS the Coder wants to interview _k_ people from Udayland, each of them has birthday in one of 2_n_ days (each day with equal probability). He is interested in the probability of at least two of them have the birthday at the same day. ZS the Coder knows that the answer can be written as an irreducible fraction . He wants to find the values of _A_ and _B_ (he does not like to deal with floating point numbers). Can you help him? Input The first and only line of the input contains two integers _n_ and _k_ (1u2009β€u2009_n_u2009β€u20091018,u20092u2009β€u2009_k_u2009β€u20091018), meaning that there are 2_n_ days in a year and that ZS the Coder wants to interview exactly _k_ people. Output If the probability of at least two _k_ people having the same birthday in 2_n_ days long year equals (_A_u2009β₯u20090, _B_u2009β₯u20091, ), print the _A_ and _B_ in a single line. Since these numbers may be too large, print them modulo 106u2009+u20093. Note that _A_ and _B_ must be coprime before their remainders modulo 106u2009+u20093 are taken. Note In the first sample case, there are 23u2009=u20098 days in Udayland. The probability that 2 people have the same birthday among 2 people is clearly , so _A_u2009=u20091, _B_u2009=u20098. In the second sample case, there are only 21u2009=u20092 days in Udayland, but there are 3 people, so it is guaranteed that two of them have the same birthday. Thus, the probability is 1 and _A_u2009=u2009_B_u2009=u20091. | 2,300 | true | false | false | false | false | false | false | false | false | false | 6,972 |
1935A | Congratulations, you have been accepted to the Master's Assistance Center! However, you were extremely bored in class and got tired of doing nothing, so you came up with a game for yourself. You are given a string $$$s$$$ and an even integer $$$n$$$. There are two types of operations that you can apply to it: 1. Add the reversed string $$$s$$$ to the end of the string $$$s$$$ (for example, if $$$s = $$$ cpm, then after applying the operation $$$s = $$$ cpmmpc). 2. Reverse the current string $$$s$$$ (for example, if $$$s = $$$ cpm, then after applying the operation $$$s = $$$ mpc). It is required to determine the lexicographically smallest$$$^{dagger}$$$ string that can be obtained after applying exactly $$$n$$$ operations. Note that you can apply operations of different types in any order, but you must apply exactly $$$n$$$ operations in total. $$$^{dagger}$$$A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a e b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 500$$$) β the number of test cases. The description of the test cases follows. The first line of each test case contains a single even integer $$$n$$$ ($$$2 leq n leq 10^9$$$) β the number of operations applied to the string $$$s$$$. The second line of each test case contains a single string $$$s$$$ ($$$1 leq s leq 100$$$), consisting of lowercase English letters, β the string to which the operations are applied. Output For each test case, output a single line β the lexicographically smallest string that can be obtained after applying exactly $$$n$$$ operations. Example Input 5 4 cpm 2 grib 10 kupitimilablodarbuz 1000000000 capybara 6 abacaba Output cpm birggrib kupitimilablodarbuz arabypaccapybara abacaba Note In the first test case, you can apply the operation of the second type (i.e., reverse the string $$$s$$$) $$$4$$$ times. Then the string $$$s$$$ will remain equal to cpm. In the second test case, you can do the following: Apply the operation of the second type, after which $$$s$$$ will become equal to birg. Apply operation of the first type (i.e., add the reversed string $$$s$$$ to the end of the string $$$s$$$), after which $$$s$$$ will become equal to birggrib. | 800 | false | false | false | false | false | true | false | false | false | false | 674 |
1848A | Vika and her friends went shopping in a mall, which can be represented as a rectangular grid of rooms with sides of length $$$n$$$ and $$$m$$$. Each room has coordinates $$$(a, b)$$$, where $$$1 le a le n, 1 le b le m$$$. Thus we call a hall with coordinates $$$(c, d)$$$ a neighbouring for it if $$$a - c + b - d = 1$$$. Tired of empty fashion talks, Vika decided to sneak away unnoticed. But since she hasn't had a chance to visit one of the shops yet, she doesn't want to leave the mall. After a while, her friends noticed Vika's disappearance and started looking for her. Currently, Vika is in a room with coordinates $$$(x, y)$$$, and her $$$k$$$ friends are in rooms with coordinates $$$(x_1, y_1)$$$, $$$(x_2, y_2)$$$, ... $$$, (x_k, y_k)$$$, respectively. The coordinates can coincide. Note that all the girls must move to the neighbouring rooms. Every minute, first Vika moves to one of the adjacent to the side rooms of her choice, and then each friend (seeing Vika's choice) also chooses one of the adjacent rooms to move to. If at the end of the minute (that is, after all the girls have moved on to the neighbouring rooms) at least one friend is in the same room as Vika, she is caught and all the other friends are called. Tell us, can Vika run away from her annoying friends forever, or will she have to continue listening to empty fashion talks after some time? Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0β the number of test cases. The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, $$$k$$$ ($$$1 le n, m, k le 100$$$)xa0β the sizes of the mall and the number of Vika's friends. The second line of each test case contains a pair of integers $$$x$$$ and $$$y$$$ ($$$1 le x le n$$$, $$$1 le y le m$$$)xa0β the coordinates of the room where Vika is. Each of the next $$$k$$$ lines of each test case contains a pair of integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i le n$$$, $$$1 le y_i le m$$$)xa0β the coordinates of the room where the $$$i$$$-th friend is. Output For each test case, output "YES" if Vika can run away from her friends forever, otherwise output "NO". You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer. Note In the first test case, the friend will never catch up with Vika, because Vika can always move to the room diagonally opposite to the one where the friend is. In the second test case, no matter where Vika goes, each of her friends can catch her after the first move. In the third test case, Vika and her friend will always be in different halls. | 900 | true | false | false | false | false | false | false | false | false | false | 1,194 |
1879A | Monocarp organizes a weightlifting competition. There are $$$n$$$ athletes participating in the competition, the $$$i$$$-th athlete has strength $$$s_i$$$ and endurance $$$e_i$$$. The $$$1$$$-st athlete is Monocarp's friend Polycarp, and Monocarp really wants Polycarp to win. The competition will be conducted as follows. The jury will choose a positive (greater than zero) integer $$$w$$$, which denotes the weight of the barbell that will be used in the competition. The goal for each athlete is to lift the barbell as many times as possible. The athlete who lifts the barbell the most amount of times will be declared the winner (if there are multiple such athletes β there's no winner). If the barbell's weight $$$w$$$ is strictly greater than the strength of the $$$i$$$-th athlete $$$s_i$$$, then the $$$i$$$-th athlete will be unable to lift the barbell even one single time. Otherwise, the $$$i$$$-th athlete will be able to lift the barbell, and the number of times he does it will be equal to his endurance $$$e_i$$$. For example, suppose there are $$$4$$$ athletes with parameters $$$s_1 = 7, e_1 = 4$$$; $$$s_2 = 9, e_2 = 3$$$; $$$s_3 = 4, e_3 = 6$$$; $$$s_4 = 2, e_4 = 2$$$. If the weight of the barbell is $$$5$$$, then: the first athlete will be able to lift the barbell $$$4$$$ times; the second athlete will be able to lift the barbell $$$3$$$ times; the third athlete will be unable to lift the barbell; the fourth athlete will be unable to lift the barbell. Monocarp wants to choose $$$w$$$ in such a way that Polycarp (the $$$1$$$-st athlete) wins the competition. Help him to choose the value of $$$w$$$, or report that it is impossible. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) β the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$2 le n le 100$$$) β the number of athletes. Then $$$n$$$ lines follow, the $$$i$$$-th of them contains two integers $$$s_i$$$ and $$$e_i$$$ ($$$1 le s_i le 10^9$$$; $$$1 le e_i le 100$$$) β the strength and the endurance of the $$$i$$$-th athlete. | 800 | false | true | false | false | false | false | false | false | false | false | 1,010 |
1580D | Alice has an integer sequence $$$a$$$ of length $$$n$$$ and all elements are different. She will choose a subsequence of $$$a$$$ of length $$$m$$$, and defines the value of a subsequence $$$a_{b_1},a_{b_2},ldots,a_{b_m}$$$ as $$$$$$sum_{i = 1}^m (m cdot a_{b_i}) - sum_{i = 1}^m sum_{j = 1}^m f(min(b_i, b_j), max(b_i, b_j)),$$$$$$ where $$$f(i, j)$$$ denotes $$$min(a_i, a_{i + 1}, ldots, a_j)$$$. Alice wants you to help her to maximize the value of the subsequence she choose. A sequence $$$s$$$ is a subsequence of a sequence $$$t$$$ if $$$s$$$ can be obtained from $$$t$$$ by deletion of several (possibly, zero or all) elements. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le m le n le 4000$$$). The second line contains $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i < 2^{31}$$$). Output Print the maximal value Alice can get. Examples Input 11 5 9 3 7 1 8 12 10 20 15 18 5 Note In the first example, Alice can choose the subsequence $$$[15, 2, 18, 13]$$$, which has the value $$$4 cdot (15 + 2 + 18 + 13) - (15 + 2 + 2 + 2) - (2 + 2 + 2 + 2) - (2 + 2 + 18 + 12) - (2 + 2 + 12 + 13) = 100$$$. In the second example, there are a variety of subsequences with value $$$176$$$, and one of them is $$$[9, 7, 12, 20, 18]$$$. | 2,900 | false | true | false | true | false | false | true | false | false | false | 2,730 |
1934A | Given an array $$$a$$$ of $$$n$$$ elements, find the maximum value of the expression: $$$$$$a_i - a_j + a_j - a_k + a_k - a_l + a_l - a_i$$$$$$ where $$$i$$$, $$$j$$$, $$$k$$$, and $$$l$$$ are four distinct indices of the array $$$a$$$, with $$$1 le i, j, k, l le n$$$. Here $$$x$$$ denotes the absolute value of $$$x$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 500$$$)xa0β the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$4 le n le 100$$$)xa0β the length of the given array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^6 le a_i le 10^6$$$). Output For each test case, print a single integerxa0β the maximum value. Example Input 5 4 1 1 1 1 5 1 1 2 2 3 8 5 1 3 2 -3 -1 10 3 4 3 3 1 1 4 1 2 2 -1 Note In the first test case, for any selection of $$$i$$$, $$$j$$$, $$$k$$$, $$$l$$$, the answer will be $$$0$$$. For example, $$$a_1 - a_2 + a_2 - a_3 + a_3 - a_4 + a_4 - a_1 = 1 - 1 + 1 - 1 + 1 - 1 + 1 - 1 = 0 + 0 + 0 + 0 = 0$$$. In the second test case, for $$$i = 1$$$, $$$j = 3$$$, $$$k = 2$$$, and $$$l = 5$$$, the answer will be $$$6$$$. $$$a_1 - a_3 + a_3 - a_2 + a_2 - a_5 + a_5 - a_1 = 1 - 2 + 2 - 1 + 1 - 3 + 3 - 1 = 1 + 1 + 2 + 2 = 6$$$. | 800 | true | true | false | false | false | false | false | false | false | false | 680 |
335D | You are given _n_ rectangles, labeled 1 through _n_. The corners of rectangles have integer coordinates and their edges are parallel to the _Ox_ and _Oy_ axes. The rectangles may touch each other, but they do not overlap (that is, there are no points that belong to the interior of more than one rectangle). Your task is to determine if there's a non-empty subset of the rectangles that forms a square. That is, determine if there exists a subset of the rectangles and some square for which every point that belongs to the interior or the border of that square belongs to the interior or the border of at least one of the rectangles in the subset, and every point that belongs to the interior or the border of at least one rectangle in the subset belongs to the interior or the border of that square. Input First line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009105) β the number of rectangles. Each of the next _n_ lines contains a description of a rectangle, with the _i_-th such line describing the rectangle labeled _i_. Each rectangle description consists of four integers: _x_1, _y_1, _x_2, _y_2 β coordinates of the bottom left and the top right corners (0u2009β€u2009_x_1u2009<u2009_x_2u2009β€u20093000, 0u2009β€u2009_y_1u2009<u2009_y_2u2009β€u20093000). No two rectangles overlap (that is, there are no points that belong to the interior of more than one rectangle). Output If such a subset exists, print "YES" (without quotes) on the first line of the output file, followed by _k_, the number of rectangles in the subset. On the second line print _k_ numbers β the labels of rectangles in the subset in any order. If more than one such subset exists, print any one. If no such subset exists, print "NO" (without quotes). Examples Input 9 0 0 1 9 1 0 9 1 1 8 9 9 8 1 9 8 2 2 3 6 3 2 7 3 2 6 7 7 5 3 7 6 3 3 5 6 Input 4 0 0 1 9 1 0 9 1 1 8 9 9 8 1 9 8 Note The first test case looks as follows: Note that rectangles 6, 8, and 9 form a square as well, and would be an acceptable answer. The second test case looks as follows: | 2,400 | false | false | false | true | false | false | true | false | false | false | 8,495 |
1497E1 | This is the easy version of the problem. The only difference is that in this version $$$k = 0$$$. There is an array $$$a_1, a_2, ldots, a_n$$$ of $$$n$$$ positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square. Moreover, it is allowed to do at most $$$k$$$ such operations before the division: choose a number in the array and change its value to any positive integer. But in this version $$$k = 0$$$, so it is not important. What is the minimum number of continuous segments you should use if you will make changes optimally? Input The first line contains a single integer $$$t$$$ $$$(1 le t le 1000)$$$ xa0β the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$k = 0$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^7$$$). It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case print a single integer xa0β the answer to the problem. Example Input 3 5 0 18 6 2 4 1 5 0 6 8 1 24 8 1 0 1 Note In the first test case the division may be as follows: $$$[18, 6]$$$ $$$[2, 4]$$$ $$$[1]$$$ | 1,700 | true | true | false | true | true | false | false | false | false | false | 3,183 |
1304C | Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $$$t_i$$$ β the time (in minutes) when the $$$i$$$-th customer visits the restaurant, $$$l_i$$$ β the lower bound of their preferred temperature range, and $$$h_i$$$ β the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $$$i$$$-th customer is satisfied if and only if the temperature is between $$$l_i$$$ and $$$h_i$$$ (inclusive) in the $$$t_i$$$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. Input Each test contains one or more test cases. The first line contains the number of test cases $$$q$$$ ($$$1 le q le 500$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 100$$$, $$$-10^9 le m le 10^9$$$), where $$$n$$$ is the number of reserved customers and $$$m$$$ is the initial temperature of the restaurant. Next, $$$n$$$ lines follow. The $$$i$$$-th line of them contains three integers $$$t_i$$$, $$$l_i$$$, and $$$h_i$$$ ($$$1 le t_i le 10^9$$$, $$$-10^9 le l_i le h_i le 10^9$$$), where $$$t_i$$$ is the time when the $$$i$$$-th customer visits, $$$l_i$$$ is the lower bound of their preferred temperature range, and $$$h_i$$$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $$$0$$$. Output For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Note In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $$$0$$$-th minute, change the state to heating (the temperature is 0). At $$$2$$$-nd minute, change the state to off (the temperature is 2). At $$$5$$$-th minute, change the state to heating (the temperature is 2, the $$$1$$$-st customer is satisfied). At $$$6$$$-th minute, change the state to off (the temperature is 3). At $$$7$$$-th minute, change the state to cooling (the temperature is 3, the $$$2$$$-nd customer is satisfied). At $$$10$$$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $$$0$$$-th minute and leave it be. Then all customers will be satisfied. Note that the $$$1$$$-st customer's visit time equals the $$$2$$$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied. | 1,500 | false | true | true | true | false | false | false | false | true | false | 4,169 |
1257G | You are given an integer $$$x$$$ represented as a product of $$$n$$$ its prime divisors $$$p_1 cdot p_2, cdot ldots cdot p_n$$$. Let $$$S$$$ be the set of all positive integer divisors of $$$x$$$ (including $$$1$$$ and $$$x$$$ itself). We call a set of integers $$$D$$$ good if (and only if) there is no pair $$$a in D$$$, $$$b in D$$$ such that $$$a e b$$$ and $$$a$$$ divides $$$b$$$. Find a good subset of $$$S$$$ with maximum possible size. Since the answer can be large, print the size of the subset modulo $$$998244353$$$. Input The first line contains the single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β the number of prime divisors in representation of $$$x$$$. The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$2 le p_i le 3 cdot 10^6$$$) β the prime factorization of $$$x$$$. Output Print the maximum possible size of a good subset modulo $$$998244353$$$. Examples Input 3 2999999 43 2999957 Note In the first sample, $$$x = 2999999 cdot 43 cdot 2999957$$$ and one of the maximum good subsets is $$${ 43, 2999957, 2999999 }$$$. In the second sample, $$$x = 2 cdot 3 cdot 2 cdot 3 cdot 2 cdot 2 = 144$$$ and one of the maximum good subsets is $$${ 9, 12, 16 }$$$. | 2,600 | true | true | false | false | false | false | false | false | false | false | 4,395 |
431E | One day two students, Grisha and Diana, found themselves in the university chemistry lab. In the lab the students found _n_ test tubes with mercury numbered from 1 to _n_ and decided to conduct an experiment. The experiment consists of _q_ steps. On each step, one of the following actions occurs: 1. Diana pours all the contents from tube number _p__i_ and then pours there exactly _x__i_ liters of mercury. 2. Let's consider all the ways to add _v__i_ liters of water into the tubes; for each way let's count the volume of liquid (water and mercury) in the tube with water with maximum amount of liquid; finally let's find the minimum among counted maximums. That is the number the students want to count. At that, the students don't actually pour the mercury. They perform calculations without changing the contents of the tubes. Unfortunately, the calculations proved to be too complex and the students asked you to help them. Help them conduct the described experiment. Input The first line contains two integers _n_ and _q_ (1u2009β€u2009_n_,u2009_q_u2009β€u2009105) β the number of tubes ans the number of experiment steps. The next line contains _n_ space-separated integers: _h_1,u2009_h_2,u2009...,u2009_h__n_ (0u2009β€u2009_h__i_u2009β€u2009109), where _h__i_ is the volume of mercury in the _Ρ_-th tube at the beginning of the experiment. The next _q_ lines contain the game actions in the following format: A line of form "1 _p__i_ _x__i_" means an action of the first type (1u2009β€u2009_p__i_u2009β€u2009_n_;xa00u2009β€u2009_x__i_u2009β€u2009109). A line of form "2 _v__i_" means an action of the second type (1u2009β€u2009_v__i_u2009β€u20091015). It is guaranteed that there is at least one action of the second type. It is guaranteed that all numbers that describe the experiment are integers. Output For each action of the second type print the calculated value. The answer will be considered correct if its relative or absolute error doesn't exceed 10u2009-u20094. Examples Input 4 5 1 3 0 1 2 3 2 1 1 3 2 2 3 2 4 Output 1.66667 1.00000 2.33333 2.66667 | 2,200 | false | false | false | false | true | false | false | true | false | false | 8,115 |
1491D | There is a new attraction in Singapore Zoo: The Infinite Zoo. The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled $$$1,2,3,ldots$$$. There is a directed edge from vertex $$$u$$$ to vertex $$$u+v$$$ if and only if $$$u&v=v$$$, where $$$&$$$ denotes the β the number of queries. The $$$i$$$-th of the next $$$q$$$ lines will contain two integers $$$u_i$$$, $$$v_i$$$ ($$$1 leq u_i, v_i < 2^{30}$$$) β a query made by Zookeeper. Output For the $$$i$$$-th of the $$$q$$$ queries, output "YES" in a single line if Zookeeper can travel from vertex $$$u_i$$$ to vertex $$$v_i$$$. Otherwise, output "NO". You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer. Example Input 5 1 4 3 6 1 6 6 2 5 5 Note The subgraph on vertices $$$1,2,3,4,5,6$$$ is shown below. | 1,800 | true | true | false | true | false | true | false | false | false | false | 3,219 |
1282A | Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute. On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwisexa0β no. The base station can be located both on the route of Polycarp and outside it. Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speedxa0β one unit of distance per minute. Input The first line contains a positive integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 le a,b,c le 10^8$$$, $$$0 le r le 10^8$$$)xa0β the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it. Output Print $$$t$$$ numbersxa0β answers to given test cases in the order they are written in the test. Each answer is an integerxa0β the number of minutes during which Polycarp will be unavailable during his movement. Example Input 9 1 10 7 1 3 3 3 0 8 2 10 4 8 2 10 100 -10 20 -17 2 -3 2 2 0 -3 1 2 0 2 3 2 3 -1 3 -2 2 Note The following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area. | 900 | true | false | true | false | false | false | false | false | false | false | 4,286 |
309E | Information technologies are developing and are increasingly penetrating into all spheres of human activity. Incredible as it is, the most modern technology are used in farming! A large farm has a meadow with grazing sheep. Overall there are _n_ sheep and each of them contains a unique number from 1 to _n_ β because the sheep need to be distinguished and you need to remember information about each one, and they are so much alike! The meadow consists of infinite number of regions numbered from 1 to infinity. It's known that sheep _i_ likes regions from _l__i_ to _r__i_. There are two shepherds taking care of the sheep: First and Second. First wakes up early in the morning and leads the sheep graze on the lawn. Second comes in the evening and collects all the sheep. One morning, First woke up a little later than usual, and had no time to lead the sheep graze on the lawn. So he tied together every two sheep if there is a region they both like. First thought that it would be better β Second would have less work in the evening, because sheep won't scatter too much, being tied to each other! In the evening Second came on the lawn, gathered the sheep and tried to line them up in a row. But try as he might, the sheep wouldn't line up as Second want! Second had neither the strength nor the ability to untie the sheep so he left them as they are, but with one condition: he wanted to line up the sheep so that the maximum distance between two tied sheep was as small as possible. The distance between the sheep is the number of sheep in the ranks that are between these two. Help Second find the right arrangement. Output In the single output line print _n_ space-separated numbers β the sought arrangement of the sheep. The _i_-th value in the line must represent the number of the sheep that took the _i_-th place from left in the optimal arrangement line. If there are multiple optimal arrangements, print any of them. | 2,900 | false | true | false | false | false | false | false | true | false | false | 8,603 |
1384A | The length of the longest common prefix of two strings $$$s = s_1 s_2 ldots s_n$$$ and $$$t = t_1 t_2 ldots t_m$$$ is defined as the maximum integer $$$k$$$ ($$$0 le k le min(n,m)$$$) such that $$$s_1 s_2 ldots s_k$$$ equals $$$t_1 t_2 ldots t_k$$$. Koa the Koala initially has $$$n+1$$$ strings $$$s_1, s_2, dots, s_{n+1}$$$. For each $$$i$$$ ($$$1 le i le n$$$) she calculated $$$a_i$$$xa0β the length of the longest common prefix of $$$s_i$$$ and $$$s_{i+1}$$$. Several days later Koa found these numbers, but she couldn't remember the strings. So Koa would like to find some strings $$$s_1, s_2, dots, s_{n+1}$$$ which would have generated numbers $$$a_1, a_2, dots, a_n$$$. Can you help her? If there are many answers print any. We can show that answer always exists for the given constraints. Input Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 le t le 100$$$)xa0β the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0β the number of elements in the list $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 50$$$)xa0β the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$. Output For each test case: Output $$$n+1$$$ lines. In the $$$i$$$-th line print string $$$s_i$$$ ($$$1 le s_i le 200$$$), consisting of lowercase Latin letters. Length of the longest common prefix of strings $$$s_i$$$ and $$$s_{i+1}$$$ has to be equal to $$$a_i$$$. If there are many answers print any. We can show that answer always exists for the given constraints. Example Input 4 4 1 2 4 2 2 5 3 3 1 3 1 3 0 0 0 Output aeren ari arousal around ari monogon monogamy monthly kevinvu kuroni kurioni korone anton loves adhoc problems Note In the $$$1$$$-st test case one of the possible answers is $$$s = [aeren, ari, arousal, around, ari]$$$. Lengths of longest common prefixes are: Between $$$color{red}{a}eren$$$ and $$$color{red}{a}ri$$$ $$$ ightarrow 1$$$ Between $$$color{red}{ar}i$$$ and $$$color{red}{ar}ousal$$$ $$$ ightarrow 2$$$ Between $$$color{red}{arou}sal$$$ and $$$color{red}{arou}nd$$$ $$$ ightarrow 4$$$ Between $$$color{red}{ar}ound$$$ and $$$color{red}{ar}i$$$ $$$ ightarrow 2$$$ | 1,200 | false | true | false | false | false | true | false | false | false | false | 3,750 |
1658D2 | This is the hard version of the problem. The difference in the constraints between both versions are colored below in red. You can make hacks only if all versions of the problem are solved. Marin and Gojou are playing hide-and-seek with an array. Gojou initially perform the following steps: First, Gojou chooses $$$2$$$ integers $$$l$$$ and $$$r$$$ such that $$$l leq r$$$. Then, Gojou will make an array $$$a$$$ of length $$$r-l+1$$$ which is a permutation of the array $$$ β the number of test cases. In the first line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$color{red}{boldsymbol{0} boldsymbol{le} boldsymbol{l}} le r < 2^{17}$$$). The second line contains $$$r - l + 1$$$ space-seperated integers of $$$a_1,a_2,ldots,a_{r-l+1}$$$ ($$$0 le a_i < 2^{17}$$$). It is guaranteed that array $$$a$$$ is valid. It is guaranteed that the sum of $$$r - l + 1$$$ over all test cases does not exceed $$$2^{17}$$$. Output For each test case print an integer $$$x$$$. If there are multiple answers, print any. Example Input 3 4 7 3 2 1 0 4 7 4 7 6 5 1 3 0 2 1 Note In the first test case, the original array is $$$[7, 6, 5, 4]$$$. In the second test case, the original array is $$$[4, 7, 6, 5]$$$. In the third test case, the original array is $$$[3, 1, 2]$$$. | 2,300 | true | false | false | false | true | false | true | false | false | false | 2,335 |
1842D | Tell a story about me and my animal friends. Tenzing has $$$n$$$ animal friends. He numbers them from $$$1$$$ to $$$n$$$. One day Tenzing wants to play with his animal friends. To do so, Tenzing will host several games. In one game, he will choose a set $$$S$$$ which is a subset of $$${1,2,3,...,n}$$$ and choose an integer $$$t$$$. Then, he will play the game with the animals in $$$S$$$ for $$$t$$$ minutes. But there are some restrictions: 1. Tenzing loves friend $$$1$$$ very much, so $$$1$$$ must be an element of $$$S$$$. 2. Tenzing doesn't like friend $$$n$$$, so $$$n$$$ must not be an element of $$$S$$$. 3. There are m additional restrictions. The $$$i$$$-th special restriction is described by integers $$$u_i$$$, $$$v_i$$$ and $$$y_i$$$, suppose $$$x$$$ is the total time that exactly one of $$$u_i$$$ and $$$v_i$$$ is playing with Tenzing. Tenzing must ensure that $$$x$$$ is less or equal to $$$y_i$$$. Otherwise, there will be unhappiness. Tenzing wants to know the maximum total time that he can play with his animal friends. Please find out the maximum total time that Tenzing can play with his animal friends and a way to organize the games that achieves this maximum total time, or report that he can play with his animal friends for an infinite amount of time. Also, Tenzing does not want to host so many games, so he will host at most $$$n^2$$$ games. Input The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 100$$$, $$$0 leq m leq frac{n(n-1)}{2}$$$)xa0β the number of animal friends and the number of special restrictions. The $$$i$$$-th of the following $$$m$$$ lines of input contains three integers $$$u_i$$$, $$$v_i$$$ and $$$y_i$$$ ($$$1leq u_i<v_ileq n$$$, $$$0leq y_ileq 10^9$$$)xa0β describing the $$$i$$$-th special restriction. It is guaranteed that for $$$1 leq i < j leq m$$$, $$$(u_i,v_i) eq (u_j,v_j)$$$. Output If Tenzing can play with his animal friends for an infinite amount of time, output "inf". (Output without quotes.) Otherwise, in the first line, output the total time $$$T$$$ ($$$0 leq t leq 10^{18}$$$) and the number of games $$$k$$$ ($$$0 leq k leq n^2$$$). In the following $$$k$$$ lines of output, output a binary string $$$s$$$ of length $$$n$$$ and an integer $$$t$$$ ($$$0 leq t leq 10^{18}$$$)xa0β representing the set $$$S$$$ and the number of minutes this game will be played. If $$$s_i= exttt{1}$$$, then $$$i in S$$$, otherwise if $$$s_i= exttt{0}$$$, then $$$i otin S$$$. Under the constraints of this problem, it can be proven that if Tenzing can only play with his friends for a finite amount of time, then he can only play with them for at most $$$10^{18}$$$ minutes. Examples Input 5 4 1 3 2 1 4 2 2 3 1 2 5 1 Output 4 4 10000 1 10010 1 10100 1 11110 1 Note In the first test case: 1. Tenzing will host a game with friend $$${1}$$$ for $$$1$$$ minute. 2. Tenzing will host a game with friends $$${1,4}$$$ for $$$1$$$ minute. 3. Tenzing will host a game with friends $$${1,3}$$$ for $$$1$$$ minute. 4. Tenzing will host a game with friends $$${1,2,3,4}$$$ for $$$1$$$ minute. If after that, Tenzing host another game with friends $$${1,2}$$$ for $$$1$$$ minute. Then the time of exactly one of friends $$$2$$$ or $$$3$$$ with Tenzing will becomes $$$2$$$ minutes which will not satisfy the $$$3$$$-rd special restriction. In the second test case, there is no special restrictions. So Tenzing can host a game with friend $$${1}$$$ for an infinite amount of time. | 1,900 | false | true | false | false | false | true | false | false | false | true | 1,236 |
506E | Problem - 506E - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags combinatorics dp matrices strings *3000 No tag edit access β Contest materials You can choose any _n_ lowercase English letters, and insert each of them to any position of _s_, possibly to the beginning or the end of _s_. You have to insert exactly _n_ letters even if it is possible to turn _s_ into a palindrome by inserting less than _n_ letters. Find the number of the palindromes that can be obtained in this way, modulo 10007. Input The first line contains a string _s_ (1u2009β€u2009_s_u2009β€u2009200). Each character in _s_ is a lowercase English letter. The second line contains an integer _n_ (1u2009β€u2009_n_u2009β€u2009109). Output Print the number of the palindromes that can be obtained, modulo 10007. Examples Input revive 1 Output 1 Input add 2 Output 28 Note For the first sample, you can obtain the palindrome "reviver" by inserting 'r' to the end of "revive". For the second sample, the following 28 palindromes can be obtained: "adada", "adbda", ..., "adzda", "dadad" and "ddadd". | 3,000 | false | false | false | true | false | false | false | false | false | false | 7,817 |
60D | Problem - 60D - Codeforces =============== xa0 can transfer laugh to other lawn (say, _j_) if there exists an integer (say, _b_) such, that some permutation of numbers _a_ | 2,500 | true | false | false | false | false | false | true | false | false | false | 9,672 |
1034E | Little C loves number Β«3Β» very much. He loves all things about it. Now he is interested in the following problem: There are two arrays of $$$2^n$$$ intergers $$$a_0,a_1,...,a_{2^n-1}$$$ and $$$b_0,b_1,...,b_{2^n-1}$$$. The task is for each $$$i (0 leq i leq 2^n-1)$$$, to calculate $$$c_i=sum a_j cdot b_k$$$ ($$$jk=i$$$ and $$$j&k=0$$$, where "$$$$$$" denotes $$$, such that $$$jk=i$$$, $$$j&k=0$$$ and $$$0 leq i,j,k leq 2^n-1$$$. So Little C wants to solve this excellent problem (because it's well related to $$$3$$$) excellently. Help him calculate all $$$c_i$$$. Little C loves $$$3$$$ very much, so he only want to know each $$$c_i & 3$$$. Input The first line contains one integer $$$n (0 leq n leq 21)$$$. The second line contains $$$2^n$$$ integers in $$$[0,3]$$$ without spaces β the $$$i$$$-th of them is $$$a_{i-1}$$$. The third line contains $$$2^n$$$ integers in $$$[0,3]$$$ without spaces β the $$$i$$$-th of them is $$$b_{i-1}$$$. Output Print one line contains $$$2^n$$$ integers in $$$[0,3]$$$ without spaces β the $$$i$$$-th of them is $$$c_{i-1}&3$$$. (It's obvious that $$$c_{i}&3$$$ is in $$$[0,3]$$$). | 3,200 | true | false | false | true | false | false | false | false | false | false | 5,524 |
710C | . Output Print _n_ lines with _n_ integers. All the integers should be different and from 1 to _n_2. The sum in each row, column and both main diagonals should be odd. | 1,500 | true | false | false | false | false | true | false | false | false | false | 6,980 |
1968C | You are given an array $$$x_2,x_3,dots,x_n$$$. Your task is to find any array $$$a_1,dots,a_n$$$, where: $$$1le a_ile 10^9$$$ for all $$$1le ile n$$$. $$$x_i=a_i bmod a_{i-1}$$$ for all $$$2le ile n$$$. Here $$$cbmod d$$$ denotes the remainder of the division of the integer $$$c$$$ by the integer $$$d$$$. For example $$$5 bmod 2 = 1$$$, $$$72 bmod 3 = 0$$$, $$$143 bmod 14 = 3$$$. Note that if there is more than one $$$a$$$ which satisfies the statement, you are allowed to find any. Input The first line contains a single integer $$$t$$$ $$$(1le tle 10^4)$$$xa0β the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(2le nle 500)$$$xa0β the number of elements in $$$a$$$. The second line of each test case contains $$$n-1$$$ integers $$$x_2,dots,x_n$$$ $$$(1le x_ile 500)$$$xa0β the elements of $$$x$$$. It is guaranteed that the sum of values $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case output any $$$a_1,dots,a_n$$$ ($$$1 le a_i le 10^9$$$) which satisfies the statement. Example Input 5 4 2 4 1 3 1 1 6 4 2 5 1 2 2 500 3 1 5 Output 3 5 4 9 2 5 11 5 14 16 5 11 24 501 500 2 7 5 Note In the first test case $$$a=[3,5,4,9]$$$ satisfies the conditions, because: $$$a_2bmod a_1=5bmod 3=2=x_2$$$; $$$a_3bmod a_2=4bmod 5=4=x_3$$$; $$$a_4bmod a_3=9bmod 4=1=x_4$$$; | 1,000 | false | false | false | false | false | true | false | false | false | false | 498 |
1806A | YunQian is standing on an infinite plane with the Cartesian coordinate system on it. In one move, she can move to the diagonally adjacent point on the top right or the adjacent point on the left. That is, if she is standing on point $$$(x,y)$$$, she can either move to point $$$(x+1,y+1)$$$ or point $$$(x-1,y)$$$. YunQian initially stands at point $$$(a,b)$$$ and wants to move to point $$$(c,d)$$$. Find the minimum number of moves she needs to make or declare that it is impossible. Input The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0β the number of test cases. The description of test cases follows. The first line and only line of each test case contain four integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$-10^8le a,b,c,dle 10^8$$$). Output For each test case, if it is possible to move from point $$$(a,b)$$$ to point $$$(c,d)$$$, output the minimum number of moves. Otherwise, output $$$-1$$$. Example Input 6 -1 0 -1 2 0 0 4 5 -2 -1 1 1 -3 2 -3 2 2 -1 -1 -1 1 1 0 2 Note In the first test case, one possible way using $$$4$$$ moves is $$$(-1,0) o (0,1) o (-1,1) o (0,2) o (-1,2)$$$. It can be proven that it is impossible to move from point $$$(-1,0)$$$ to point $$$(-1,2)$$$ in less than $$$4$$$ moves. | 800 | true | true | false | false | false | false | false | false | false | false | 1,442 |
932G | Problem - 932G - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags dp string suffix structures strings *2900 No tag edit access β Contest materials in partition, then _p__i_u2009=u2009_p__k_u2009-u2009_i_u2009+u20091 for all _i_ (1u2009β€u2009_i_u2009β€u2009_k_) and _k_ is even. Since the number of ways can be large, print it modulo 109u2009+u20097. Input The only line of input contains a string _s_ (2u2009β€u2009_s_u2009β€u2009106) of even length consisting of lowercase Latin letters. Output Print one integer, the number of ways of partitioning the string modulo 109u2009+u20097. Examples Input abcdcdab Output 1 Input abbababababbab Output 3 Note In the first case, the only way to partition the string is _ab__cd__cd__ab_. In the second case, the string can be partitioned as _ab__b__ab__ab__ab__ab__b__ab_ or _ab__b__abab__abab__b__ab_ or _abbab__ab__ab__abbab_. | 2,900 | false | false | false | true | false | false | false | false | false | false | 5,972 |
648D | Statement is not available on English language ΠΠ° ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ½ΠΎΠΉ ΠΏΡΡΠΌΠΎΠΉ ΡΠΈΠ΄ΠΈΡ _n_ ΡΠΎΠ±Π°ΡΠ΅ΠΊ, _i_-Ρ ΡΠΎΠ±Π°ΡΠΊΠ° Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π² ΡΠΎΡΠΊΠ΅ _x__i_. ΠΡΠΎΠΌΠ΅ ΡΠΎΠ³ΠΎ, Π½Π° ΠΏΡΡΠΌΠΎΠΉ Π΅ΡΡΡ _m_ ΠΌΠΈΡΠΎΠΊ Ρ Π΅Π΄ΠΎΠΉ, Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΠ·Π²Π΅ΡΡΠ½Π° Π΅Ρ ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° Π½Π° ΠΏΡΡΠΌΠΎΠΉ _u__j_ ΠΈ Π²ΡΠ΅ΠΌΡ _t__j_, ΡΠ΅ΡΠ΅Π· ΠΊΠΎΡΠΎΡΠΎΠ΅ Π΅Π΄Π° Π² ΠΌΠΈΡΠΊΠ΅ ΠΎΡΡΡΠ½Π΅Ρ ΠΈ ΡΡΠ°Π½Π΅Ρ Π½Π΅Π²ΠΊΡΡΠ½ΠΎΠΉ. ΠΡΠΎ Π·Π½Π°ΡΠΈΡ, ΡΡΠΎ Π΅ΡΠ»ΠΈ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΡΠΈΠ±Π΅ΠΆΠΈΡ ΠΊ ΠΌΠΈΡΠΊΠ΅ Π² ΠΌΠΎΠΌΠ΅Π½Ρ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ, ΡΡΡΠΎΠ³ΠΎ Π±ΠΎΠ»ΡΡΠΈΠΉ _t__j_, ΡΠΎ Π΅Π΄Π° ΡΠΆΠ΅ ΠΎΡΡΡΠ½Π΅Ρ, ΠΈ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΊΡΡΠ°ΡΡ Π΅Ρ Π½Π΅ ΡΡΠ°Π½Π΅Ρ. Π‘ΡΠΈΡΠ°Ρ, ΡΡΠΎ ΠΊΠ°ΠΆΠ΄Π°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° Π±Π΅ΠΆΠΈΡ ΡΠΎ ΡΠΊΠΎΡΠΎΡΡΡΡ 1, Π½Π°ΠΉΠ΄ΠΈΡΠ΅ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΠ±Π°ΡΠ΅ΠΊ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠΌΠΎΠ³ΡΡ ΠΏΠΎΠΊΡΡΠ°ΡΡ. Π‘ΡΠΈΡΠ°ΠΉΡΠ΅, ΡΡΠΎ ΡΠΎΠ±Π°ΡΠΊΠΈ ΠΏΠΎΠ±Π΅Π³ΡΡ ΠΊ ΡΠ΅ΠΌ ΠΌΠΈΡΠΊΠ°ΠΌ, Π½Π° ΠΊΠΎΡΠΎΡΡΠ΅ Π²Ρ ΠΈΠΌ ΡΠΊΠ°ΠΆΠ΅ΡΠ΅. ΠΠ· ΠΎΠ΄Π½ΠΎΠΉ ΠΌΠΈΡΠΊΠΈ Π½Π΅ ΠΌΠΎΠ³ΡΡ ΠΊΡΡΠ°ΡΡ Π΄Π²Π΅ ΠΈΠ»ΠΈ Π±ΠΎΠ»Π΅Π΅ ΡΠΎΠ±Π°ΡΠΊΠΈ. Π‘ΠΎΠ±Π°ΡΠΊΠΈ ΠΌΠΎΠ³ΡΡ ΠΎΠ±Π³ΠΎΠ½ΡΡΡ Π΄ΡΡΠ³ Π΄ΡΡΠ³Π°, ΡΠΎ Π΅ΡΡΡ, Π΅ΡΠ»ΠΈ ΠΎΠ΄Π½Π° ΠΈΠ· Π½ΠΈΡ
ΠΎΡΡΠ°Π½ΠΎΠ²ΠΈΡΡΡ ΠΏΠΎΠΊΡΡΠ°ΡΡ, Π΄ΡΡΠ³Π°Ρ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΎΠΉΡΠΈ ΠΌΠΈΠΌΠΎ Π½Π΅Ρ, ΡΡΠΎΠ±Ρ ΠΏΠΎΠΏΠ°ΡΡΡ ΠΊ Π΄ΡΡΠ³ΠΎΠΉ ΠΌΠΈΡΠΊΠ΅. ΠΡ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅ Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ ΠΏΠ°ΡΠ° ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» _n_ ΠΈ _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009200u2009000)xa0β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΠ±Π°ΡΠ΅ΠΊ ΠΈ ΠΌΠΈΡΠΎΠΊ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ. ΠΠΎ Π²ΡΠΎΡΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ _n_ ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» _x__i_ (u2009-u2009109u2009β€u2009_x__i_u2009β€u2009109)xa0β ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° _i_-ΠΉ ΡΠΎΠ±Π°ΡΠΊΠΈ. Π ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
_m_ ΡΡΡΠΎΠΊΠ°Ρ
Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ ΠΏΠ°ΡΡ ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» _u__j_ ΠΈ _t__j_ (u2009-u2009109u2009β€u2009_u__j_u2009β€u2009109, 1u2009β€u2009_t__j_u2009β€u2009109)xa0β ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° _j_-ΠΉ ΠΌΠΈΡΠΊΠΈ ΠΈ Π²ΡΠ΅ΠΌΡ, ΠΊΠΎΠ³Π΄Π° ΠΎΡΡΡΠ½Π΅Ρ Π΅Π΄Π° Π² Π½Π΅ΠΉ, ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ. ΠΠ°ΡΠ°Π½ΡΠΈΡΡΠ΅ΡΡΡ, ΡΡΠΎ Π½ΠΈΠΊΠ°ΠΊΠΈΠ΅ Π΄Π²Π΅ ΡΠΎΠ±Π°ΡΠΊΠΈ Π½Π΅ Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ Π² ΠΎΠ΄Π½ΠΎΠΉ ΡΠΎΡΠΊΠ΅. ΠΠΈΠΊΠ°ΠΊΠΈΠ΅ Π΄Π²Π΅ ΠΌΠΈΡΠΊΠΈ ΡΠ°ΠΊΠΆΠ΅ Π½Π΅ ΠΌΠΎΠ³ΡΡ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡΡ Π² ΠΎΠ΄Π½ΠΎΠΉ ΡΠΎΡΠΊΠ΅. ΠΡΡ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅ ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ _a_xa0β ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΠ±Π°ΡΠ΅ΠΊ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠΌΠΎΠ³ΡΡ ΠΏΠΎΠΊΡΡΠ°ΡΡ. ΠΡΠΈΠΌΠ΅ΡΡ ΠΡ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅ 5 4 -2 0 4 8 13 -1 1 4 3 6 3 11 2 ΠΡ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅ 3 3 -1 3 7 1 1 4 1 7 1 ΠΡ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅ 4 4 20 1 10 30 1 1 2 5 22 2 40 10 ΠΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΠ΅ Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΏΠ΅ΡΠ²Π°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΠΎΠ±Π΅ΠΆΠΈΡ Π½Π°ΠΏΡΠ°Π²ΠΎ ΠΊ ΠΏΠ΅ΡΠ²ΠΎΠΉ ΠΌΠΈΡΠΊΠ΅, ΡΡΠ΅ΡΡΡ ΡΠΎΠ±Π°ΡΠΊΠ° ΡΡΠ°Π·Ρ Π½Π°ΡΠ½ΡΡ Π΅ΡΡΡ ΠΈΠ· Π²ΡΠΎΡΠΎΠΉ ΠΌΠΈΡΠΊΠΈ, ΡΠ΅ΡΠ²ΡΡΡΠ°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΠΎΠ±Π΅ΠΆΠΈΡ Π²Π»Π΅Π²ΠΎ ΠΊ ΡΡΠ΅ΡΡΠ΅ΠΉ ΠΌΠΈΡΠΊΠ΅, Π° ΠΏΡΡΠ°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΠΎΠ±Π΅ΠΆΠΈΡ Π²Π»Π΅Π²ΠΎ ΠΊ ΡΠ΅ΡΠ²ΡΡΡΠΎΠΉ ΠΌΠΈΡΠΊΠ΅. | 1,900 | false | true | false | false | true | false | false | false | true | false | 7,224 |
510D | Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also _n_ cards, each card has 2 attributes: length _l__i_ and cost _c__i_. If she pays _c__i_ dollars then she can apply _i_-th card. After applying _i_-th card she becomes able to make jumps of length _l__i_, i. e. from cell _x_ to cell (_x_u2009-u2009_l__i_) or cell (_x_u2009+u2009_l__i_). She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. If this is possible, calculate the minimal cost. Input The first line contains an integer _n_ (1u2009β€u2009_n_u2009β€u2009300), number of cards. The second line contains _n_ numbers _l__i_ (1u2009β€u2009_l__i_u2009β€u2009109), the jump lengths of cards. The third line contains _n_ numbers _c__i_ (1u2009β€u2009_c__i_u2009β€u2009105), the costs of cards. Output If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards. Examples Input 5 10 20 30 40 50 1 1 1 1 1 Input 7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10 Input 8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026 Note In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1. | 1,900 | true | false | false | true | false | false | true | false | false | false | 7,797 |
1253A | You're given two arrays $$$a[1 dots n]$$$ and $$$b[1 dots n]$$$, both of the same length $$$n$$$. In order to perform a push operation, you have to choose three integers $$$l, r, k$$$ satisfying $$$1 le l le r le n$$$ and $$$k > 0$$$. Then, you will add $$$k$$$ to elements $$$a_l, a_{l+1}, ldots, a_r$$$. For example, if $$$a = [3, 7, 1, 4, 1, 2]$$$ and you choose $$$(l = 3, r = 5, k = 2)$$$, the array $$$a$$$ will become $$$[3, 7, underline{3, 6, 3}, 2]$$$. You can do this operation at most once. Can you make array $$$a$$$ equal to array $$$b$$$? (We consider that $$$a = b$$$ if and only if, for every $$$1 le i le n$$$, $$$a_i = b_i$$$) Input The first line contains a single integer $$$t$$$ ($$$1 le t le 20$$$) β the number of test cases in the input. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100 000$$$) β the number of elements in each array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 1000$$$). The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Output For each test case, output one line containing "YES" if it's possible to make arrays $$$a$$$ and $$$b$$$ equal by performing at most once the described operation or "NO" if it's impossible. You can print each letter in any case (upper or lower). Example Input 4 6 3 7 1 4 1 2 3 7 3 6 3 2 5 1 1 1 1 1 1 2 1 3 1 2 42 42 42 42 1 7 6 Note The first test case is described in the statement: we can perform a push operation with parameters $$$(l=3, r=5, k=2)$$$ to make $$$a$$$ equal to $$$b$$$. In the second test case, we would need at least two operations to make $$$a$$$ equal to $$$b$$$. In the third test case, arrays $$$a$$$ and $$$b$$$ are already equal. In the fourth test case, it's impossible to make $$$a$$$ equal to $$$b$$$, because the integer $$$k$$$ has to be positive. | 1,000 | false | false | true | false | false | false | false | false | false | false | 4,422 |
117E | You are given an undirected connected graph _G_ consisting of _n_ vertexes and _n_ edges. _G_ contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off. You are also given _m_ queries represented as (_v_,u2009_u_) β change the state of all edges on the shortest path from vertex _v_ to vertex _u_ in graph _G_. If there are several such paths, the lexicographically minimal one is chosen. More formally, let us consider all shortest paths from vertex _v_ to vertex _u_ as the sequences of vertexes _v_,u2009_v_1,u2009_v_2,u2009...,u2009_u_. Among such sequences we choose the lexicographically minimal one. After each query you should tell how many connected components has the graph whose vertexes coincide with the vertexes of graph _G_ and edges coincide with the switched on edges of graph _G_. Input The first line contains two integers _n_ and _m_ (3u2009β€u2009_n_u2009β€u2009105, 1u2009β€u2009_m_u2009β€u2009105). Then _n_ lines describe the graph edges as _a_ _b_ (1u2009β€u2009_a_,u2009_b_u2009β€u2009_n_). Next _m_ lines contain the queries as _v_ _u_ (1u2009β€u2009_v_,u2009_u_u2009β€u2009_n_). It is guaranteed that the graph is connected, does not have any self-loops or multiple edges. Output Print _m_ lines, each containing one integer β the query results. Examples Input 5 2 2 1 4 3 2 4 2 5 4 1 5 4 1 5 Input 6 2 4 6 4 3 1 2 6 5 1 5 1 4 2 5 2 6 Note Let's consider the first sample. We'll highlight the switched on edges blue on the image. The graph before applying any operations. No graph edges are switched on, that's why there initially are 5 connected components. The graph after query _v_u2009=u20095,u2009_u_u2009=u20094. We can see that the graph has three components if we only consider the switched on edges. The graph after query _v_u2009=u20091,u2009_u_u2009=u20095. We can see that the graph has three components if we only consider the switched on edges. Lexicographical comparison of two sequences of equal length of _k_ numbers should be done as follows. Sequence _x_ is lexicographically less than sequence _y_ if exists such _i_ (1u2009β€u2009_i_u2009β€u2009_k_), so that _x__i_u2009<u2009_y__i_, and for any _j_ (1u2009β€u2009_j_u2009<u2009_i_) _x__j_u2009=u2009_y__j_. | 2,900 | false | false | true | false | true | false | false | false | false | false | 9,417 |
1227C | You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence $$$s=s_{1}s_{2}dots s_{n}$$$ of length $$$n$$$. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choose any consecutive substring of $$$s$$$ and reverse it. In other words, you can choose any substring $$$s[l dots r]=s_l, s_{l+1}, dots, s_r$$$ and change the order of elements in it into $$$s_r, s_{r-1}, dots, s_{l}$$$. For example, if you will decide to reverse substring $$$s[2 dots 4]$$$ of string $$$s=$$$"((()))" it will be equal to $$$s=$$$"()(())". A regular (aka balanced) bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. A prefix of a string $$$s$$$ is a substring that starts at position $$$1$$$. For example, for $$$s=$$$"(())()" there are $$$6$$$ prefixes: "(", "((", "(()", "(())", "(())(" and "(())()". In your opinion, a neat and clean room $$$s$$$ is a bracket sequence that: the whole string $$$s$$$ is a regular bracket sequence; and there are exactly $$$k$$$ prefixes of this sequence which are regular (including whole $$$s$$$ itself). For example, if $$$k = 2$$$, then "(())()" is a neat and clean room. You want to use at most $$$n$$$ operations to make your room neat and clean. Operations are applied one after another sequentially. It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in $$$n$$$ or less operations. Input The first line contains integer number $$$t$$$ ($$$1 le t le 100$$$)xa0β the number of test cases in the input. Then $$$t$$$ test cases follow. The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le frac{n}{2}, 2 le n le 2000$$$, $$$n$$$ is even)xa0β length of $$$s$$$ and required number of regular prefixes. The second line of a test case contains $$$s$$$ of length $$$n$$$xa0β the given bracket sequence. It contains only '(' and ')'. It is guaranteed that there are exactly $$$frac{n}{2}$$$ characters '(' and exactly $$$frac{n}{2}$$$ characters ')' in the given string. The sum of all values $$$n$$$ over all the test cases in the input doesn't exceed $$$2000$$$. Output For each test case print an answer. In the first line print integer $$$m$$$ ($$$0 le m le n$$$)xa0β the number of operations. You do not need to minimize $$$m$$$, any value is suitable. In the following $$$m$$$ lines print description of the operations, each line should contain two integers $$$l,r$$$ ($$$1 le l le r le n$$$), representing single reverse operation of $$$s[l dots r]=s_{l}s_{l+1}dots s_{r}$$$. Operations are applied one after another sequentially. The final $$$s$$$ after all operations should be a regular, also it should be exactly $$$k$$$ prefixes (including $$$s$$$) which are regular. It is guaranteed that the answer exists. If there are several possible answers you can print any. Note In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change $$$s$$$). | 1,700 | false | false | false | false | false | true | false | false | false | false | 4,542 |
1862F | A portal of dark forces has opened at the border of worlds, and now the whole world is under a terrible threat. To close the portal and save the world, you need to defeat $$$n$$$ monsters that emerge from the portal one after another. Only the sorceress Vika can handle this. She possesses two magical powersxa0β water magic and fire magic. In one second, Vika can generate $$$w$$$ units of water mana and $$$f$$$ units of fire mana. She will need mana to cast spells. Initially Vika have $$$0$$$ units of water mana and $$$0$$$ units of fire mana. Each of the $$$n$$$ monsters that emerge from the portal has its own strength, expressed as a positive integer. To defeat the $$$i$$$-th monster with strength $$$s_i$$$, Vika needs to cast a water spell or a fire spell of at least the same strength. In other words, Vika can spend at least $$$s_i$$$ units of water mana on a water spell, or at least $$$s_i$$$ units of fire mana on a fire spell. Vika can create and cast spells instantly. Vika can cast an unlimited number of spells every second as long she has enough mana for that. The sorceress wants to save the world as quickly as possible, so tell her how much time she will need. Input Each test consists of several test cases. The first line of each test contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0β the number of test cases. This is followed by a description of the test cases. The first line of each test case contains two integers $$$w$$$, $$$f$$$ ($$$1 le w, f le 10^9$$$)xa0β the amount of water and fire mana that Vika can generate per second. The second line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0β the number of monsters. The third line of each test case contains $$$n$$$ integers $$$s_1, s_2, s_3, dots, s_n$$$ ($$$1 le s_i le 10^4$$$)xa0β the strengths of the monsters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$. Output For each test case, output a single integerxa0β the minimum time in seconds that Vika will need to defeat all the monsters. Example Input 4 2 3 3 2 6 7 37 58 1 93 190 90 2 23 97 13 4 4 10 10 2 45 Note In the first sample, after the first second, Vika can spend $$$2$$$ units of fire mana to kill the first monster. Then she has $$$2$$$ units of water mana and $$$1$$$ unit of fire mana. After the third second, she will have $$$6$$$ units of water mana and $$$7$$$ units of fire mana at her disposal. This will be enough to immediately kill the second and third monsters. | 1,800 | false | false | false | true | false | false | true | true | false | false | 1,108 |
156B | As Sherlock Holmes was investigating a crime, he identified _n_ suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to _n_. After that, he asked each one: "Which one committed the crime?". Suspect number _i_ answered either "The crime was committed by suspect number _a__i_", or "Suspect number _a__i_ didn't commit the crime". Also, the suspect could say so about himself (_a__i_u2009=u2009_i_). Sherlock Holmes understood for sure that exactly _m_ answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers _n_ and _m_ (1u2009β€u2009_n_u2009β€u2009105,u20090u2009β€u2009_m_u2009β€u2009_n_) β the total number of suspects and the number of suspects who told the truth. Next _n_ lines contain the suspects' answers. The _i_-th line contains either "+_a__i_" (without the quotes), if the suspect number _i_ says that the crime was committed by suspect number _a__i_, or "-_a__i_" (without the quotes), if the suspect number _i_ says that the suspect number _a__i_ didn't commit the crime (_a__i_ is an integer, 1u2009β€u2009_a__i_u2009β€u2009_n_). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly _m_ people told the truth. Output Print _n_ lines. Line number _i_ should contain "Truth" if suspect number _i_ has told the truth for sure. Print "Lie" if the suspect number _i_ lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Output Not defined Not defined Not defined Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one. | 1,600 | false | false | true | false | true | true | false | false | false | false | 9,251 |
1265E | Creatnx has $$$n$$$ mirrors, numbered from $$$1$$$ to $$$n$$$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $$$i$$$-th mirror will tell Creatnx that he is beautiful with probability $$$frac{p_i}{100}$$$ for all $$$1 le i le n$$$. Creatnx asks the mirrors one by one, starting from the $$$1$$$-st mirror. Every day, if he asks $$$i$$$-th mirror, there are two possibilities: The $$$i$$$-th mirror tells Creatnx that he is beautiful. In this case, if $$$i = n$$$ Creatnx will stop and become happy, otherwise he will continue asking the $$$i+1$$$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the $$$1$$$-st mirror again. You need to calculate xa0β the number of mirrors. The second line contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 leq p_i leq 100$$$). Output Print the answer modulo $$$998244353$$$ in a single line. Note In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability $$$frac{1}{2}$$$. So, the expected number of days until Creatnx becomes happy is $$$2$$$. | 2,100 | true | false | false | true | true | false | false | false | false | false | 4,372 |
1575H | The Winter holiday will be here soon. Mr. Chanek wants to decorate his house's wall with ornaments. The wall can be represented as a binary string $$$a$$$ of length $$$n$$$. His favorite nephew has another binary string $$$b$$$ of length $$$m$$$ ($$$m leq n$$$). Mr. Chanek's nephew loves the non-negative integer $$$k$$$. His nephew wants exactly $$$k$$$ occurrences of $$$b$$$ as substrings in $$$a$$$. However, Mr. Chanek does not know the value of $$$k$$$. So, for each $$$k$$$ ($$$0 leq k leq n - m + 1$$$), find the minimum number of elements in $$$a$$$ that have to be changed such that there are exactly $$$k$$$ occurrences of $$$b$$$ in $$$a$$$. A string $$$s$$$ occurs exactly $$$k$$$ times in $$$t$$$ if there are exactly $$$k$$$ different pairs $$$(p,q)$$$ such that we can obtain $$$s$$$ by deleting $$$p$$$ characters from the beginning and $$$q$$$ characters from the end of $$$t$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq m leq n leq 500$$$) β size of the binary string $$$a$$$ and $$$b$$$ respectively. The second line contains a binary string $$$a$$$ of length $$$n$$$. The third line contains a binary string $$$b$$$ of length $$$m$$$. Output Output $$$n - m + 2$$$ integers β the $$$(k+1)$$$-th integer denotes the minimal number of elements in $$$a$$$ that have to be changed so there are exactly $$$k$$$ occurrences of $$$b$$$ as a substring in $$$a$$$. Note For $$$k = 0$$$, to make the string $$$a$$$ have no occurrence of 101, you can do one character change as follows. 100101011 $$$ ightarrow$$$ 100100011 For $$$k = 1$$$, you can also change a single character. 100101011 $$$ ightarrow$$$ 100001011 For $$$k = 2$$$, no changes are needed. | 2,200 | false | false | false | true | false | false | false | false | false | false | 2,761 |
1537A | An array $$$b$$$ of length $$$k$$$ is called good if its arithmetic mean is equal to $$$1$$$. More formally, if $$$$$$frac{b_1 + cdots + b_k}{k}=1.$$$$$$ Note that the value $$$frac{b_1+cdots+b_k}{k}$$$ is not rounded up or down. For example, the array $$$[1,1,1,2]$$$ has an arithmetic mean of $$$1.25$$$, which is not equal to $$$1$$$. You are given an integer array $$$a$$$ of length $$$n$$$. In an operation, you can append a non-negative integer to the end of the array. What's the minimum number of operations required to make the array good? We have a proof that it is always possible with finitely many operations. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) β the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 50$$$) β the length of the initial array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,ldots,a_n$$$ ($$$-10^4leq a_i leq 10^4$$$), the elements of the array. Output For each test case, output a single integer β the minimum number of non-negative integers you have to append to the array so that the arithmetic mean of the array will be exactly $$$1$$$. Example Input 4 3 1 1 1 2 1 2 4 8 4 6 2 1 -2 Note In the first test case, we don't need to add any element because the arithmetic mean of the array is already $$$1$$$, so the answer is $$$0$$$. In the second test case, the arithmetic mean is not $$$1$$$ initially so we need to add at least one more number. If we add $$$0$$$ then the arithmetic mean of the whole array becomes $$$1$$$, so the answer is $$$1$$$. In the third test case, the minimum number of elements that need to be added is $$$16$$$ since only non-negative integers can be added. In the fourth test case, we can add a single integer $$$4$$$. The arithmetic mean becomes $$$frac{-2+4}{2}$$$ which is equal to $$$1$$$. | 800 | true | true | false | false | false | false | false | false | false | false | 2,962 |
733E | The academic year has just begun, but lessons and olympiads have already occupied all the free time. It is not a surprise that today Olga fell asleep on the Literature. She had a dream in which she was on a stairs. The stairs consists of _n_ steps. The steps are numbered from bottom to top, it means that the lowest step has number 1, and the highest step has number _n_. Above each of them there is a pointer with the direction (up or down) Olga should move from this step. As soon as Olga goes to the next step, the direction of the pointer (above the step she leaves) changes. It means that the direction "up" changes to "down", the direction "down" xa0βxa0 to the direction "up". Olga always moves to the next step in the direction which is shown on the pointer above the step. If Olga moves beyond the stairs, she will fall and wake up. Moving beyond the stairs is a moving down from the first step or moving up from the last one (it means the _n_-th) step. In one second Olga moves one step up or down according to the direction of the pointer which is located above the step on which Olga had been at the beginning of the second. For each step find the duration of the dream if Olga was at this step at the beginning of the dream. Olga's fall also takes one second, so if she was on the first step and went down, she would wake up in the next second. Input The first line contains single integer _n_ (1u2009β€u2009_n_u2009β€u2009106)xa0β the number of steps on the stairs. The second line contains a string _s_ with the length _n_xa0β it denotes the initial direction of pointers on the stairs. The _i_-th character of string _s_ denotes the direction of the pointer above _i_-th step, and is either 'U' (it means that this pointer is directed up), or 'D' (it means this pointed is directed down). The pointers are given in order from bottom to top. Output Print _n_ numbers, the _i_-th of which is equal either to the duration of Olga's dream or to u2009-u20091 if Olga never goes beyond the stairs, if in the beginning of sleep she was on the _i_-th step. | 2,400 | true | false | false | false | true | true | false | false | false | false | 6,860 |
1067D | Ivan plays some computer game. There are $$$n$$$ quests in the game. Each quest can be upgraded once, this increases the reward for its completion. Each quest has $$$3$$$ parameters $$$a_{i}$$$, $$$b_{i}$$$, $$$p_{i}$$$: reward for completing quest before upgrade, reward for completing quest after upgrade ($$$a_{i} < b_{i}$$$) and probability of successful completing the quest. Each second Ivan can try to complete one quest and he will succeed with probability $$$p_{i}$$$. In case of success Ivan will get the reward and opportunity to upgrade any one quest (not necessary the one he just completed). In case of failure he gets nothing. Quests do not vanish after completing. Ivan has $$$t$$$ seconds. He wants to maximize expected value of his total gain after $$$t$$$ seconds. Help him to calculate this value. Input First line contains $$$2$$$ integers $$$n$$$ ($$$ 1 le n le 10^{5}$$$) and $$$t$$$ ($$$ 1 le t le 10^{10}$$$)xa0β number of quests and total time. Following $$$n$$$ lines contain description of quests. Each description is $$$3$$$ numbers $$$a_{i}$$$ $$$b_{i}$$$ $$$p_{i}$$$ ($$$1 le a_{i} < b_{i} le 10^{8}$$$, $$$0 < p_{i} < 1$$$)xa0β reward for completing quest before upgrade, reward for completing quest after upgrade and probability of successful completing of quest. $$$a_{i}$$$ and $$$b_{i}$$$ are integers. All probabilities are given with at most $$$9$$$ decimal places. Output Print the expected value. Your answer will be accepted if absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is considered correct if $$$frac{a-b}{maxu2061(b, ,, 1)} le 10^{-6}$$$. Examples Input 3 2 3 1000 0.5 1 2 0.48 3 20 0.3 Input 2 2 1 1000 0.1 2 3 0.2 | 3,100 | true | true | false | true | false | false | false | false | false | false | 5,372 |
1493B | The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts $$$h$$$ hours and each hour lasts $$$m$$$ minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from $$$0$$$ to $$$h-1$$$ and minutes are numbered from $$$0$$$ to $$$m-1$$$. That's how the digits are displayed on the clock. Please note that digit $$$1$$$ is placed in the middle of its position. A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day). The image of the clocks in the mirror is reflected against a vertical axis. The reflection is not a valid time. The reflection is a valid time with $$$h=24$$$, $$$m = 60$$$. However, for example, if $$$h=10$$$, $$$m=60$$$, then the reflection is not a valid time. An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment $$$s$$$ and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid. It can be shown that with any $$$h$$$, $$$m$$$, $$$s$$$ such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest. You are asked to solve the problem for several test cases. Input The first line contains a single integer $$$T$$$ ($$$1 le T le 100$$$) β the number of test cases. The next $$$2 cdot T$$$ lines contain the description of test cases. The description of each test case consists of two lines. The first line of a test case contains two integers $$$h$$$, $$$m$$$ ($$$1 le h, m le 100$$$). The second line contains the start time $$$s$$$ in the described format HH:MM. | 1,300 | false | false | true | false | false | false | true | false | false | false | 3,207 |
819A | Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens. All characters in this game are lowercase English letters. There are two players: Mister B and his competitor. Initially the players have a string _s_ consisting of the first _a_ English letters in alphabetical order (for example, if _a_u2009=u20095, then _s_ equals to "abcde"). The players take turns appending letters to string _s_. Mister B moves first. Mister B must append exactly _b_ letters on each his move. He can arbitrary choose these letters. His opponent adds exactly _a_ letters on each move. Mister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string _s_ of length _a_ and generates a string _t_ of length _a_ such that all letters in the string _t_ are distinct and don't appear in the considered suffix. From multiple variants of _t_ lexicographically minimal is chosen (if _a_u2009=u20094 and the suffix is "bfdd", the computer chooses string _t_ equal to "aceg"). After that the chosen string _t_ is appended to the end of _s_. Mister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string _s_ on the segment between positions _l_ and _r_, inclusive. Letters of string _s_ are numerated starting from 1. Input First and only line contains four space-separated integers: _a_, _b_, _l_ and _r_ (1u2009β€u2009_a_,u2009_b_u2009β€u200912, 1u2009β€u2009_l_u2009β€u2009_r_u2009β€u2009109) β the numbers of letters each player appends and the bounds of the segment. Output Print one integer β the minimum possible number of different letters in the segment from position _l_ to position _r_, inclusive, in string _s_. Note In the first sample test one of optimal strategies generate string _s_u2009=u2009"abababab...", that's why answer is 2. In the second sample test string _s_u2009=u2009"abcdbcaefg..." can be obtained, chosen segment will look like "bcdbc", that's why answer is 3. In the third sample test string _s_u2009=u2009"abczzzacad..." can be obtained, chosen, segment will look like "zzz", that's why answer is 1. | 2,200 | false | true | false | false | false | false | false | false | false | false | 6,480 |
1239F | I'm the Map, I'm the Map! I'm the MAP!!! Map In anticipation of new adventures Boots wanted to do a good deed. After discussion with the Map and Backpack, they decided to gift Dora a connected graph. After a long search, Boots chose $$$t$$$ graph's variants, which Dora might like. However fox Swiper wants to spoil his plan. The Swiper knows, that Dora now is only able to count up to $$$3$$$, so he has came up with a following idea. He wants to steal some non-empty set of vertices, so that the Dora won't notice the loss. He has decided to steal some non-empty set of vertices, so that after deletion of the stolen vertices and edges adjacent to them, every remaining vertex wouldn't change it's degree modulo $$$3$$$. The degree of a vertex is the number of edges it is adjacent to. It would've been suspicious to steal all the vertices, so Swiper needs another plan. Boots are sure, that the crime can not be allowed. However they are afraid, that they won't be able to handle this alone. So Boots decided to ask for your help. Please determine for every graph's variant whether the Swiper can perform the theft or not. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100,000$$$) β the number of graph variants. The first line of each variant contains integers $$$n$$$, $$$m$$$ ($$$1 le n le 500,000$$$, $$$0 le m le 500,000$$$), the number of vertexes and edges in the graph. Then $$$m$$$ lines follow, each containing integers $$$a_i$$$, $$$b_i$$$ ($$$1 le a_i, b_i le n$$$), the indices of the vertices connected with a corresponding edge. It's guaranteed, that the graph is connected and doesn't contain multiple edges or self-loops. It's guaranteed, that the sum of $$$n$$$ over all variants is at most $$$500,000$$$ and that the sum of $$$m$$$ over all variants is at most $$$500,000$$$. Descriptions of graph's variants are separated with an empty line. Output For each variant: In case the answer exists, print "Yes" and then the answer itself.The first line should contain an integer $$$c$$$ ($$$1 < c < n$$$), the number of vertices the Crook can steal, without Dora noticing the loss. On the next line print $$$c$$$ distinct integers, the indices of the graph's vertices in arbitrary order. Otherwise print "No". In case there are several correct ways to steal the vertices, print any of them. Please note, that it's not required to maximize the number of stolen vertices. Example Input 3 3 3 1 2 2 3 3 1 6 6 1 2 1 3 2 3 2 5 2 6 2 4 8 12 1 2 1 3 2 3 1 4 4 5 5 1 3 6 3 7 3 8 6 1 7 1 8 1 Output No Yes 3 4 5 6 Yes 3 6 7 8 Note The picture below shows the third variant from the example test. The set of the vertices the Crook can steal is denoted with bold. | 3,400 | false | false | true | false | false | false | false | false | false | true | 4,491 |
997E | A permutation $$$p$$$ of length $$$n$$$ is a sequence $$$p_1, p_2, ldots, p_n$$$ consisting of $$$n$$$ distinct integers, each of which from $$$1$$$ to $$$n$$$ ($$$1 leq p_i leq n$$$) . Let's call the subsegment $$$[l,r]$$$ of the permutation good if all numbers from the minimum on it to the maximum on this subsegment occur among the numbers $$$p_l, p_{l+1}, dots, p_r$$$. For example, good segments of permutation $$$[1, 3, 2, 5, 4]$$$ are: $$$[1, 1]$$$, $$$[1, 3]$$$, $$$[1, 5]$$$, $$$[2, 2]$$$, $$$[2, 3]$$$, $$$[2, 5]$$$, $$$[3, 3]$$$, $$$[4, 4]$$$, $$$[4, 5]$$$, $$$[5, 5]$$$. You are given a permutation $$$p_1, p_2, ldots, p_n$$$. You need to answer $$$q$$$ queries of the form: find the number of good subsegments of the given segment of permutation. In other words, to answer one query, you need to calculate the number of good subsegments $$$[x dots y]$$$ for some given segment $$$[l dots r]$$$, such that $$$l leq x leq y leq r$$$. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 120000$$$)xa0β the number of elements in the permutation. The second line contains $$$n$$$ distinct integers $$$p_1, p_2, ldots, p_n$$$ separated by spaces ($$$1 leq p_i leq n$$$). The third line contains an integer $$$q$$$ ($$$1 leq q leq 120000$$$)xa0β number of queries. The following $$$q$$$ lines describe queries, each line contains a pair of integers $$$l$$$, $$$r$$$ separated by space ($$$1 leq l leq r leq n$$$). Output Print a $$$q$$$ lines, $$$i$$$-th of them should contain a number of good subsegments of a segment, given in the $$$i$$$-th query. | 3,000 | false | false | false | false | true | false | false | false | false | false | 5,714 |
1793D | In winter, the inhabitants of the Moscow Zoo are very bored, in particular, it concerns gorillas. You decided to entertain them and brought a permutation $$$p$$$ of length $$$n$$$ to the zoo. A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in any order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ occurs twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$, but $$$4$$$ is present in the array). The gorillas had their own permutation $$$q$$$ of length $$$n$$$. They suggested that you count the number of pairs of integers $$$l, r$$$ ($$$1 le l le r le n$$$) such that $$$operatorname{MEX}([p_l, p_{l+1}, ldots, p_r])=operatorname{MEX}([q_l, q_{l+1}, ldots, q_r])$$$. The $$$operatorname{MEX}$$$ of the sequence is the minimum integer positive number missing from this sequence. For example, $$$operatorname{MEX}([1, 3]) = 2$$$, $$$operatorname{MEX}([5]) = 1$$$, $$$operatorname{MEX}([3, 1, 2, 6]) = 4$$$. You do not want to risk your health, so you will not dare to refuse the gorillas. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β the permutations length. The second line contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$)xa0β the elements of the permutation $$$p$$$. The third line contains $$$n$$$ integers $$$q_1, q_2, ldots, q_n$$$ ($$$1 le q_i le n$$$)xa0β the elements of the permutation $$$q$$$. Note In the first example, two segments are correct β $$$[1, 3]$$$ with $$$operatorname{MEX}$$$ equal to $$$4$$$ in both arrays and $$$[3, 3]$$$ with $$$operatorname{MEX}$$$ equal to $$$1$$$ in both of arrays. In the second example, for example, the segment $$$[1, 4]$$$ is correct, and the segment $$$[6, 7]$$$ isn't correct, because $$$operatorname{MEX}(5, 4) eq operatorname{MEX}(1, 4)$$$. | 1,800 | true | true | true | true | false | false | false | true | false | false | 1,517 |
1017G | Abendsen assigned a mission to Juliana. In this mission, Juliana has a rooted tree with $$$n$$$ vertices. Vertex number $$$1$$$ is the root of this tree. Each vertex can be either black or white. At first, all vertices are white. Juliana is asked to process $$$q$$$ queries. Each query is one of three types: 1. If vertex $$$v$$$ is white, mark it as black; otherwise, perform this operation on all direct sons of $$$v$$$ instead. 2. Mark all vertices in the subtree of $$$v$$$ (including $$$v$$$) as white. 3. Find the color of the $$$i$$$-th vertex. An example of operation "1 1" (corresponds to the first example test). The vertices $$$1$$$ and $$$2$$$ are already black, so the operation goes to their sons instead. Can you help Juliana to process all these queries? Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2leq nleq 10^5$$$, $$$1leq qleq 10^5$$$)xa0β the number of vertices and the number of queries. The second line contains $$$n-1$$$ integers $$$p_2, p_3, ldots, p_n$$$ ($$$1leq p_i<i$$$), where $$$p_i$$$ means that there is an edge between vertices $$$i$$$ and $$$p_i$$$. Each of the next $$$q$$$ lines contains two integers $$$t_i$$$ and $$$v_i$$$ ($$$1leq t_ileq 3$$$, $$$1leq v_ileq n$$$)xa0β the type of the $$$i$$$-th query and the vertex of the $$$i$$$-th query. It is guaranteed that the given graph is a tree. Output For each query of type $$$3$$$, print "black" if the vertex is black; otherwise, print "white". Examples Input 8 10 1 2 1 2 5 4 5 1 2 3 2 3 1 1 1 1 1 3 5 3 7 3 4 2 2 3 5 Output black white black white black white Input 8 11 1 1 2 3 3 6 6 1 1 1 1 1 3 3 2 3 4 3 6 3 7 2 3 1 6 3 7 3 6 Output black white black white white black Note The first example is shown on the picture below. The second example is shown on the picture below. | 3,200 | false | false | false | false | true | false | false | false | false | false | 5,599 |
1717F | Oh no, on the first exam Madoka got this hard problem: Given integer $$$n$$$ and $$$m$$$ pairs of integers ($$$v_i, u_i$$$). Also there is an array $$$b_1, b_2, ldots, b_n$$$, initially filled with zeros. Then for each index $$$i$$$, where $$$1 leq i leq m$$$, perform either $$$b_{v_i} := b_{v_i} - 1$$$ and $$$b_{u_i} := b_{u_i} + 1$$$, or $$$b_{v_i} := b_{v_i} + 1$$$ and $$$b_{u_i} := b_{u_i} - 1$$$. Note that exactly one of these operations should be performed for every $$$i$$$. Also there is an array $$$s$$$ of length $$$n$$$ consisting of $$$0$$$ and $$$1$$$. And there is an array $$$a_1, a_2, ldots, a_n$$$, where it is guaranteed, that if $$$s_i = 0$$$ holds, then $$$a_i = 0$$$. Help Madoka and determine whenever it is possible to perform operations in such way that for every $$$i$$$, where $$$s_i = 1$$$ it holds that $$$a_i = b_i$$$. If it possible you should also provide Madoka with a way to perform operations. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 10000, 1 leq m leq 10000$$$) β the length of the array $$$a$$$ and the number of pair of integers. The second line contains $$$n$$$ integers $$$s_1, s_2, ldots s_n$$$ ($$$0 le s_i le 1$$$) β the elements of the array $$$s$$$. The third line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$a_i leq m$$$) β the elements of the array $$$a$$$. It is guaranteed that if $$$s_i = 0$$$ holds, then $$$a_i = 0$$$. $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 leq v_i, u_i leq n, v_i e u_i$$$) β the indexes of the elements of the array $$$b$$$ to which the operation is performed. It is also guaranteed that there are no two indices $$$i$$$ and $$$j$$$, where $$$1 le i < j le m$$$, such that $$$(v_i, u_i) = (v_j, u_j)$$$ or $$$(v_i, u_i) = (u_j, v_j)$$$. Output In the first line print "YES" if it is possible to perform operations in the required way, and "NO" otherwise. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer). In case you printed "YES", print $$$m$$$ pairs of integers. If for pair $$$(v_i, u_i)$$$ we should perform $$$b_{v_i} := b_{v_i} - 1$$$ and $$$b_{u_i} := b_{u_i} + 1$$$, print $$$(v_i, u_i)$$$. Otherwise print $$$(u_i, v_i)$$$. If there are multiple ways to get the correct answer, you can print any of them. You can print pairs in any order. Examples Input 5 5 1 1 1 1 1 -2 0 2 1 -1 1 5 1 4 3 5 3 4 4 5 Output YES 1 5 1 4 5 3 4 3 5 4 Input 5 5 0 1 0 1 0 0 1 0 0 0 1 3 2 3 3 5 3 4 4 5 Output YES 3 1 3 2 5 3 3 4 4 5 Input 4 4 1 1 1 1 0 2 -2 2 1 3 1 4 2 3 2 4 Note In the first example, the array $$$b$$$ will change as follows: $$$[0,0,0,0,0] ightarrow [-1,0,0,1,0] ightarrow [-2,0,0,1,1] ightarrow [-2,0,1,0,1] ightarrow [-2,0,2,0,0] ightarrow [-2,0,2,1,-1]$$$. $$$a_i = b_i$$$ for all indices $$$i$$$ from $$$1$$$ to $$$5$$$. In the second example, it is enough for us that $$$b_2 = 1$$$ at the end, since only $$$s_2 = 1$$$. In the third example, the operations cannot be performed as required. | 2,500 | false | false | true | false | false | true | false | false | false | true | 1,976 |
1268D | You are given a tournamentxa0β complete directed graph. In one operation you can pick any vertex $$$v$$$ and change the direction of all edges with $$$v$$$ on one of the ends (i.e all edges $$$u o v$$$ change their orientation to $$$v o u$$$ and vice versa). You want to make the tournament strongly connected with the smallest possible number of such operations if it is possible. Also, if it is possible, you need to find the number of ways to make this number of operations to make graph strongly connected (two ways are different if for some $$$i$$$ vertex that we chose on $$$i$$$-th operation in one way is different from vertex that we chose on $$$i$$$-th operation in another way). You only need to find this value modulo $$$998,244,353$$$. Input The first line of input contains one integer $$$n$$$ ($$$3 leq n leq 2000$$$): the number of vertices in the tournament. Following $$$n$$$ lines contain a description of the given tournament, each of them contains a binary string of length $$$n$$$. If $$$j$$$-th character of $$$i$$$-th string is equal to '1', then the graph has an edge $$$i o j$$$. It is guaranteed that there are no edges $$$i o i$$$ and the graph has exactly one edge among $$$i o j$$$ and $$$j o i$$$ for different $$$i$$$ and $$$j$$$. Output If it is not possible to convert tournament to strongly connected with the given operations, output "-1". Otherwise, output two integers: the smallest number of operations that you need to make the given graph strongly connected and the number of ways to do this number of operations to make graph strongly connected, modulo $$$998,244,353$$$. Examples Input 4 0010 1000 0100 1110 Input 6 010000 001000 100000 111001 111100 111010 | 3,200 | true | false | false | false | false | false | true | false | false | true | 4,348 |
77C | Eat a beaver, save a tree!" β That will be the motto of ecologists' urgent meeting in Beaverley Hills. And the whole point is that the population of beavers on the Earth has reached incredible sizes! Each day their number increases in several times and they don't even realize how much their unhealthy obsession with trees harms the nature and the humankind. The amount of oxygen in the atmosphere has dropped to 17 per cent and, as the best minds of the world think, that is not the end. In the middle of the 50-s of the previous century a group of soviet scientists succeed in foreseeing the situation with beavers and worked out a secret technology to clean territory. The technology bears a mysterious title "Beavermuncher-0xFF". Now the fate of the planet lies on the fragile shoulders of a small group of people who has dedicated their lives to science. The prototype is ready, you now need to urgently carry out its experiments in practice. You are given a tree, completely occupied by beavers. A tree is a connected undirected graph without cycles. The tree consists of _n_ vertices, the _i_-th vertex contains _k__i_ beavers. "Beavermuncher-0xFF" works by the following principle: being at some vertex _u_, it can go to the vertex _v_, if they are connected by an edge, and eat exactly one beaver located at the vertex _v_. It is impossible to move to the vertex _v_ if there are no beavers left in _v_. "Beavermuncher-0xFF" cannot just stand at some vertex and eat beavers in it. "Beavermuncher-0xFF" must move without stops. Why does the "Beavermuncher-0xFF" works like this? Because the developers have not provided place for the battery in it and eating beavers is necessary for converting their mass into pure energy. It is guaranteed that the beavers will be shocked by what is happening, which is why they will not be able to move from a vertex of the tree to another one. As for the "Beavermuncher-0xFF", it can move along each edge in both directions while conditions described above are fulfilled. The root of the tree is located at the vertex _s_. This means that the "Beavermuncher-0xFF" begins its mission at the vertex _s_ and it must return there at the end of experiment, because no one is going to take it down from a high place. Determine the maximum number of beavers "Beavermuncher-0xFF" can eat and return to the starting vertex. Output Print the maximum number of beavers munched by the "Beavermuncher-0xFF". Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d). | 2,100 | false | true | false | true | false | false | false | false | false | false | 9,578 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.