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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
475C | Problem - 475C - Codeforces =============== xa0 ]( "Bayan Contest Warm Up round + 50 tshirts!") . Then the brush is moved several times. Each time the brush is moved one unit right or down. The brush has been strictly inside the frame during the painting. The brush alters every cell it has covered at some moment. You have found one of the old Kamal-ol-molk's paintings. You want to know if it's possible that it has been altered in described manner. If yes, you also want to know minimum possible area of the brush. Input The first line of input contains two integers _n_ and _m_, (1u2009≤u2009_n_,u2009_m_u2009≤u20091000), denoting the height and width of the painting. The next _n_ lines contain the painting. Each line has _m_ characters. Character 'X' denotes an altered cell, otherwise it's showed by '.'. There will be at least one altered cell in the painting. Output Print the minimum area of the brush in a line, if the painting is possibly altered, otherwise print u2009-u20091. Examples Input 4 4 XX.. XX.. XXXX XXXX Output 4 Input 4 4 .... .XXX .XXX .... Output 2 Input 4 5 XXXX. XXXX. .XX.. .XX.. Output -1 | 2,100 | false | true | false | false | false | true | true | false | false | false | 7,932 |
386B | Everyone loves a freebie. Especially students. It is well-known that if in the night before exam a student opens window, opens the student's record-book and shouts loudly three times "Fly, freebie, fly!" — then flown freebie helps him to pass the upcoming exam. In the night before the exam on mathematical analysis _n_ students living in dormitory shouted treasured words. The _i_-th student made a sacrament at the time _t__i_, where _t__i_ is the number of seconds elapsed since the beginning of the night. It is known that the freebie is a capricious and willful lady. That night the freebie was near dormitory only for _T_ seconds. Therefore, if for two students their sacrament times differ for more than _T_, then the freebie didn't visit at least one of them. Since all students are optimists, they really want to know what is the maximal number of students visited by the freebie can be. Input The first line of the input contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100), where _n_ — the number of students shouted "Fly, freebie, fly!" The second line contains _n_ positive integers _t__i_ (1u2009≤u2009_t__i_u2009≤u20091000). The last line contains integer _T_ (1u2009≤u2009_T_u2009≤u20091000) — the time interval during which the freebie was near the dormitory. Output Print a single integer — the largest number of people who will pass exam tomorrow because of the freebie visit. | 1,400 | false | false | true | false | false | false | true | true | false | false | 8,285 |
1372E | Omkar is building a house. He wants to decide how to make the floor plan for the last floor. Omkar's floor starts out as $$$n$$$ rows of $$$m$$$ zeros ($$$1 le n,m le 100$$$). Every row is divided into intervals such that every $$$0$$$ in the row is in exactly $$$1$$$ interval. For every interval for every row, Omkar can change exactly one of the $$$0$$$s contained in that interval to a $$$1$$$. Omkar defines the quality of a floor as the sum of the squares of the sums of the values in each column, i. e. if the sum of the values in the $$$i$$$-th column is $$$q_i$$$, then the quality of the floor is $$$sum_{i = 1}^m q_i^2$$$. Help Omkar find the maximum quality that the floor can have. Input The first line contains two integers, $$$n$$$ and $$$m$$$ ($$$1 le n,m le 100$$$), which are the number of rows and number of columns, respectively. You will then receive a description of the intervals in each row. For every row $$$i$$$ from $$$1$$$ to $$$n$$$: The first row contains a single integer $$$k_i$$$ ($$$1 le k_i le m$$$), which is the number of intervals on row $$$i$$$. The $$$j$$$-th of the next $$$k_i$$$ lines contains two integers $$$l_{i,j}$$$ and $$$r_{i,j}$$$, which are the left and right bound (both inclusive), respectively, of the $$$j$$$-th interval of the $$$i$$$-th row. It is guaranteed that all intervals other than the first interval will be directly after the interval before it. Formally, $$$l_{i,1} = 1$$$, $$$l_{i,j} leq r_{i,j}$$$ for all $$$1 le j le k_i$$$, $$$r_{i,j-1} + 1 = l_{i,j}$$$ for all $$$2 le j le k_i$$$, and $$$r_{i,k_i} = m$$$. Output Output one integer, which is the maximum possible quality of an eligible floor plan. Example Input 4 5 2 1 2 3 5 2 1 3 4 5 3 1 1 2 4 5 5 3 1 1 2 2 3 5 Note The given test case corresponds to the following diagram. Cells in the same row and have the same number are a part of the same interval. The most optimal assignment is: The sum of the $$$1$$$st column is $$$4$$$, the sum of the $$$2$$$nd column is $$$2$$$, the sum of the $$$3$$$rd and $$$4$$$th columns are $$$0$$$, and the sum of the $$$5$$$th column is $$$4$$$. The quality of this floor plan is $$$4^2 + 2^2 + 0^2 + 0^2 + 4^2 = 36$$$. You can show that there is no floor plan with a higher quality. | 2,900 | false | true | false | true | false | false | false | false | false | false | 3,803 |
1817C | A polynomial $$$A(x)$$$ of degree $$$d$$$ is an expression of the form $$$A(x) = a_0 + a_1 x + a_2 x^2 + dots + a_d x^d$$$, where $$$a_i$$$ are integers, and $$$a_d eq 0$$$. Two polynomials $$$A(x)$$$ and $$$B(x)$$$ are called similar if there is an integer $$$s$$$ such that for any integer $$$x$$$ it holds that $$$$$$ B(x) equiv A(x+s) pmod{10^9+7}. $$$$$$ For two similar polynomials $$$A(x)$$$ and $$$B(x)$$$ of degree $$$d$$$, you're given their values in the points $$$x=0,1,dots, d$$$ modulo $$$10^9+7$$$. Find a value $$$s$$$ such that $$$B(x) equiv A(x+s) pmod{10^9+7}$$$ for all integers $$$x$$$. Input The first line contains a single integer $$$d$$$ ($$$1 le d le 2,500,000$$$). The second line contains $$$d+1$$$ integers $$$A(0), A(1), ldots, A(d)$$$ ($$$0 le A(i) < 10^9+7$$$) — the values of the polynomial $$$A(x)$$$. The third line contains $$$d+1$$$ integers $$$B(0), B(1), ldots, B(d)$$$ ($$$0 le B(i) < 10^9+7$$$) — the values of the polynomial $$$B(x)$$$. It is guaranteed that $$$A(x)$$$ and $$$B(x)$$$ are similar and that the leading coefficients (i.e., the coefficients in front of $$$x^d$$$) of $$$A(x)$$$ and $$$B(x)$$$ are not divisible by $$$10^9+7$$$. Output Print a single integer $$$s$$$ ($$$0 leq s < 10^9+7$$$) such that $$$B(x) equiv A(x+s) pmod{10^9+7}$$$ for all integers $$$x$$$. If there are multiple solutions, print any. Note In the first example, $$$A(x) equiv x-1 pmod{10^9+7}$$$ and $$$B(x)equiv x+2 pmod{10^9+7}$$$. They're similar because $$$$$$B(x) equiv A(x+3) pmod{10^9+7}.$$$$$$ In the second example, $$$A(x) equiv (x+1)^2 pmod{10^9+7}$$$ and $$$B(x) equiv (x+10)^2 pmod{10^9+7}$$$, hence $$$$$$B(x) equiv A(x+9) pmod{10^9+7}.$$$$$$ | 2,400 | true | false | false | false | false | false | false | false | false | false | 1,371 |
665A | Buses run between the cities _A_ and _B_, the first one is at 05:00 AM and the last one departs not later than at 11:59 PM. A bus from the city _A_ departs every _a_ minutes and arrives to the city _B_ in a _t__a_ minutes, and a bus from the city _B_ departs every _b_ minutes and arrives to the city _A_ in a _t__b_ minutes. The driver Simion wants to make his job diverse, so he counts the buses going towards him. Simion doesn't count the buses he meet at the start and finish. You know the time when Simion departed from the city _A_ to the city _B_. Calculate the number of buses Simion will meet to be sure in his counting. Input The first line contains two integers _a_,u2009_t__a_ (1u2009≤u2009_a_,u2009_t__a_u2009≤u2009120) — the frequency of the buses from the city _A_ to the city _B_ and the travel time. Both values are given in minutes. The second line contains two integers _b_,u2009_t__b_ (1u2009≤u2009_b_,u2009_t__b_u2009≤u2009120) — the frequency of the buses from the city _B_ to the city _A_ and the travel time. Both values are given in minutes. The last line contains the departure time of Simion from the city _A_ in the format hh:mm. It is guaranteed that there are a bus from the city _A_ at that time. Note that the hours and the minutes are given with exactly two digits. Output Print the only integer _z_ — the number of buses Simion will meet on the way. Note that you should not count the encounters in cities _A_ and _B_. Note In the first example Simion departs form the city _A_ at 05:20 AM and arrives to the city _B_ at 05:50 AM. He will meet the first 5 buses from the city _B_ that departed in the period [05:00 AM - 05:40 AM]. Also Simion will meet a bus in the city _B_ at 05:50 AM, but he will not count it. Also note that the first encounter will be between 05:26 AM and 05:27 AM (if we suggest that the buses are go with the sustained speed). | 1,600 | false | false | true | false | false | false | false | false | false | false | 7,161 |
1721B | The robot is placed in the top left corner of a grid, consisting of $$$n$$$ rows and $$$m$$$ columns, in a cell $$$(1, 1)$$$. In one step, it can move into a cell, adjacent by a side to the current one: $$$(x, y) ightarrow (x, y + 1)$$$; $$$(x, y) ightarrow (x + 1, y)$$$; $$$(x, y) ightarrow (x, y - 1)$$$; $$$(x, y) ightarrow (x - 1, y)$$$. The robot can't move outside the grid. The cell $$$(s_x, s_y)$$$ contains a deadly laser. If the robot comes into some cell that has distance less than or equal to $$$d$$$ to the laser, it gets evaporated. The distance between two cells $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$x_1 - x_2 + y_1 - y_2$$$. Print the smallest number of steps that the robot can take to reach the cell $$$(n, m)$$$ without getting evaporated or moving outside the grid. If it's not possible to reach the cell $$$(n, m)$$$, print -1. The laser is neither in the starting cell, nor in the ending cell. The starting cell always has distance greater than $$$d$$$ to the laser. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The only line of each testcase contains five integers $$$n, m, s_x, s_y, d$$$ ($$$2 le n, m le 1000$$$; $$$1 le s_x le n$$$; $$$1 le s_y le m$$$; $$$0 le d le n + m$$$)xa0— the size of the grid, the cell that contains the laser and the evaporating distance of the laser. The laser is neither in the starting cell, nor in the ending cell ($$$(s_x, s_y) eq (1, 1)$$$ and $$$(s_x, s_y) eq (n, m)$$$). The starting cell $$$(1, 1)$$$ always has distance greater than $$$d$$$ to the laser ($$$s_x - 1 + s_y - 1 > d$$$). Output For each testcase, print a single integer. If it's possible to reach the cell $$$(n, m)$$$ from $$$(1, 1)$$$ without getting evaporated or moving outside the grid, then print the smallest amount of steps it can take the robot to reach it. Otherwise, print -1. Example Input 3 2 3 1 3 0 2 3 1 3 1 5 5 3 4 1 | 1,000 | false | false | true | false | false | false | false | false | false | false | 1,958 |
1004D | Since Sonya has just learned the basics of matrices, she decided to play with them a little bit. Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so. The Manhattan distance between two cells ($$$x_1$$$, $$$y_1$$$) and ($$$x_2$$$, $$$y_2$$$) is defined as $$$x_1 - x_2 + y_1 - y_2$$$. For example, the Manhattan distance between the cells $$$(5, 2)$$$ and $$$(7, 1)$$$ equals to $$$5-7+2-1=3$$$. Example of a rhombic matrix. Note that rhombic matrices are uniquely defined by $$$n$$$, $$$m$$$, and the coordinates of the cell containing the zero. She drew a $$$n imes m$$$ rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of $$$ncdot m$$$ numbers). Note that Sonya will not give you $$$n$$$ and $$$m$$$, so only the sequence of numbers in this matrix will be at your disposal. Write a program that finds such an $$$n imes m$$$ rhombic matrix whose elements are the same as the elements in the sequence in some order. Input The first line contains a single integer $$$t$$$ ($$$1leq tleq 10^6$$$)xa0— the number of cells in the matrix. The second line contains $$$t$$$ integers $$$a_1, a_2, ldots, a_t$$$ ($$$0leq a_i< t$$$)xa0— the values in the cells in arbitrary order. Output In the first line, print two positive integers $$$n$$$ and $$$m$$$ ($$$n imes m = t$$$)xa0— the size of the matrix. In the second line, print two integers $$$x$$$ and $$$y$$$ ($$$1leq xleq n$$$, $$$1leq yleq m$$$)xa0— the row number and the column number where the cell with $$$0$$$ is located. If there are multiple possible answers, print any of them. If there is no solution, print the single integer $$$-1$$$. Examples Input 20 1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4 Input 18 2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1 Note You can see the solution to the first example in the legend. You also can choose the cell $$$(2, 2)$$$ for the cell where $$$0$$$ is located. You also can choose a $$$5 imes 4$$$ matrix with zero at $$$(4, 2)$$$. In the second example, there is a $$$3 imes 6$$$ matrix, where the zero is located at $$$(2, 3)$$$ there. In the third example, a solution does not exist. | 2,300 | false | false | true | false | false | true | true | false | false | false | 5,665 |
1337A | Ichihime is the current priestess of the Mahjong Soul Temple. She claims to be human, despite her cat ears. These days the temple is holding a math contest. Usually, Ichihime lacks interest in these things, but this time the prize for the winner is her favorite — cookies. Ichihime decides to attend the contest. Now she is solving the following problem. xa0You are given four positive integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$, such that $$$a leq b leq c leq d$$$. Your task is to find three integers $$$x$$$, $$$y$$$, $$$z$$$, satisfying the following conditions: $$$a leq x leq b$$$. $$$b leq y leq c$$$. $$$c leq z leq d$$$. There exists a triangle with a positive non-zero area and the lengths of its three sides are $$$x$$$, $$$y$$$, and $$$z$$$. Ichihime desires to get the cookie, but the problem seems too hard for her. Can you help her? Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) xa0— the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given as four space-separated integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$1 leq a leq b leq c leq d leq 10^9$$$). Output For each test case, print three integers $$$x$$$, $$$y$$$, $$$z$$$ xa0— the integers you found satisfying the conditions given in the statement. It is guaranteed that the answer always exists. If there are multiple answers, print any. Example Input 4 1 3 5 7 1 5 5 7 100000 200000 300000 400000 1 1 977539810 977539810 Output 3 4 5 5 5 5 182690 214748 300999 1 977539810 977539810 Note One of the possible solutions to the first test case: One of the possible solutions to the second test case: | 800 | true | false | false | false | false | true | false | false | false | false | 4,016 |
1430D | You have a string $$$s$$$ consisting of $$$n$$$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: 1. select an integer $$$i$$$ from $$$1$$$ to the length of the string $$$s$$$, then delete the character $$$s_i$$$ (the string length gets reduced by $$$1$$$, the indices of characters to the right of the deleted one also get reduced by $$$1$$$); 2. if the string $$$s$$$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $$$s =$$$ 111010, the first operation can be one of the following: 1. select $$$i = 1$$$: we'll get 111010 $$$ ightarrow$$$ 11010 $$$ ightarrow$$$ 010; 2. select $$$i = 2$$$: we'll get 111010 $$$ ightarrow$$$ 11010 $$$ ightarrow$$$ 010; 3. select $$$i = 3$$$: we'll get 111010 $$$ ightarrow$$$ 11010 $$$ ightarrow$$$ 010; 4. select $$$i = 4$$$: we'll get 111010 $$$ ightarrow$$$ 11110 $$$ ightarrow$$$ 0; 5. select $$$i = 5$$$: we'll get 111010 $$$ ightarrow$$$ 11100 $$$ ightarrow$$$ 00; 6. select $$$i = 6$$$: we'll get 111010 $$$ ightarrow$$$ 11101 $$$ ightarrow$$$ 01. You finish performing operations when the string $$$s$$$ becomes empty. What is the maximum number of operations you can perform? 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 a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of the string $$$s$$$. The second line contains string $$$s$$$ of $$$n$$$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $$$n$$$ over test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0— the maximum number of operations you can perform. Example Input 5 6 111010 1 0 1 1 2 11 6 101010 Note In the first test case, you can, for example, select $$$i = 2$$$ and get string 010 after the first operation. After that, you can select $$$i = 3$$$ and get string 1. Finally, you can only select $$$i = 1$$$ and get empty string. | 1,700 | false | true | false | false | true | false | false | true | false | false | 3,513 |
1780F | One day Kira found $$$n$$$ friends from Morioh and decided to gather them around a table to have a peaceful conversation. The height of friend $$$i$$$ is equal to $$$a_i$$$. It so happened that the height of each of the friends is unique. Unfortunately, there were only $$$3$$$ chairs in Kira's house, and obviously, it will not be possible to seat all friends! So, Kira has to invite only $$$3$$$ of his friends. But everything is not so simple! If the heights of the lowest and the tallest of the invited friends are not coprime, then the friends will play tricks on each other, which will greatly anger Kira. Kira became interested, how many ways are there to choose $$$3$$$ of his friends so that they don't play tricks? Two ways are considered different if there is a friend invited in one way, but not in the other. Formally, if Kira invites friends $$$i$$$, $$$j$$$, and $$$k$$$, then the following should be true: $$$gcd(min(a_i, a_j, a_k), max(a_i, a_j, a_k)) = 1$$$, where $$$gcd(x, y)$$$ denotes the — the number of Kira's friends. The next line contains $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 3cdot10^5$$$) — heights of Kira's friends. Note In the first example, only one way is suitable: invite friends $$$1$$$, $$$2$$$, and $$$3$$$. Here $$$1 < 2 < 3$$$, and the numbers $$$1$$$ and $$$3$$$ are coprime. | 2,300 | false | false | false | true | true | false | true | false | true | false | 1,590 |
269A | Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxes. The essence of the trick is in packing the boxes inside other boxes. From the top view each magical box looks like a square with side length equal to 2_k_ (_k_ is an integer, _k_u2009≥u20090) units. A magical box _v_ can be put inside a magical box _u_, if side length of _v_ is strictly less than the side length of _u_. In particular, Emuskald can put 4 boxes of side length 2_k_u2009-u20091 into one box of side length 2_k_, or as in the following figure: Emuskald is about to go on tour performing around the world, and needs to pack his magical boxes for the trip. He has decided that the best way to pack them would be inside another magical box, but magical boxes are quite expensive to make. Help him find the smallest magical box that can fit all his boxes. Input The first line of input contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009105), the number of different sizes of boxes Emuskald has. Each of following _n_ lines contains two integers _k__i_ and _a__i_ (0u2009≤u2009_k__i_u2009≤u2009109, 1u2009≤u2009_a__i_u2009≤u2009109), which means that Emuskald has _a__i_ boxes with side length 2_k__i_. It is guaranteed that all of _k__i_ are distinct. Output Output a single integer _p_, such that the smallest magical box that can contain all of Emuskald’s boxes has side length 2_p_. Note Picture explanation. If we have 3 boxes with side length 2 and 5 boxes with side length 1, then we can put all these boxes inside a box with side length 4, for example, as shown in the picture. In the second test case, we can put all four small boxes into a box with side length 2. | 1,600 | true | true | false | false | false | false | false | false | false | false | 8,758 |
1696E | We say an infinite sequence $$$a_{0}, a_{1}, a_2, ldots$$$ is non-increasing if and only if for all $$$ige 0$$$, $$$a_i ge a_{i+1}$$$. There is an infinite right and down grid. The upper-left cell has coordinates $$$(0,0)$$$. Rows are numbered $$$0$$$ to infinity from top to bottom, columns are numbered from $$$0$$$ to infinity from left to right. There is also a non-increasing infinite sequence $$$a_{0}, a_{1}, a_2, ldots$$$. You are given $$$a_0$$$, $$$a_1$$$, $$$ldots$$$, $$$a_n$$$; for all $$$i>n$$$, $$$a_i=0$$$. For every pair of $$$x$$$, $$$y$$$, the cell with coordinates $$$(x,y)$$$ (which is located at the intersection of $$$x$$$-th row and $$$y$$$-th column) is white if $$$y<a_x$$$ and black otherwise. Initially there is one doll named Jina on $$$(0,0)$$$. You can do the following operation. Select one doll on $$$(x,y)$$$. Remove it and place a doll on $$$(x,y+1)$$$ and place a doll on $$$(x+1,y)$$$. Note that multiple dolls can be present at a cell at the same time; in one operation, you remove only one. Your goal is to make all white cells contain $$$0$$$ dolls. What's the minimum number of operations needed to achieve the goal? Print the answer modulo $$$10^9+7$$$. Input The first line of input contains one integer $$$n$$$ ($$$1le nle 2cdot 10^5$$$). The second line of input contains $$$n+1$$$ integers $$$a_0,a_1,ldots,a_n$$$ ($$$0le a_ile 2cdot 10^5$$$). It is guaranteed that the sequence $$$a$$$ is non-increasing. Note Consider the first example. In the given grid, cells $$$(0,0),(0,1),(1,0),(1,1)$$$ are white, and all other cells are black. Let us use triples to describe the grid: triple $$$(x,y,z)$$$ means that there are $$$z$$$ dolls placed on cell $$$(x,y)$$$. Initially the state of the grid is $$$(0,0,1)$$$. One of the optimal sequence of operations is as follows: Do the operation with $$$(0,0)$$$. Now the state of the grid is $$$(1,0,1),(0,1,1)$$$. Do the operation with $$$(0,1)$$$. Now the state of the grid is $$$(1,0,1),(1,1,1),(0,2,1)$$$. Do the operation with $$$(1,0)$$$. Now the state of the grid is $$$(1,1,2),(0,2,1),(2,0,1)$$$. Do the operation with $$$(1,1)$$$. Now the state of the grid is $$$(1,1,1),(0,2,1),(2,0,1),(1,2,1),(2,1,1)$$$. Do the operation with $$$(1,1)$$$. Now the state of the grid is $$$(0,2,1),(2,0,1),(1,2,2),(2,1,2)$$$. Now all white cells contain $$$0$$$ dolls, so we have achieved the goal with $$$5$$$ operations. | 2,000 | true | false | false | false | false | false | false | false | false | false | 2,104 |
1928A | Bob has a rectangle of size $$$a imes b$$$. He tries to cut this rectangle into two rectangles with integer sides by making a cut parallel to one of the sides of the original rectangle. Then Bob tries to form some other rectangle from the two resulting rectangles, and he can rotate and move these two rectangles as he wishes. Note that if two rectangles differ only by a $$$90^{circ}$$$ rotation, they are considered the same. For example, the rectangles $$$6 imes 4$$$ and $$$4 imes 6$$$ are considered the same. Thus, from the $$$2 imes 6$$$ rectangle, another rectangle can be formed, because it can be cut into two $$$2 imes 3$$$ rectangles, and then these two rectangles can be used to form the $$$4 imes 3$$$ rectangle, which is different from the $$$2 imes 6$$$ rectangle. However, from the $$$2 imes 1$$$ rectangle, another rectangle cannot be formed, because it can only be cut into two rectangles of $$$1 imes 1$$$, and from these, only the $$$1 imes 2$$$ and $$$2 imes 1$$$ rectangles can be formed, which are considered the same. Help Bob determine if he can obtain some other rectangle, or if he is just wasting his time. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. This is followed by the description of the test cases. The single line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 10^9$$$) — the size of Bob's rectangle. Output For each test case, output "Yes" if Bob can obtain another rectangle from the $$$a imes b$$$ rectangle. Otherwise, output "No". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive answers. Example Input 7 1 1 2 1 2 6 3 2 2 2 2 4 6 3 Output No No Yes Yes Yes Yes No Note In the first test case, the $$$1 imes 1$$$ rectangle cannot be cut into two rectangles, so another rectangle cannot be obtained from it. In the fourth test case, the $$$3 imes 2$$$ rectangle can be cut into two $$$3 imes 1$$$ rectangles, and from these, the $$$1 imes 6$$$ rectangle can be formed. In the fifth test case, the $$$2 imes 2$$$ rectangle can be cut into two $$$1 imes 2$$$ rectangles, and from these, the $$$1 imes 4$$$ rectangle can be formed. | 800 | true | false | false | false | false | false | false | false | false | false | 723 |
1341A | Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished. In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it. Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b leq x_i leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d leq sumlimits_{i=1}^{n}{x_i} leq c + d$$$). Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 leq t leq 1000)$$$ xa0— the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 leq n leq 1000)$$$ xa0— the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 leq b < a leq 1000, 0 leq d < c leq 1000)$$$ xa0— numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$). Output For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$. Example Input 5 7 20 3 101 18 11 11 10 234 2 8 9 7 250 122 19 41 21 321 10 3 10 8 6 1 Note In the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack. In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack. In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack. In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains. | 900 | true | false | false | false | false | false | false | false | false | false | 4,001 |
1189B | You are given $$$n$$$ numbers $$$a_1, a_2, ldots, a_n$$$. Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors? For example, for the array $$$[1, 4, 5, 6, 7, 8]$$$, the arrangement on the left is valid, while arrangement on the right is not, as $$$5ge 4 + 1$$$ and $$$8> 1 + 6$$$. Input The first line contains a single integer $$$n$$$ ($$$3le n le 10^5$$$)xa0— the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i le 10^9$$$)xa0— the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed). Output If there is no solution, output "NO" in the first line. If there is a solution, output "YES" in the first line. In the second line output $$$n$$$ numbersxa0— elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are multiple solutions, output any of them. You can print the circle starting with any element. Note One of the possible arrangements is shown in the first example: $$$4< 2 + 3$$$; $$$2 < 4 + 3$$$; $$$3< 4 + 2$$$. One of the possible arrangements is shown in the second example. No matter how we arrange $$$13, 8, 5$$$ in a circle in the third example, $$$13$$$ will have $$$8$$$ and $$$5$$$ as neighbors, but $$$13ge 8 + 5$$$. There is no solution in the fourth example. | 1,100 | true | true | false | false | false | false | false | false | true | false | 4,752 |
923D | Alice has a string consisting of characters 'A', 'B' and 'C'. Bob can use the following transitions on any substring of our string in any order any number of times: Note that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source. Input The first line contains a string _S_ (1u2009≤u2009_S_u2009≤u2009105). The second line contains a string _T_ (1u2009≤u2009_T_u2009≤u2009105), each of these strings consists only of uppercase English letters 'A', 'B' and 'C'. The third line contains the number of queries _Q_ (1u2009≤u2009_Q_u2009≤u2009105). The following _Q_ lines describe queries. The _i_-th of these lines contains four space separated integers _a__i_, _b__i_, _c__i_, _d__i_. These represent the _i_-th query: is it possible to create _T_[_c__i_.._d__i_] from _S_[_a__i_.._b__i_] by applying the above transitions finite amount of times? Here, _U_[_x_.._y_] is a substring of _U_ that begins at index _x_ (indexed from 1) and ends at index _y_. In particular, _U_[1.._U_] is the whole string _U_. It is guaranteed that 1u2009≤u2009_a_u2009≤u2009_b_u2009≤u2009_S_ and 1u2009≤u2009_c_u2009≤u2009_d_u2009≤u2009_T_. Output Print a string of _Q_ characters, where the _i_-th character is '1' if the answer to the _i_-th query is positive, and '0' otherwise. Example Input AABCCBAAB ABCB 5 1 3 1 2 2 2 2 4 7 9 1 1 3 4 2 3 4 5 1 3 Note In the first query we can achieve the result, for instance, by using transitions . The third query asks for changing AAB to Axa0— but in this case we are not able to get rid of the character 'B'. | 2,500 | false | false | true | false | false | true | false | false | false | false | 6,020 |
914E | You are given a tree (a connected acyclic undirected graph) of _n_ vertices. Vertices are numbered from 1 to _n_ and each vertex is assigned a character from a to t. A path in the tree is said to be palindromic if at least one permutation of the labels in the path is a palindrome. For each vertex, output the number of palindromic paths passing through it. Note: The path from vertex _u_ to vertex _v_ is considered to be the same as the path from vertex _v_ to vertex _u_, and this path will be counted only once for each of the vertices it passes through. Input The first line contains an integer _n_ (2u2009≤u2009_n_u2009≤u20092·105) xa0— the number of vertices in the tree. The next _n_u2009-u20091 lines each contain two integers _u_ and _v_ (1u2009u2009≤u2009u2009_u_,u2009_v_u2009u2009≤u2009u2009_n_,u2009_u_u2009≠u2009_v_) denoting an edge connecting vertex _u_ and vertex _v_. It is guaranteed that the given graph is a tree. The next line contains a string consisting of _n_ lowercase characters from a to t where the _i_-th (1u2009≤u2009_i_u2009≤u2009_n_) character is the label of vertex _i_ in the tree. Output Print _n_ integers in a single line, the _i_-th of which is the number of palindromic paths passing through vertex _i_ in the tree. Examples Input 7 6 2 4 3 3 7 5 2 7 2 1 4 afefdfs Note In the first sample case, the following paths are palindromic: 2u2009-u20093u2009-u20094 2u2009-u20093u2009-u20095 4u2009-u20093u2009-u20095 Additionally, all paths containing only one vertex are palindromic. Listed below are a few paths in the first sample that are not palindromic: 1u2009-u20092u2009-u20093 1u2009-u20092u2009-u20093u2009-u20094 1u2009-u20092u2009-u20093u2009-u20095 | 2,400 | false | false | false | false | true | false | false | false | false | false | 6,079 |
39F | Thumbelina has had an accident. She has found herself on a little island in the middle of a swamp and wants to get to the shore very much. One can get to the shore only by hills that are situated along a straight line that connects the little island with the shore. Let us assume that the hills are numbered from 1 to _n_ and the number of a hill is equal to the distance in meters between it and the island. The distance between the _n_-th hill and the shore is also 1 meter. Thumbelina is too small to make such jumps. Fortunately, a family of frogs living in the swamp suggests to help her. Each frog agrees to give Thumbelina a ride but Thumbelina should choose only one frog. Each frog has a certain jump length. If Thumbelina agrees to accept help from a frog whose jump length is _d_, the frog will jump from the island on the hill _d_, then — on the hill 2_d_, then 3_d_ and so on until they get to the shore (i.e. find itself beyond the hill _n_). However, there is one more problem: mosquitoes also live in the swamp. At the moment they have a siesta, and they are having a nap on some hills. If the frog jumps on a hill with a mosquito the frog will smash it. The frogs Thumbelina has met are pacifists, so they will find the death of each mosquito very much sad. Help Thumbelina choose a frog that will bring her to the shore and smash as small number of mosquitoes as possible. Input The first line contains three integers _n_, _m_ and _k_ (1u2009≤u2009_n_u2009≤u2009109, 1u2009≤u2009_m_,u2009_k_u2009≤u2009100) — the number of hills, frogs and mosquitoes respectively. The second line contains _m_ integers _d__i_ (1u2009≤u2009_d__i_u2009≤u2009109) — the lengths of the frogs’ jumps. The third line contains _k_ integers — the numbers of the hills on which each mosquito is sleeping. No more than one mosquito can sleep on each hill. The numbers in the lines are separated by single spaces. Output In the first line output the number of frogs that smash the minimal number of mosquitoes, in the second line — their numbers in increasing order separated by spaces. The frogs are numbered from 1 to _m_ in the order of the jump length given in the input data. Examples Input 1000000000 2 3 2 5 999999995 999999998 999999996 | 1,300 | false | false | true | false | false | false | false | false | false | false | 9,795 |
403D | The sequence of integer pairs (_a_1,u2009_b_1),u2009(_a_2,u2009_b_2),u2009...,u2009(_a__k_,u2009_b__k_) is beautiful, if the following statements are fulfilled: 1u2009≤u2009_a_1u2009≤u2009_b_1u2009<u2009_a_2u2009≤u2009_b_2u2009<u2009...u2009<u2009_a__k_u2009≤u2009_b__k_u2009≤u2009_n_, where _n_ is a given positive integer; all numbers _b_1u2009-u2009_a_1, _b_2u2009-u2009_a_2, ..., _b__k_u2009-u2009_a__k_ are distinct. For the given number _n_ find the number of beautiful sequences of length _k_. As the answer can be rather large, print the remainder after dividing it by 1000000007 (109u2009+u20097). Input The first line contains integer _t_ (1u2009≤u2009_t_u2009≤u2009u20092·105) — the number of the test data. Each of the next _t_ lines contains two integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u20091000). Output For each test from the input print the answer to the problem modulo 1000000007 (109u2009+u20097). Print the answers to the tests in the order in which the tests are given in the input. Note In the first test sample there is exactly one beautiful sequence: (1,u20091). In the second test sample, the following sequences are beautiful: (1,u20091); (1,u20092); (2,u20092). In the fourth test sample, the following sequences are beautiful: (1,u20091); (1,u20092); (1,u20093); (2,u20092); (2,u20093); (3,u20093). In the fifth test sample, the following sequences are beautiful: (1,u20091),u2009(2,u20093); (1,u20092),u2009(3,u20093). In the third and sixth samples, there are no beautiful sequences. | 2,300 | false | false | false | true | false | false | false | false | false | false | 8,212 |
845E | The capital of Berland looks like a rectangle of size _n_u2009×u2009_m_ of the square blocks of same size. Fire! It is known that _k_u2009+u20091 blocks got caught on fire (_k_u2009+u20091u2009≤u2009_n_·_m_). Those blocks are centers of ignition. Moreover positions of _k_ of these centers are known and one of these stays unknown. All _k_u2009+u20091 positions are distinct. The fire goes the following way: during the zero minute of fire only these _k_u2009+u20091 centers of ignition are burning. Every next minute the fire goes to all neighbouring blocks to the one which is burning. You can consider blocks to burn for so long that this time exceeds the time taken in the problem. The neighbouring blocks are those that touch the current block by a side or by a corner. Berland Fire Deparment wants to estimate the minimal time it takes the fire to lighten up the whole city. Remember that the positions of _k_ blocks (centers of ignition) are known and (_k_u2009+u20091)-th can be positioned in any other block. Help Berland Fire Department to estimate the minimal time it takes the fire to lighten up the whole city. Input The first line contains three integers _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009109, 1u2009≤u2009_k_u2009≤u2009500). Each of the next _k_ lines contain two integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_u2009≤u2009_n_, 1u2009≤u2009_y__i_u2009≤u2009_m_) — coordinates of the _i_-th center of ignition. It is guaranteed that the locations of all centers of ignition are distinct. Output Print the minimal time it takes the fire to lighten up the whole city (in minutes). Note In the first example the last block can have coordinates (4,u20094). In the second example the last block can have coordinates (8,u20093). | 2,400 | false | false | false | false | true | false | false | true | false | false | 6,380 |
615C | A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art. First, he wants to construct the running track with coating _t_. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string. Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings _s_. Also, he has scissors and glue. Ayrat is going to buy some coatings _s_, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating _s_ he needs to buy in order to get the coating _t_ for his running track. Of course, he also want's to know some way to achieve the answer. Input First line of the input contains the string _s_xa0— the coating that is present in the shop. Second line contains the string _t_xa0— the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100. Output The first line should contain the minimum needed number of coatings _n_ or -1 if it's impossible to create the desired coating. If the answer is not -1, then the following _n_ lines should contain two integers _x__i_ and _y__i_xa0— numbers of ending blocks in the corresponding piece. If _x__i_u2009≤u2009_y__i_ then this piece is used in the regular order, and if _x__i_u2009>u2009_y__i_ piece is used in the reversed order. Print the pieces in the order they should be glued to get the string _t_. Note In the first sample string "cbaabc" = "cba" + "abc". In the second sample: "ayrat" = "a" + "yr" + "at". | 2,000 | false | true | false | true | false | false | false | false | false | false | 7,378 |
1814B | A robot is placed in a cell $$$(0, 0)$$$ of an infinite grid. This robot has adjustable length legs. Initially, its legs have length $$$1$$$. Let the robot currently be in the cell $$$(x, y)$$$ and have legs of length $$$m$$$. In one move, it can perform one of the following three actions: jump into the cell $$$(x + m, y)$$$; jump into the cell $$$(x, y + m)$$$; increase the length of the legs by $$$1$$$, i.u2009e. set it to $$$m + 1$$$. What's the smallest number of moves robot has to make to reach a cell $$$(a, b)$$$? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 10^9$$$)xa0— the ending cell. Output For each test case, print a single integerxa0— the smallest number of moves the robot is required to make to reach a cell $$$(a, b)$$$ from a cell $$$(0, 0)$$$. Note In the first testcase, the robot can first jump to $$$(0, 1)$$$, then to $$$(1, 1)$$$. If it ever increases the length of its legs, it will only be able to jump past $$$(1, 1)$$$. In the second testcase, the robot can jump to $$$(1, 0)$$$, then increase the length of its length to $$$2$$$ and jump three times to reach $$$(1, 6)$$$. In the third testcase, the robot can increase the length of its legs three times to make it $$$4$$$. Then jump to $$$(0, 4)$$$. Then jump twice to reach $$$(8, 4)$$$. | 1,700 | true | false | false | false | false | false | true | false | false | false | 1,386 |
444E | DZY loves planting, and he enjoys solving tree problems. DZY has a weighted tree (connected undirected graph without cycles) containing _n_ nodes (they are numbered from 1 to _n_). He defines the function _g_(_x_,u2009_y_) (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_) as the longest edge in the shortest path between nodes _x_ and _y_. Specially _g_(_z_,u2009_z_)u2009=u20090 for every _z_. For every integer sequence _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009_n_), DZY defines _f_(_p_) as . DZY wants to find such a sequence _p_ that _f_(_p_) has maximum possible value. But there is one more restriction: the element _j_ can appear in _p_ at most _x__j_ times. Please, find the maximum possible _f_(_p_) under the described restrictions. Input The first line contains an integer _n_xa0(1u2009≤u2009_n_u2009≤u20093000). Each of the next _n_u2009-u20091 lines contains three integers _a__i_,u2009_b__i_,u2009_c__i_xa0(1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_;xa01u2009≤u2009_c__i_u2009≤u200910000), denoting an edge between _a__i_ and _b__i_ with length _c__i_. It is guaranteed that these edges form a tree. Each of the next _n_ lines describes an element of sequence _x_. The _j_-th line contains an integer _x__j_xa0(1u2009≤u2009_x__j_u2009≤u2009_n_). Output Print a single integer representing the answer. Examples Input 4 1 2 1 2 3 2 3 4 3 1 1 1 1 Input 4 1 2 1 2 3 2 3 4 3 4 4 4 4 Note In the first sample, one of the optimal _p_ is [4,u20093,u20092,u20091]. | 2,700 | false | false | false | false | false | false | false | true | false | false | 8,059 |
912A | During the winter holidays, the demand for Christmas balls is exceptionally high. Since it's already 2018, the advances in alchemy allow easy and efficient ball creation by utilizing magic crystals. Grisha needs to obtain some yellow, green and blue balls. It's known that to produce a yellow ball one needs two yellow crystals, greenxa0— one yellow and one blue, and for a blue ball, three blue crystals are enough. Right now there are _A_ yellow and _B_ blue crystals in Grisha's disposal. Find out how many additional crystals he should acquire in order to produce the required number of balls. Input The first line features two integers _A_ and _B_ (0u2009≤u2009_A_,u2009_B_u2009≤u2009109), denoting the number of yellow and blue crystals respectively at Grisha's disposal. The next line contains three integers _x_, _y_ and _z_ (0u2009≤u2009_x_,u2009_y_,u2009_z_u2009≤u2009109)xa0— the respective amounts of yellow, green and blue balls to be obtained. Output Print a single integerxa0— the minimum number of crystals that Grisha should acquire in addition. Examples Input 12345678 87654321 43043751 1000000000 53798715 Note In the first sample case, Grisha needs five yellow and four blue crystals to create two yellow balls, one green ball, and one blue ball. To do that, Grisha needs to obtain two additional crystals: one yellow and one blue. | 800 | false | false | true | false | false | false | false | false | false | false | 6,096 |
1758D | You are given an integer $$$n$$$. Find a sequence of $$$n$$$ distinct integers $$$a_1, a_2, dots, a_n$$$ such that $$$1 leq a_i leq 10^9$$$ for all $$$i$$$ and $$$$$$max(a_1, a_2, dots, a_n) - min(a_1, a_2, dots, a_n)= sqrt{a_1 + a_2 + dots + a_n}.$$$$$$ It can be proven that there exists a sequence of distinct integers that satisfies all the conditions above. Input The first line of input contains $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first and only line of each test case contains one integer $$$n$$$ ($$$2 leq n leq 3 cdot 10^5$$$)xa0— the length of the sequence you have to find. The sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output $$$n$$$ space-separated distinct integers $$$a_1, a_2, dots, a_n$$$ satisfying the conditions in the statement. If there are several possible answers, you can output any of them. Please remember that your integers must be distinct! Example Output 3 1 20 29 18 26 28 25 21 23 31 Note In the first test case, the maximum is $$$3$$$, the minimum is $$$1$$$, the sum is $$$4$$$, and $$$3 - 1 = sqrt{4}$$$. In the second test case, the maximum is $$$29$$$, the minimum is $$$18$$$, the sum is $$$121$$$, and $$$29-18 = sqrt{121}$$$. For each test case, the integers are all distinct. | 1,800 | true | false | false | false | false | true | true | true | false | false | 1,765 |
946E | Problem - 946E - 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 greedy implementation *2200 No tag edit access → Contest materials — the number of testcases you have to solve. Then _t_ lines follow, each representing one testcase and containing one string which is the decimal representation of number _s_. It is guaranteed that this string has even length, contains no leading zeroes, and there exists at least one beautiful number less than _s_. The sum of lengths of _s_ over all testcases doesn't exceed 2·105. Output For each testcase print one line containing the largest beautiful number which is less than _s_ (it is guaranteed that the answer exists). Example Input 4 89 88 1000 28923845 Output 88 77 99 28923839 | 2,200 | false | true | true | false | false | false | false | false | false | false | 5,928 |
1366A | Problem - 1366A - 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 greedy math *1100 No tag edit access → Contest materials . In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick. Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has $$$a$$$ sticks and $$$b$$$ diamonds? Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. The only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$0 le a, b le 10^9$$$)xa0— the number of sticks and the number of diamonds, respectively. Output For each test case print one integer — the maximum number of emeralds Polycarp can earn. Example Input 4 4 4 1000000000 0 7 15 8 7 Output 2 0 7 5 Note In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel. In the second test case Polycarp does not have any diamonds, so he cannot craft anything. | 1,100 | true | true | false | false | false | false | false | true | false | false | 3,850 |
1555C | Alice and Bob are playing a game on a matrix, consisting of $$$2$$$ rows and $$$m$$$ columns. The cell in the $$$i$$$-th row in the $$$j$$$-th column contains $$$a_{i, j}$$$ coins in it. Initially, both Alice and Bob are standing in a cell $$$(1, 1)$$$. They are going to perform a sequence of moves to reach a cell $$$(2, m)$$$. The possible moves are: Move rightxa0— from some cell $$$(x, y)$$$ to $$$(x, y + 1)$$$; Move downxa0— from some cell $$$(x, y)$$$ to $$$(x + 1, y)$$$. First, Alice makes all her moves until she reaches $$$(2, m)$$$. She collects the coins in all cells she visit (including the starting cell). When Alice finishes, Bob starts his journey. He also performs the moves to reach $$$(2, m)$$$ and collects the coins in all cells that he visited, but Alice didn't. The score of the game is the total number of coins Bob collects. Alice wants to minimize the score. Bob wants to maximize the score. What will the score of the game be if both players play optimally? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of the testcase contains a single integer $$$m$$$ ($$$1 le m le 10^5$$$)xa0— the number of columns of the matrix. The $$$i$$$-th of the next $$$2$$$ lines contain $$$m$$$ integers $$$a_{i,1}, a_{i,2}, dots, a_{i,m}$$$ ($$$1 le a_{i,j} le 10^4$$$)xa0— the number of coins in the cell in the $$$i$$$-th row in the $$$j$$$-th column of the matrix. The sum of $$$m$$$ over all testcases doesn't exceed $$$10^5$$$. Output For each testcase print a single integerxa0— the score of the game if both players play optimally. Example Input 3 3 1 3 7 3 5 1 3 1 3 9 3 5 1 1 4 7 Note The paths for the testcases are shown on the following pictures. Alice's path is depicted in red and Bob's path is depicted in blue. | 1,300 | false | false | true | true | false | true | true | false | false | false | 2,859 |
833A | Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting. The game consists of multiple rounds. Its rules are very simple: in each round, a natural number _k_ is chosen. Then, the one who says (or barks) it faster than the other wins the round. After that, the winner's score is multiplied by _k_2, and the loser's score is multiplied by _k_. In the beginning of the game, both Slastyona and Pushok have scores equal to one. Unfortunately, Slastyona had lost her notepad where the history of all _n_ games was recorded. She managed to recall the final results for each games, though, but all of her memories of them are vague. Help Slastyona verify their correctness, or, to put it another way, for each given pair of scores determine whether it was possible for a game to finish with such result or not. Input In the first string, the number of games _n_ (1u2009≤u2009_n_u2009≤u2009350000) is given. Each game is represented by a pair of scores _a_, _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009109)xa0– the results of Slastyona and Pushok, correspondingly. Output For each pair of scores, answer "Yes" if it's possible for a game to finish with given score, and "No" otherwise. You can output each letter in arbitrary case (upper or lower). Example Input 6 2 4 75 45 8 8 16 16 247 994 1000000000 1000000 Note First game might have been consisted of one round, in which the number 2 would have been chosen and Pushok would have won. The second game needs exactly two rounds to finish with such result: in the first one, Slastyona would have said the number 5, and in the second one, Pushok would have barked the number 3. | 1,700 | true | false | false | false | false | false | false | false | false | false | 6,434 |
1466G | Whoso in ignorance draws near to them and hears the Sirens' voice, he nevermore returns. Homer, Odyssey In the times of Jason and the Argonauts, it was well known that sirens use the sound of their songs to lure sailors into their demise. Yet only a few knew that every time sirens call a sailor by his name, his will weakens, making him more vulnerable. For the purpose of this problem, both siren songs and names of the sailors will be represented as strings of lowercase English letters. The more times the sailor's name occurs as a contiguous substring of the song, the greater danger he is in. Jason found out that sirens can sing one of the $$$n+1$$$ songs, which have the following structure: let $$$s_i$$$ ($$$0 leq i leq n$$$) be the $$$i$$$-th song and $$$t$$$ be a string of length $$$n$$$, then for every $$$i < n$$$: $$$s_{i+1} = s_i t_i s_i$$$. In other words $$$i+1$$$-st song is the concatenation of $$$i$$$-th song, $$$i$$$-th letter ($$$0$$$-indexed) of $$$t$$$ and the $$$i$$$-th song. Fortunately, he also knows $$$s_0$$$ and $$$t$$$. Jason wonders how many times a sailor's name is mentioned in a particular song. Answer $$$q$$$ queries: given the sailor's name ($$$w$$$) and the index of a song ($$$i$$$) output the number of occurrences of $$$w$$$ in $$$s_i$$$ as a substring. As this number can be quite large, output its remainder modulo $$$10^9+7$$$. Input In the first line of input there are two integers $$$n$$$, $$$q$$$ ( $$$ 1 leq n, q leq 10^5$$$) meaning that there are $$$n+1$$$ songs and $$$q$$$ queries. In the next two lines strings $$$s_0$$$ and $$$t$$$ follow ($$$1 leq s_0 leq 100, t = n$$$). Next $$$q$$$ lines describe the queries; each one contains an integer $$$k$$$ ($$$ 0 leq k leq n$$$), the index of the song sung by the sirens, and a non-empty string $$$w$$$, which is the name of a sailor. All strings in this problem consist only of lowercase English letters, and the sum of lengths of sailors' names does not exceed $$$10^6$$$. Output Output $$$q$$$ lines, $$$i$$$-th of them should contain the remainder modulo $$$10^9+7$$$ of the number of occurrences of $$$w$$$ in $$$s_k$$$. Examples Input 3 3 aa bcd 2 aba 3 ca 3 aa Input 4 5 aba bbac 1 a 3 baca 3 ab 2 bab 4 aba Note In the first example songs of the sirens are as follows: Song $$$0$$$: aa Song $$$1$$$: aabaa Song $$$2$$$: aabaacaabaa Song $$$3$$$: aabaacaabaadaabaacaabaa | 2,600 | true | false | false | false | false | false | false | false | false | false | 3,360 |
637C | During a New Year special offer the "Sudislavl Bars" offered _n_ promo codes. Each promo code consists of exactly six digits and gives right to one free cocktail at the bar "Mosquito Shelter". Of course, all the promocodes differ. As the "Mosquito Shelter" opens only at 9, and partying in Sudislavl usually begins at as early as 6, many problems may arise as to how to type a promotional code without errors. It is necessary to calculate such maximum _k_, that the promotional code could be uniquely identified if it was typed with no more than _k_ errors. At that, _k_u2009=u20090 means that the promotional codes must be entered exactly. A mistake in this problem should be considered as entering the wrong numbers. For example, value "123465" contains two errors relative to promocode "123456". Regardless of the number of errors the entered value consists of exactly six digits. Input The first line of the output contains number _n_ (1u2009≤u2009_n_u2009≤u20091000)xa0— the number of promocodes. Each of the next _n_ lines contains a single promocode, consisting of exactly 6 digits. It is guaranteed that all the promocodes are distinct. Promocodes can start from digit "0". Output Print the maximum _k_ (naturally, not exceeding the length of the promocode), such that any promocode can be uniquely identified if it is typed with at most _k_ mistakes. Examples Input 6 211111 212111 222111 111111 112111 121111 Note In the first sample _k_u2009<u20093, so if a bar customer types in value "090909", then it will be impossible to define which promocode exactly corresponds to it. | 1,400 | false | false | true | false | false | true | true | false | false | false | 7,263 |
1858D | The teachers of the Summer Informatics School decided to plant $$$n$$$ trees in a row, and it was decided to plant only oaks and firs. To do this, they made a plan, which can be represented as a binary string $$$s$$$ of length $$$n$$$. If $$$s_i = 0$$$, then the $$$i$$$-th tree in the row should be an oak, and if $$$s_i = 1$$$, then the $$$i$$$-th tree in the row should be a fir. The day of tree planting is tomorrow, and the day after tomorrow an inspector will come to the School. The inspector loves nature very much, and he will evaluate the beauty of the row as follows: First, he will calculate $$$l_0$$$ as the maximum number of consecutive oaks in the row (the maximum substring consisting of zeros in the plan $$$s$$$). If there are no oaks in the row, then $$$l_0 = 0$$$. Then, he will calculate $$$l_1$$$ as the maximum number of consecutive firs in the row (the maximum substring consisting of ones in the plan $$$s$$$). If there are no firs in the row, then $$$l_1 = 0$$$. Finally, he will calculate the beauty of the row as $$$a cdot l_0 + l_1$$$ for some $$$a$$$xa0— the inspector's favourite number. The teachers know the value of the parameter $$$a$$$, but for security reasons they cannot tell it to you. They only told you that $$$a$$$ is an integer from $$$1$$$ to $$$n$$$. Since the trees have not yet been planted, the teachers decided to change the type of no more than $$$k$$$ trees to the opposite (i.e., change $$$s_i$$$ from $$$0$$$ to $$$1$$$ or from $$$1$$$ to $$$0$$$ in the plan) in order to maximize the beauty of the row of trees according to the inspector. For each integer $$$j$$$ from $$$1$$$ to $$$n$$$ answer the following question independently: What is the maximum beauty of the row of trees that the teachers can achieve by changing the type of no more than $$$k$$$ trees if the inspector's favourite number $$$a$$$ is equal to $$$j$$$? 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$$$ and $$$k$$$ ($$$1 le n le 3000$$$, $$$0 le k le n$$$)xa0— the number of trees in the row and the maximum number of changes. The second line contains a string $$$s$$$ of length $$$n$$$, consisting of zeros and onesxa0— the plan description. It is guaranteed that the sum of all $$$n$$$ values for all test cases does not exceed $$$3000$$$. Output For each test case, print $$$n$$$ integers, the $$$j$$$-th ($$$1 le j le n$$$) of which is the maximum beauty of the row of trees after no more than $$$k$$$ changes if $$$a = j$$$ is used to calculate the beauty. Example Input 5 3 0 111 4 1 0110 5 0 10000 6 2 101101 7 1 0001101 Output 3 3 3 4 5 7 9 5 9 13 17 21 6 9 13 17 21 25 7 10 13 17 21 25 29 Note In the first test case no changes are allowed, so $$$l_0 = 0$$$ and $$$l_1 = 3$$$ always hold. Thus, regardless of the value of $$$a$$$, the beauty of the row of trees will be $$$3$$$. In the second test case for $$$a in {1, 2}$$$ the teachers can, for example, change the plan $$$s$$$ to $$$0111$$$ (by changing $$$s_4$$$), and for $$$a in {3, 4}$$$xa0— to $$$0010$$$ (by changing $$$s_2$$$). In this case, the beauty of the row for each $$$a$$$ is calculated as follows: For $$$a = 1$$$: $$$l_0 = 1$$$, $$$l_1 = 3$$$. The beauty of the row is $$$1cdot 1 + 3 = 4$$$. For $$$a = 2$$$: $$$l_0 = 1$$$, $$$l_1 = 3$$$. The beauty of the row is $$$2cdot 1 + 3 = 5$$$. For $$$a = 3$$$: $$$l_0 = 2$$$, $$$l_1 = 1$$$. The beauty of the row is $$$3cdot 2 + 1 = 7$$$. For $$$a = 4$$$: $$$l_0 = 2$$$, $$$l_1 = 1$$$. The beauty of the row is $$$4cdot 2 + 1 = 9$$$. It can be shown that the changes described above are optimal for all $$$a$$$ from $$$1$$$ to $$$4$$$. | 2,200 | false | true | false | true | true | false | true | false | false | false | 1,134 |
1144D | You are given an array $$$a$$$ consisting of $$$n$$$ integers. You can perform the following operations arbitrary number of times (possibly, zero): 1. Choose a pair of indices $$$(i, j)$$$ such that $$$i-j=1$$$ (indices $$$i$$$ and $$$j$$$ are adjacent) and set $$$a_i := a_i + a_i - a_j$$$; 2. Choose a pair of indices $$$(i, j)$$$ such that $$$i-j=1$$$ (indices $$$i$$$ and $$$j$$$ are adjacent) and set $$$a_i := a_i - a_i - a_j$$$. The value $$$x$$$ means the absolute value of $$$x$$$. For example, $$$4 = 4$$$, $$$-3 = 3$$$. Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it. It is guaranteed that you always can obtain the array of equal elements using such operations. Note that after each operation each element of the current array should not exceed $$$10^{18}$$$ by absolute value. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 2 cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. Output In the first line print one integer $$$k$$$ — the minimum number of operations required to obtain the array of equal elements. In the next $$$k$$$ lines print operations itself. The $$$p$$$-th operation should be printed as a triple of integers $$$(t_p, i_p, j_p)$$$, where $$$t_p$$$ is either $$$1$$$ or $$$2$$$ ($$$1$$$ means that you perform the operation of the first type, and $$$2$$$ means that you perform the operation of the second type), and $$$i_p$$$ and $$$j_p$$$ are indices of adjacent elements of the array such that $$$1 le i_p, j_p le n$$$, $$$i_p - j_p = 1$$$. See the examples for better understanding. Note that after each operation each element of the current array should not exceed $$$10^{18}$$$ by absolute value. If there are many possible answers, you can print any. | 1,400 | false | true | false | false | false | true | false | false | false | false | 5,002 |
1446C | For a given sequence of distinct non-negative integers $$$(b_1, b_2, dots, b_k)$$$ we determine if it is good in the following way: Consider a graph on $$$k$$$ nodes, with numbers from $$$b_1$$$ to $$$b_k$$$ written on them. For every $$$i$$$ from $$$1$$$ to $$$k$$$: find such $$$j$$$ ($$$1 le j le k$$$, $$$j eq i$$$), for which $$$(b_i oplus b_j)$$$ is the smallest among all such $$$j$$$, where $$$oplus$$$ denotes the operation of bitwise XOR ([ Next, draw an undirected edge between vertices with numbers $$$b_i$$$ and $$$b_j$$$ in this graph. We say that the sequence is good if and only if the resulting graph forms a tree (is connected and doesn't have any simple cycles). It is possible that for some numbers $$$b_i$$$ and $$$b_j$$$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once. You can find an example below (the picture corresponding to the first test case). Sequence $$$(0, 1, 5, 2, 6)$$$ is not good as we cannot reach $$$1$$$ from $$$5$$$. However, sequence $$$(0, 1, 5, 2)$$$ is good. You are given a sequence $$$(a_1, a_2, dots, a_n)$$$ of distinct non-negative integers. You would like to remove some of the elements (possibly none) to make the remaining sequence good. What is the minimum possible number of removals required to achieve this goal? It can be shown that for any sequence, we can remove some number of elements, leaving at least $$$2$$$, so that the remaining sequence is good. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 200,000$$$)xa0— length of the sequence. The second line contains $$$n$$$ distinct non-negative integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$)xa0— the elements of the sequence. Note Note that numbers which you remove don't impact the procedure of telling whether the resulting sequence is good. It is possible that for some numbers $$$b_i$$$ and $$$b_j$$$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once. | 2,100 | false | false | false | true | true | false | false | true | false | false | 3,445 |
1750F | Everyone was happy coding, until suddenly a power shortage happened and the best competitive programming site went down. Fortunately, a system administrator bought some new equipment recently, including some UPSs. Thus there are some servers that are still online, but we need all of them to be working in order to keep the round rated. Imagine the servers being a binary string $$$s$$$ of length $$$n$$$. If the $$$i$$$-th server is online, then $$$s_i = 1$$$, and $$$s_i = 0$$$ otherwise. A system administrator can do the following operation called electricity spread, that consists of the following phases: Select two servers at positions $$$1 le i < j le n$$$ such that both are online (i.e. $$$s_i=s_j=1$$$). The spread starts only from online servers. Check if we have enough power to make the spread. We consider having enough power if the number of turned on servers in range $$$[i, j]$$$ is at least the number of turned off servers in range $$$[i, j]$$$. More formally, check whether $$$2 cdot (s_i + s_{i+1} + ldots + s_j) ge j - i + 1$$$. If the check is positive, turn on all the offline servers in range $$$[i, j]$$$. More formally, make $$$s_k := 1$$$ for all $$$k$$$ from $$$i$$$ to $$$j$$$. We call a binary string $$$s$$$ of length $$$n$$$ rated if we can turn on all servers (i.e. make $$$s_i = 1$$$ for $$$1 le i le n$$$) using the electricity spread operation any number of times (possibly, $$$0$$$). Your task is to find the number of rated strings of length $$$n$$$ modulo $$$m$$$. Input The first and only line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 5000$$$, $$$10 le m le 10^9$$$) — the length of the string and the required module. Output Print a single integer — the number of rated binary strings of length $$$n$$$. Since this number can be large, print it modulo $$$m$$$. Note In the first example, the only rated string is 11. So the answer is $$$1$$$. In the second example, the rated strings are: 111; 101, because we can perform an operation with $$$i = 1$$$ and $$$j = 3$$$. So the answer is $$$2$$$. In the third sample, the rated strings are: 1001; 1111; 1011; 1101. So the answer is $$$4$$$. | 2,700 | true | false | false | true | false | false | false | false | false | false | 1,780 |
584E | Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them obtain the required permutation by paying as little money as possible. More formally, we have two permutations, _p_ and _s_ of numbers from 1 to _n_. We can swap _p__i_ and _p__j_, by paying _i_u2009-u2009_j_ coins for it. Find and print the smallest number of coins required to obtain permutation _s_ from permutation _p_. Also print the sequence of swap operations at which we obtain a solution. Input The first line contains a single number _n_ (1u2009≤u2009_n_u2009≤u20092000) — the length of the permutations. The second line contains a sequence of _n_ numbers from 1 to _n_ — permutation _p_. Each number from 1 to _n_ occurs exactly once in this line. The third line contains a sequence of _n_ numbers from 1 to _n_ — permutation _s_. Each number from 1 to _n_ occurs once in this line. Output In the first line print the minimum number of coins that you need to spend to transform permutation _p_ into permutation _s_. In the second line print number _k_ (0u2009≤u2009_k_u2009≤u20092·106) — the number of operations needed to get the solution. In the next _k_ lines print the operations. Each line must contain two numbers _i_ and _j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_, _i_u2009≠u2009_j_), which means that you need to swap _p__i_ and _p__j_. It is guaranteed that the solution exists. Note In the first sample test we swap numbers on positions 3 and 4 and permutation _p_ becomes 4 2 3 1. We pay 3u2009-u20094u2009=u20091 coins for that. On second turn we swap numbers on positions 1 and 3 and get permutation 3241 equal to _s_. We pay 3u2009-u20091u2009=u20092 coins for that. In total we pay three coins. | 2,300 | true | true | false | false | false | true | false | false | false | false | 7,506 |
723F | You are given an undirected connected graph consisting of _n_ vertices and _m_ edges. There are no loops and no multiple edges in the graph. You are also given two distinct vertices _s_ and _t_, and two values _d__s_ and _d__t_. Your task is to build any spanning tree of the given graph (note that the graph is not weighted), such that the degree of the vertex _s_ doesn't exceed _d__s_, and the degree of the vertex _t_ doesn't exceed _d__t_, or determine, that there is no such spanning tree. The spanning tree of the graph _G_ is a subgraph which is a tree and contains all vertices of the graph _G_. In other words, it is a connected graph which contains _n_u2009-u20091 edges and can be obtained by removing some of the edges from _G_. The degree of a vertex is the number of edges incident to this vertex. Input The first line of the input contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009200u2009000, 1u2009≤u2009_m_u2009≤u2009_min_(400u2009000,u2009_n_·(_n_u2009-u20091)u2009/u20092))xa0— the number of vertices and the number of edges in the graph. The next _m_ lines contain the descriptions of the graph's edges. Each of the lines contains two integers _u_ and _v_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009_n_, _u_u2009≠u2009_v_)xa0— the ends of the corresponding edge. It is guaranteed that the graph contains no loops and no multiple edges and that it is connected. The last line contains four integers _s_, _t_, _d__s_, _d__t_ (1u2009≤u2009_s_,u2009_t_u2009≤u2009_n_, _s_u2009≠u2009_t_, 1u2009≤u2009_d__s_,u2009_d__t_u2009≤u2009_n_u2009-u20091). Output If the answer doesn't exist print "No" (without quotes) in the only line of the output. Otherwise, in the first line print "Yes" (without quotes). In the each of the next (_n_u2009-u20091) lines print two integers — the description of the edges of the spanning tree. Each of the edges of the spanning tree must be printed exactly once. You can output edges in any order. You can output the ends of each edge in any order. If there are several solutions, print any of them. Examples Input 7 8 7 4 1 3 5 4 5 7 3 2 2 4 6 1 1 2 6 4 1 4 Output Yes 1 3 5 7 3 2 7 4 2 4 6 1 | 2,300 | false | true | true | false | false | false | false | false | false | true | 6,915 |
381A | Sereja and Dima play a game. The rules of the game are very simple. The players have _n_ cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The game ends when there is no more cards. The player who has the maximum sum of numbers on his cards by the end of the game, wins. Sereja and Dima are being greedy. Each of them chooses the card with the larger number during his move. Inna is a friend of Sereja and Dima. She knows which strategy the guys are using, so she wants to determine the final score, given the initial state of the game. Help her. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20091000) — the number of cards on the table. The second line contains space-separated numbers on the cards from left to right. The numbers on the cards are distinct integers from 1 to 1000. Output On a single line, print two integers. The first number is the number of Sereja's points at the end of the game, the second number is the number of Dima's points at the end of the game. Note In the first sample Sereja will take cards with numbers 10 and 2, so Sereja's sum is 12. Dima will take cards with numbers 4 and 1, so Dima's sum is 5. | 800 | false | true | true | false | false | false | false | false | false | false | 8,305 |
1738B | Suppose $$$a_1, a_2, dots, a_n$$$ is a sorted integer sequence of length $$$n$$$ such that $$$a_1 leq a_2 leq dots leq a_n$$$. For every $$$1 leq i leq n$$$, the prefix sum $$$s_i$$$ of the first $$$i$$$ terms $$$a_1, a_2, dots, a_i$$$ is defined by $$$$$$ s_i = sum_{k=1}^i a_k = a_1 + a_2 + dots + a_i. $$$$$$ Now you are given the last $$$k$$$ terms of the prefix sums, which are $$$s_{n-k+1}, dots, s_{n-1}, s_{n}$$$. Your task is to determine whether this is possible. Formally, given $$$k$$$ integers $$$s_{n-k+1}, dots, s_{n-1}, s_{n}$$$, the task is to check whether there is a sequence $$$a_1, a_2, dots, a_n$$$ such that 1. $$$a_1 leq a_2 leq dots leq a_n$$$, and 2. $$$s_i = a_1 + a_2 + dots + a_i$$$ for all $$$n-k+1 leq i leq n$$$. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^5$$$) — the number of test cases. The following lines contain the description of each test case. The first line of each test case contains two integers $$$n$$$ ($$$1 leq n leq 10^5$$$) and $$$k$$$ ($$$1 leq k leq n$$$), indicating the length of the sequence $$$a$$$ and the number of terms of prefix sums, respectively. The second line of each test case contains $$$k$$$ integers $$$s_{n-k+1}, dots, s_{n-1}, s_{n}$$$ ($$$-10^9 leq s_i leq 10^9$$$ for every $$$n-k+1 leq i leq n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output "YES" (without quotes) if it is possible and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). Example Input 4 5 5 1 2 3 4 5 7 4 -6 -5 -3 0 3 3 2 3 4 3 2 3 4 Note In the first test case, we have the only sequence $$$a = [1, 1, 1, 1, 1]$$$. In the second test case, we can choose, for example, $$$a = [-3, -2, -1, 0, 1, 2, 3]$$$. In the third test case, the prefix sums define the only sequence $$$a = [2, 1, 1]$$$, but it is not sorted. In the fourth test case, it can be shown that there is no sequence with the given prefix sums. | 1,200 | true | true | false | false | false | true | false | false | true | false | 1,860 |
804D | Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem. We have a forest (acyclic undirected graph) with _n_ vertices and _m_ edges. There are _q_ queries we should answer. In each query two vertices _v_ and _u_ are given. Let _V_ be the set of vertices in the connected component of the graph that contains _v_, and _U_ be the set of vertices in the connected component of the graph that contains _u_. Let's add an edge between some vertex and some vertex in and compute the value _d_ of the resulting component. If the resulting component is a tree, the value _d_ is the diameter of the component, and it is equal to -1 otherwise. What is the expected value of _d_, if we choose vertices _a_ and _b_ from the sets uniformly at random? Can you help Pasha to solve this problem? The diameter of the component is the maximum distance among some pair of vertices in the component. The distance between two vertices is the minimum number of edges on some path between the two vertices. Note that queries don't add edges to the initial forest. Input The first line contains three integers _n_, _m_ and _q_(1u2009≤u2009_n_,u2009_m_,u2009_q_u2009≤u2009105)xa0— the number of vertices, the number of edges in the graph and the number of queries. Each of the next _m_ lines contains two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_), that means there is an edge between vertices _u__i_ and _v__i_. It is guaranteed that the given graph is a forest. Each of the next _q_ lines contains two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_)xa0— the vertices given in the _i_-th query. Output For each query print the expected value of _d_ as described in the problem statement. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Let's assume that your answer is _a_, and the jury's answer is _b_. The checker program will consider your answer correct, if . Examples Input 5 2 3 2 4 4 3 4 2 4 1 2 5 Output -1 2.6666666667 2.6666666667 Note In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1u2009-u20092, the other one is 2u2009-u20093. In both ways the resulting diameter is 2, so the answer is 2. In the second example the answer for the first query is obviously -1. The answer for the second query is the average of three cases: for added edges 1u2009-u20092 or 1u2009-u20093 the diameter is 3, and for added edge 1u2009-u20094 the diameter is 2. Thus, the answer is . | 2,500 | false | false | false | true | false | false | true | true | true | false | 6,542 |
1032C | Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence $$$a_1, a_2, ldots, a_n$$$ of key numbers: the more a number is, the closer it is to the right end of the piano keyboard. Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed. Let's denote the fingers of hand by numbers from $$$1$$$ to $$$5$$$. We call a fingering any sequence $$$b_1, ldots, b_n$$$ of fingers numbers. A fingering is convenient if for all $$$1leq i leq n - 1$$$ the following holds: if $$$a_i < a_{i+1}$$$ then $$$b_i < b_{i+1}$$$, because otherwise Paul needs to take his hand off the keyboard to play the $$$(i+1)$$$-st note; if $$$a_i > a_{i+1}$$$ then $$$b_i > b_{i+1}$$$, because of the same; if $$$a_i = a_{i+1}$$$ then $$$b_i eq b_{i+1}$$$, because using the same finger twice in a row is dumb. Please note that there is $$$ eq$$$, not $$$=$$$ between $$$b_i$$$ and $$$b_{i+1}$$$. Please provide any convenient fingering or find out that there is none. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) denoting the number of notes. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 2cdot10^5$$$) denoting the positions of notes on the keyboard. Output If there is no convenient fingering, print $$$-1$$$. Otherwise, print $$$n$$$ numbers $$$b_1, b_2, ldots, b_n$$$, each from $$$1$$$ to $$$5$$$, denoting a convenient fingering, separated by spaces. Examples Input 19 3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8 Output 1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4 Note The third sample test is kinda "Non stop" song by Reflex. | 1,700 | false | false | false | true | false | true | false | false | false | false | 5,540 |
68D | Recently Petya has become keen on physics. Anna V., his teacher noticed Petya's interest and gave him a fascinating physical puzzle — a half-decay tree. A half-decay tree is a complete binary tree with the height _h_. The height of a tree is the length of the path (in edges) from the root to a leaf in the tree. While studying the tree Petya can add electrons to vertices or induce random decay with synchrophasotron. Random decay is a process during which the edges of some path from the root to the random leaf of the tree are deleted. All the leaves are equiprobable. As the half-decay tree is the school property, Petya will return back the deleted edges into the tree after each decay. After being desintegrated, the tree decomposes into connected components. Charge of each component is the total quantity of electrons placed in vertices of the component. Potential of desintegerated tree is the maximum from the charges of its connected components. Each time before inducing random decay Petya is curious about the mathematical expectation of potential of the tree after being desintegrated. Input First line will contain two integers _h_ and _q_ (1u2009≤u2009_h_u2009≤u200930,u20091u2009≤u2009_q_u2009≤u2009105). Next _q_ lines will contain a query of one of two types: add _v_ _e_Petya adds _e_ electrons to vertex number _v_ (1u2009≤u2009_v_u2009≤u20092_h_u2009+u20091u2009-u20091,u20090u2009≤u2009_e_u2009≤u2009104). _v_ and _e_ are integers. The vertices of the tree are numbered in the following way: the root is numbered with 1, the children of the vertex with number _x_ are numbered with 2_x_ and 2_x_u2009+u20091. decayPetya induces tree decay. Output For each query decay solution you should output the mathematical expectation of potential of the tree after being desintegrated. The absolute or relative error in the answer should not exceed 10u2009-u20094. Examples Input 1 4 add 1 3 add 2 10 add 3 11 decay | 2,500 | true | false | false | true | true | false | false | false | false | false | 9,628 |
440C | . Output Print expected minimal number of digits 1. | 1,800 | false | false | false | false | false | false | true | false | false | false | 8,077 |
1537E2 | This is the hard version of the problem. The only difference is the constraints on $$$n$$$ and $$$k$$$. You can make hacks only if all versions of the problem are solved. You have a string $$$s$$$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $$$s:=s+s$$$, where $$$+$$$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $$$k$$$ that can be obtained by doing these operations on string $$$s$$$. 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 The first line contains two integers $$$n$$$, $$$k$$$ ($$$1 leq n, k leq 5cdot 10^5$$$) — the length of the original string $$$s$$$ and the length of the desired string. The second line contains the string $$$s$$$, consisting of $$$n$$$ lowercase English letters. Output Print the lexicographically smallest string of length $$$k$$$ that can be obtained by doing the operations on string $$$s$$$. Note In the first test, it is optimal to make one duplication: "dbcadabc" $$$ o$$$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $$$3$$$ characters, then duplicate the string $$$3$$$ times, then delete the last $$$3$$$ characters to make the string have length $$$k$$$. "abcd" $$$ o$$$ "abc" $$$ o$$$ "ab" $$$ o$$$ "a" $$$ o$$$ "aa" $$$ o$$$ "aaaa" $$$ o$$$ "aaaaaaaa" $$$ o$$$ "aaaaaaa" $$$ o$$$ "aaaaaa" $$$ o$$$ "aaaaa". | 2,200 | false | true | false | false | true | false | false | true | false | false | 2,957 |
1896D | You are given a $$$1$$$-indexed array $$$a$$$ of length $$$n$$$ where each element is $$$1$$$ or $$$2$$$. Process $$$q$$$ queries of the following two types: "1 s": check if there exists a subarray$$$^{dagger}$$$ of $$$a$$$ whose sum equals to $$$s$$$. "2 i v": change $$$a_i$$$ to $$$v$$$. $$$^{dagger}$$$ An array $$$b$$$ is a subarray of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself. Input Each test contains multiple test cases. The first line 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 two integers $$$n$$$ and $$$q$$$ ($$$1le n,qle 10^5$$$)xa0— the length of array $$$a$$$ and the number of queries. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$a_i$$$ is $$$1$$$ or $$$2$$$)xa0— the elements of array $$$a$$$. Each of the following $$$q$$$ lines of each test case contains some integers. The first integer $$$mathrm{op}$$$ is either $$$1$$$ or $$$2$$$. If $$$mathrm{op}$$$ is $$$1$$$, it is followed by one integer $$$s$$$ ($$$1 leq s leq 2n$$$). If $$$mathrm{op}$$$ is $$$2$$$, it is followed by two integers $$$i$$$ and $$$v$$$ ($$$1 leq i leq n$$$, $$$v$$$ is $$$1$$$ or $$$2$$$). It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases both do not exceed $$$10^5$$$. Output For each query with $$$mathrm{op}=1$$$, output "YES" in one line if there exists a subarray of $$$a$$$ whose sum is equals to $$$s$$$, otherwise output "NO". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 2 5 5 2 1 2 1 2 1 5 1 6 1 7 2 4 2 1 7 3 2 2 2 2 1 6 1 5 Output YES YES NO YES YES NO Note Consider the first example: The answer for the first query is "YES" because $$$a_1+a_2+a_3=2+1+2=5$$$. The answer for the second query is "YES" because $$$a_1+a_2+a_3+a_4=2+1+2+1=6$$$. The answer for the third query is "NO" because we cannot find any subarray of $$$a$$$ whose sum is $$$7$$$. After the fourth query, the array $$$a$$$ becomes $$$[2,1,2,2,2]$$$. The answer for the fifth query is "YES" because $$$a_2+a_3+a_4+a_5=1+2+2+2=7$$$. | 1,700 | true | false | false | false | true | false | false | true | false | false | 934 |
305B | Problem - 305B - 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 brute force implementation math *1700 No tag edit access → Contest materials ") ") — the numerator and the denominator of the first fraction. The second line contains integer _n_ (1u2009≤u2009_n_u2009≤u200990) — the height of the second fraction. The third line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20091018) — the continued fraction. 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. Output Print "YES" if these fractions are equal and "NO" otherwise. Examples Input 9 4 2 2 4 Output YES Input 9 4 3 2 3 1 Output YES Input 9 4 3 1 2 4 Output NO Note In the first sample . In the second sample . In the third sample . | 1,700 | true | false | true | false | false | false | true | false | false | false | 8,615 |
1783C | You are participating in Yet Another Tournament. There are $$$n + 1$$$ participants: you and $$$n$$$ other opponents, numbered from $$$1$$$ to $$$n$$$. Each two participants will play against each other exactly once. If the opponent $$$i$$$ plays against the opponent $$$j$$$, he wins if and only if $$$i > j$$$. When the opponent $$$i$$$ plays against you, everything becomes a little bit complicated. In order to get a win against opponent $$$i$$$, you need to prepare for the match for at least $$$a_i$$$ minutes — otherwise, you lose to that opponent. You have $$$m$$$ minutes in total to prepare for matches, but you can prepare for only one match at one moment. In other words, if you want to win against opponents $$$p_1, p_2, dots, p_k$$$, you need to spend $$$a_{p_1} + a_{p_2} + dots + a_{p_k}$$$ minutes for preparation — and if this number is greater than $$$m$$$, you cannot achieve a win against all of these opponents at the same time. The final place of each contestant is equal to the number of contestants with strictly more wins $$$+$$$ $$$1$$$. For example, if $$$3$$$ contestants have $$$5$$$ wins each, $$$1$$$ contestant has $$$3$$$ wins and $$$2$$$ contestants have $$$1$$$ win each, then the first $$$3$$$ participants will get the $$$1$$$-st place, the fourth one gets the $$$4$$$-th place and two last ones get the $$$5$$$-th place. Calculate the minimum possible place (lower is better) you can achieve if you can't prepare for the matches more than $$$m$$$ minutes in total. 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 $$$n$$$ and $$$m$$$ ($$$1 le n le 5 cdot 10^5$$$; $$$0 le m le sumlimits_{i=1}^{n}{a_i}$$$)xa0— the number of your opponents and the total time you have for preparation. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 1000$$$), where $$$a_i$$$ is the time you need to prepare in order to win against the $$$i$$$-th opponent. It's guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$5 cdot 10^5$$$. Output For each test case, print the minimum possible place you can take if you can prepare for the matches no more than $$$m$$$ minutes in total. Example Input 5 4 401 100 100 200 1 3 2 1 2 3 5 0 1 1 1 1 1 4 0 0 1 1 1 4 4 1 2 2 1 Note In the first test case, you can prepare to all opponents, so you'll win $$$4$$$ games and get the $$$1$$$-st place, since all your opponents win no more than $$$3$$$ games. In the second test case, you can prepare against the second opponent and win. As a result, you'll have $$$1$$$ win, opponent $$$1$$$xa0— $$$1$$$ win, opponent $$$2$$$xa0— $$$1$$$ win, opponent $$$3$$$xa0— $$$3$$$ wins. So, opponent $$$3$$$ will take the $$$1$$$-st place, and all other participants, including you, get the $$$2$$$-nd place. In the third test case, you have no time to prepare at all, so you'll lose all games. Since each opponent has at least $$$1$$$ win, you'll take the last place (place $$$6$$$). In the fourth test case, you have no time to prepare, but you can still win against the first opponent. As a result, opponent $$$1$$$ has no wins, you have $$$1$$$ win and all others have at least $$$2$$$ wins. So your place is $$$4$$$. | 1,700 | false | true | false | false | false | false | false | true | true | false | 1,577 |
545B | Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance: We will define the distance between two strings _s_ and _t_ of the same length consisting of digits zero and one as the number of positions _i_, such that _s__i_ isn't equal to _t__i_. As besides everything else Susie loves symmetry, she wants to find for two strings _s_ and _t_ of length _n_ such string _p_ of length _n_, that the distance from _p_ to _s_ was equal to the distance from _p_ to _t_. It's time for Susie to go to bed, help her find such string _p_ or state that it is impossible. Input The first line contains string _s_ of length _n_. The second line contains string _t_ of length _n_. The length of string _n_ is within range from 1 to 105. It is guaranteed that both strings contain only digits zero and one. Output Print a string of length _n_, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line "impossible" (without the quotes). If there are multiple possible answers, print any of them. Note In the first sample different answers are possible, namely — 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101. | 1,100 | false | true | false | false | false | false | false | false | false | false | 7,661 |
342D | Xenia likes puzzles very much. She is especially fond of the puzzles that consist of domino pieces. Look at the picture that shows one of such puzzles. A puzzle is a 3u2009×u2009_n_ table with forbidden cells (black squares) containing dominoes (colored rectangles on the picture). A puzzle is called correct if it meets the following conditions: each domino occupies exactly two non-forbidden cells of the table; no two dominoes occupy the same table cell; exactly one non-forbidden cell of the table is unoccupied by any domino (it is marked by a circle in the picture). To solve the puzzle, you need multiple steps to transport an empty cell from the starting position to some specified position. A move is transporting a domino to the empty cell, provided that the puzzle stays correct. The horizontal dominoes can be moved only horizontally, and vertical dominoes can be moved only vertically. You can't rotate dominoes. The picture shows a probable move. Xenia has a 3u2009×u2009_n_ table with forbidden cells and a cell marked with a circle. Also, Xenia has very many identical dominoes. Now Xenia is wondering, how many distinct correct puzzles she can make if she puts dominoes on the existing table. Also, Xenia wants the circle-marked cell to be empty in the resulting puzzle. The puzzle must contain at least one move. Help Xenia, count the described number of puzzles. As the described number can be rather large, print the remainder after dividing it by 1000000007 (109u2009+u20097). Input The first line contains integer _n_ (3u2009≤u2009_n_u2009≤u2009104) — the puzzle's size. Each of the following three lines contains _n_ characters — the description of the table. The _j_-th character of the _i_-th line equals "X" if the corresponding cell is forbidden; it equals ".", if the corresponding cell is non-forbidden and "O", if the corresponding cell is marked with a circle. It is guaranteed that exactly one cell in the table is marked with a circle. It is guaranteed that all cells of a given table having at least one common point with the marked cell is non-forbidden. Output Print a single number — the answer to the problem modulo 1000000007 (109u2009+u20097). Note Two puzzles are considered distinct if there is a pair of cells that contain one domino in one puzzle and do not contain it in the other one. | 2,100 | false | false | false | true | false | false | false | false | false | false | 8,465 |
815B | Karen has just arrived at school, and she has a math test today! The test is about basic addition and subtraction. Unfortunately, the teachers were too busy writing tasks for Codeforces rounds, and had no time to make an actual test. So, they just put one question in the test that is worth all the points. There are _n_ integers written on a row. Karen must alternately add and subtract each pair of adjacent integers, and write down the sums or differences on the next row. She must repeat this process on the values on the next row, and so on, until only one integer remains. The first operation should be addition. Note that, if she ended the previous row by adding the integers, she should start the next row by subtracting, and vice versa. The teachers will simply look at the last integer, and then if it is correct, Karen gets a perfect score, otherwise, she gets a zero for the test. Karen has studied well for this test, but she is scared that she might make a mistake somewhere and it will cause her final answer to be wrong. If the process is followed, what number can she expect to be written on the last row? Since this number can be quite large, output only the non-negative remainder after dividing it by 109u2009+u20097. Input The first line of input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200000), the number of numbers written on the first row. The next line contains _n_ integers. Specifically, the _i_-th one among these is _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109), the _i_-th number on the first row. Output Output a single integer on a line by itself, the number on the final row after performing the process above. Since this number can be quite large, print only the non-negative remainder after dividing it by 109u2009+u20097. Note In the first test case, the numbers written on the first row are 3, 6, 9, 12 and 15. Karen performs the operations as follows: The non-negative remainder after dividing the final number by 109u2009+u20097 is still 36, so this is the correct output. In the second test case, the numbers written on the first row are 3, 7, 5 and 2. Karen performs the operations as follows: The non-negative remainder after dividing the final number by 109u2009+u20097 is 109u2009+u20096, so this is the correct output. | 2,200 | true | false | false | false | false | true | true | false | false | false | 6,499 |
1028F | Consider a set of points $$$A$$$, initially it is empty. There are three types of queries: 1. Insert a point $$$(x_i, y_i)$$$ to $$$A$$$. It is guaranteed that this point does not belong to $$$A$$$ at this moment. 2. Remove a point $$$(x_i, y_i)$$$ from $$$A$$$. It is guaranteed that this point belongs to $$$A$$$ at this moment. 3. Given a point $$$(x_i, y_i)$$$, calculate the minimum number of points required to add to $$$A$$$ to make $$$A$$$ symmetrical with respect to the line containing points $$$(0, 0)$$$ and $$$(x_i, y_i)$$$. Note that these points are not actually added to $$$A$$$, i.e. these queries are independent from each other. Input The first line contains a single integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$) — the number of queries. Each of the following $$$q$$$ lines describes a query and contains three integers $$$t_i$$$, $$$x_i$$$ and $$$y_i$$$ ($$$ t_i in {1, 2, 3}$$$, $$$1 le x_i, y_i le 112,904$$$) — the type of the query and the coordinates of the point. Type $$$1$$$ is addition of the point, type $$$2$$$ is removal of the point, type $$$3$$$ is the query to compute the minimum number of points required to make $$$A$$$ symmetrical. It is guaranteed that there are no more than $$$10^5$$$ queries of type $$$3$$$ and no more than $$$10^5$$$ queries having type $$$1$$$ or $$$2$$$. Output For each query of the third type output a line with a single integer — the answer to this query. Examples Input 12 1 1 6 1 6 1 1 5 5 1 2 3 3 4 4 1 3 2 3 7 7 2 2 3 2 6 1 3 8 8 2 5 5 3 1 1 Input 6 1 1 2 3 1 1 1 1 1 3 2 2 2 1 1 3 2 4 Note The first example is shown on the picture below. | 2,900 | false | false | false | false | false | false | true | false | false | false | 5,564 |
2005D | You are given two arrays $$$a_1, a_2, ldots, a_n$$$ and $$$b_1, b_2, ldots, b_n$$$. You must perform the following operation exactly once: choose any indices $$$l$$$ and $$$r$$$ such that $$$1 le l le r le n$$$; swap $$$a_i$$$ and $$$b_i$$$ for all $$$i$$$ such that $$$l leq i leq r$$$. Find the maximum possible value of $$$ ext{gcd}(a_1, a_2, ldots, a_n) + ext{gcd}(b_1, b_2, ldots, b_n)$$$ after performing the operation exactly once. Also find the number of distinct pairs $$$(l, r)$$$ which achieve the maximum value. Input In the first line of the input, you are given a single integer $$$t$$$ ($$$1 le t le 10^5$$$), the number of test cases. Then the description of each test case follows. In the first line of each test case, you are given a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$), representing the number of integers in each array. In the next line, you are given $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the elements of the array $$$a$$$. In the last line, you are given $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le 10^9$$$)xa0— the elements of the array $$$b$$$. The sum of values of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, output a line with two integers: the maximum value of $$$ ext{gcd}(a_1, a_2, ldots, a_n) + ext{gcd}(b_1, b_2, ldots, b_n)$$$ after performing the operation exactly once, and the number of ways. Example Input 5 8 11 4 16 17 3 24 25 8 8 10 4 21 17 18 25 21 4 6 4 24 13 15 3 1 14 2 13 14 5 8 8 20 17 15 11 21 10 3 7 9 9 4 20 14 9 13 1 2 18 13 15 20 Output 2 36 3 2 2 3 2 36 6 1 Note In the first, third, and fourth test cases, there's no way to achieve a higher GCD than $$$1$$$ in any of the arrays, so the answer is $$$1 + 1 = 2$$$. Any pair $$$(l, r)$$$ achieves the same result; for example, in the first test case there are $$$36$$$ such pairs. In the last test case, you must choose $$$l = 1$$$, $$$r = 2$$$ to maximize the answer. In this case, the GCD of the first array is $$$5$$$, and the GCD of the second array is $$$1$$$, so the answer is $$$5 + 1 = 6$$$, and the number of ways is $$$1$$$. | 2,400 | false | false | true | false | true | false | true | true | false | false | 220 |
1176F | You are playing a computer card game called Splay the Sire. Currently you are struggling to defeat the final boss of the game. The boss battle consists of $$$n$$$ turns. During each turn, you will get several cards. Each card has two parameters: its cost $$$c_i$$$ and damage $$$d_i$$$. You may play some of your cards during each turn in some sequence (you choose the cards and the exact order they are played), as long as the total cost of the cards you play during the turn does not exceed $$$3$$$. After playing some (possibly zero) cards, you end your turn, and all cards you didn't play are discarded. Note that you can use each card at most once. Your character has also found an artifact that boosts the damage of some of your actions: every $$$10$$$-th card you play deals double damage. What is the maximum possible damage you can deal during $$$n$$$ turns? Input The first line contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of turns. Then $$$n$$$ blocks of input follow, the $$$i$$$-th block representing the cards you get during the $$$i$$$-th turn. Each block begins with a line containing one integer $$$k_i$$$ ($$$1 le k_i le 2 cdot 10^5$$$) — the number of cards you get during $$$i$$$-th turn. Then $$$k_i$$$ lines follow, each containing two integers $$$c_j$$$ and $$$d_j$$$ ($$$1 le c_j le 3$$$, $$$1 le d_j le 10^9$$$) — the parameters of the corresponding card. It is guaranteed that $$$sum limits_{i = 1}^{n} k_i le 2 cdot 10^5$$$. Output Print one integer — the maximum damage you may deal. Example Input 5 3 1 6 1 7 1 5 2 1 4 1 3 3 1 10 3 5 2 3 3 1 15 2 4 1 10 1 1 100 Note In the example test the best course of action is as follows: During the first turn, play all three cards in any order and deal $$$18$$$ damage. During the second turn, play both cards and deal $$$7$$$ damage. During the third turn, play the first and the third card and deal $$$13$$$ damage. During the fourth turn, play the first and the third card and deal $$$25$$$ damage. During the fifth turn, play the only card, which will deal double damage ($$$200$$$). | 2,100 | false | false | true | true | false | false | false | false | true | false | 4,833 |
573A | Problem - 573A - 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 number theory *1300 No tag edit access → Contest materials and right now all of them have bids on the table. _i_-th of them has bid with size _a__i_ dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009105), the number of players. The second line contains _n_ integer numbers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal. | 1,300 | true | false | true | false | false | false | false | false | false | false | 7,559 |
580C | Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of _n_ vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them. The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than _m_ consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go. Input The first line contains two integers, _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_m_u2009≤u2009_n_) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_, where each _a__i_ either equals to 0 (then vertex _i_ has no cat), or equals to 1 (then vertex _i_ has a cat). Next _n_u2009-u20091 lines contains the edges of the tree in the format "_x__i_ _y__i_" (without the quotes) (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_, _x__i_u2009≠u2009_y__i_), where _x__i_ and _y__i_ are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree. Output A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most _m_ consecutive vertices with cats. Examples Input 7 1 1 0 1 1 0 0 0 1 2 1 3 2 4 2 5 3 6 3 7 Note Let us remind you that a tree is a connected graph on _n_ vertices and _n_u2009-u20091 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children. Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2. Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7. | 1,500 | false | false | false | false | false | false | false | false | false | true | 7,526 |
1957F2 | This is the hard version of the problem. The difference between the two versions of this problem is the constraint on $$$k$$$. You can make hacks only if all versions of the problem are solved. You are given an undirected tree of $$$n$$$ nodes. Each node $$$v$$$ has a value $$$a_v$$$ written on it. You have to answer queries related to the tree. You are given $$$q$$$ queries. In each query, you are given $$$5$$$ integers, $$$u_1, v_1, u_2, v_2, k$$$. Denote the count of nodes with value $$$c$$$ on path $$$u_1 ightarrow v_1$$$ with $$$x_c$$$, and the count of nodes with value $$$c$$$ on path $$$u_2 ightarrow v_2$$$ with $$$y_c$$$. If there are $$$z$$$ such values of $$$c$$$ such that $$$x_c eq y_c$$$, output any $$$min(z, k)$$$ such values in any order. Input The first line contains one integer $$$n$$$ ($$$1 leq n leq 10^5$$$)xa0— the number of nodes in the tree. The next line contains $$$n$$$ integers, $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^5$$$)xa0— the value written on each node of the tree. Then $$$n - 1$$$ lines follow. Each line contains two integers $$$u$$$ and $$$v$$$ ($$$1 leq u, v leq n, u eq v$$$) denoting an edge of the tree. It is guaranteed that the given edges form a tree. The next line contains one integer $$$q$$$ ($$$1 leq q leq 10^5$$$)xa0— the number of queries. Then $$$q$$$ lines follow. Each line contains five integers $$$u_1, v_1, u_2, v_2, k$$$ ($$$1 leq u_1, v_1, u_2, v_2 leq n$$$, $$$1 leq k leq 10$$$). Output For each query, output on a separate line. For a query, first output $$$min(z, k)$$$ and then on the same line, output any $$$min(z, k)$$$ values in any order which occur a different number of times in each path. Example Input 5 5 2 3 4 3 1 2 1 3 2 4 2 5 4 1 4 4 5 3 2 3 2 3 1 1 4 4 5 1 5 5 4 3 10 Output 2 3 5 0 1 5 3 5 2 4 Note For query $$$1$$$, the first path is $$$1 ightarrow 2 ightarrow 4$$$, coming across the multiset of values $$${5, 2, 4}$$$. On the second path $$$4 ightarrow 2 ightarrow 5$$$, we have the multiset $$${4, 2, 3}$$$. Two numbersxa0— $$$3$$$ and $$$5$$$ occur a different number of times, hence we print them both. In query $$$2$$$, there is no difference between the paths, hence we output $$$0$$$. In query $$$3$$$, we have the same paths as query $$$1$$$, but we need to output only $$$1$$$ value, hence we output $$$5$$$. In query $$$4$$$, the first path is just the node $$$5$$$, resulting in the multiset $$${3}$$$, and the second path $$$4 ightarrow 2 ightarrow 1 ightarrow 3$$$ gives $$${4, 2, 5, 3}$$$. The numbers $$$5$$$, $$$2$$$ and $$$4$$$ occur a different number of times. | 2,700 | false | false | false | false | true | false | false | true | false | false | 527 |
447A | Problem - 447A - 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 ") ") Editorial") , where _h_(_x_) is the hash function. In this problem we will assume, that _h_(_x_)u2009=u2009_x_xa0_mod_xa0_p_. Operation _a_xa0_mod_xa0_b_ denotes taking a remainder after division _a_ by _b_. However, each bucket can contain no more than one element. If DZY wants to insert an number into a bucket which is already filled, we say a "conflict" happens. Suppose the first conflict happens right after the _i_-th insertion, you should output _i_. If no conflict happens, just output -1. Input The first line contains two integers, _p_ and _n_ (2u2009≤u2009_p_,u2009_n_u2009≤u2009300). Then _n_ lines follow. The _i_-th of them contains an integer _x__i_ (0u2009≤u2009_x__i_u2009≤u2009109). Output Output a single integer — the answer to the problem. Examples Input 10 5 0 21 53 41 53 Output 4 Input 5 5 0 1 2 3 4 Output -1 | 800 | false | false | true | false | false | false | false | false | false | false | 8,051 |
1863D | There is an $$$n imes m$$$ board divided into cells. There are also some dominoes on this board. Each domino covers two adjacent cells (that is, two cells that share a side), and no two dominoes overlap. Piet thinks that this board is too boring and it needs to be painted. He will paint the cells of the dominoes black and white. He calls the painting beautiful if all of the following conditions hold: for each domino, one of its cells is painted white and the other is painted black; for each row, the number of black cells in this row equals the number of white cells in this row; for each column, the number of black cells in this column equals the number of white cells in this column. Note that the cells that are not covered by dominoes are not painted at all, they are counted as neither black nor white. Help Piet produce a beautiful painting or tell that it is impossible. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10,000$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2le n, mle 500$$$). The next $$$n$$$ lines describe the tiling of the board, row by row from top to bottom. Each of these lines contains $$$m$$$ characters, describing the cells in the corresponding row from left to right. Each character is one of U, D, L, R, or ., meaning that the cell is covered with a top, bottom, left, right half of a domino or nothing, respectively. The tiling is guaranteed to be valid. It is guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$250,000$$$. Output For each test case, output a single integer $$$-1$$$ if a beautiful painting does not exist. Otherwise, print $$$n$$$ lines, each containing $$$m$$$ characters, describing the colors in the corresponding row of a beautiful painting. Every character corresponding to a cell not covered by a domino must be . (a dot), and all other characters must be B if the corresponding cell is black or W if it is white. If there are multiple solutions, print any of them. Example Input 3 4 6 ..LR.. ULRU.. DLRDUU ..LRDD 5 4 .LR. .UU. UDDU D..D LR.. 2 2 .. .. Output ..WB.. WWBB.. BBWWWB ..BWBW -1 .. .. Note In the first test case, the answer is illustrated below: In the second test case, it is impossible to paint all cells the right way. | 1,400 | false | true | false | false | false | true | false | false | false | false | 1,103 |
1322F | Mayor of city M. decided to launch several new metro lines during 2020. Since the city has a very limited budget, it was decided not to dig new tunnels but to use the existing underground network. The tunnel system of the city M. consists of $$$n$$$ metro stations. The stations are connected with $$$n - 1$$$ bidirectional tunnels. Between every two stations $$$v$$$ and $$$u$$$ there is exactly one simple path. Each metro line the mayor wants to create is a simple path between stations $$$a_i$$$ and $$$b_i$$$. Metro lines can intersects freely, that is, they can share common stations or even common tunnels. However, it's not yet decided which of two directions each line will go. More precisely, between the stations $$$a_i$$$ and $$$b_i$$$ the trains will go either from $$$a_i$$$ to $$$b_i$$$, or from $$$b_i$$$ to $$$a_i$$$, but not simultaneously. The city $$$M$$$ uses complicated faring rules. Each station is assigned with a positive integer $$$c_i$$$xa0— the fare zone of the station. The cost of travel from $$$v$$$ to $$$u$$$ is defined as $$$c_u - c_v$$$ roubles. Of course, such travel only allowed in case there is a metro line, the trains on which go from $$$v$$$ to $$$u$$$. Mayor doesn't want to have any travels with a negative cost, so it was decided to assign directions of metro lines and station fare zones in such a way, that fare zones are strictly increasing during any travel on any metro line. Mayor wants firstly assign each station a fare zone and then choose a lines direction, such that all fare zones are increasing along any line. In connection with the approaching celebration of the day of the city, the mayor wants to assign fare zones so that the maximum $$$c_i$$$ will be as low as possible. Please help mayor to design a new assignment or determine if it's impossible to do. Please note that you only need to assign the fare zones optimally, you don't need to print lines' directions. This way, you solution will be considered correct if there will be a way to assign directions of every metro line, so that the fare zones will be strictly increasing along any movement of the trains. Input The first line contains an integers $$$n$$$, $$$m$$$ ($$$2 le n, le 500,000, 1 le m le 500,000$$$)xa0— the number of stations in the city and the number of metro lines. Each of the following $$$n-1$$$ lines describes another metro tunnel. Each tunnel is described with integers $$$v_i$$$, $$$u_i$$$ ($$$1 le v_i, u_i le n$$$, $$$v_i e u_i$$$). It's guaranteed, that there is exactly one simple path between any two stations. Each of the following $$$m$$$ lines describes another metro line. Each line is described with integers $$$a_i$$$, $$$b_i$$$ ($$$1 le a_i, b_i le n$$$, $$$a_i eq b_i$$$). Output In the first line print integer $$$k$$$xa0— the maximum fare zone used. In the next line print integers $$$c_1, c_2, ldots, c_n$$$ ($$$1 le c_i le k$$$) xa0— stations' fare zones. In case there are several possible answers, print any of them. In case it's impossible to assign fare zones, print "-1". Examples Input 4 3 1 2 1 3 1 4 2 3 2 4 3 4 Input 7 5 3 4 4 2 2 5 5 1 2 6 4 7 1 3 6 7 3 7 2 1 3 6 Note In the first example, line $$$1 ightarrow 3$$$ goes through the stations 1, 2, 3 in this order. In this order the fare zones of the stations are increasing. Since this line has 3 stations, at least three fare zones are needed. So the answer 1, 2, 3 is optimal. In the second example, it's impossible to assign fare zones to be consistent with a metro lines. | 3,500 | false | false | false | true | false | false | false | false | false | false | 4,102 |
240E | A country named Berland has _n_ cities. They are numbered with integers from 1 to _n_. City with index 1 is the capital of the country. Some pairs of cities have monodirectional roads built between them. However, not all of them are in good condition. For each road we know whether it needs repairing or not. If a road needs repairing, then it is forbidden to use it. However, the Berland government can repair the road so that it can be used. Right now Berland is being threatened by the war with the neighbouring state. So the capital officials decided to send a military squad to each city. The squads can move only along the existing roads, as there's no time or money to build new roads. However, some roads will probably have to be repaired in order to get to some cities. Of course the country needs much resources to defeat the enemy, so you want to be careful with what you're going to throw the forces on. That's why the Berland government wants to repair the minimum number of roads that is enough for the military troops to get to any city from the capital, driving along good or repaired roads. Your task is to help the Berland government and to find out, which roads need to be repaired. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the number of cities and the number of roads in Berland. Next _m_ lines contain three space-separated integers _a__i_,u2009_b__i_,u2009_c__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_,u20090u2009≤u2009_c__i_u2009≤u20091), describing the road from city _a__i_ to city _b__i_. If _c__i_ equals 0, than the given road is in a good condition. If _c__i_ equals 1, then it needs to be repaired. It is guaranteed that there is not more than one road between the cities in each direction. Output If even after all roads are repaired, it is still impossible to get to some city from the capital, print u2009-u20091. Otherwise, on the first line print the minimum number of roads that need to be repaired, and on the second line print the numbers of these roads, separated by single spaces. The roads are numbered starting from 1 in the order, in which they are given in the input. If there are multiple sets, consisting of the minimum number of roads to repair to make travelling to any city from the capital possible, print any of them. If it is possible to reach any city, driving along the roads that already are in a good condition, print 0 in the only output line. Examples Input 4 4 2 3 0 3 4 0 4 1 0 4 2 1 | 2,800 | false | true | false | false | false | false | false | false | false | true | 8,877 |
1044C | You are given $$$n$$$ points on the plane. The polygon formed from all the $$$n$$$ points is strictly convex, that is, the polygon is convex, and there are no three collinear points (i.e. lying in the same straight line). The points are numbered from $$$1$$$ to $$$n$$$, in clockwise order. We define the distance between two points $$$p_1 = (x_1, y_1)$$$ and $$$p_2 = (x_2, y_2)$$$ as their Manhattan distance: $$$$$$d(p_1, p_2) = x_1 - x_2 + y_1 - y_2.$$$$$$ Furthermore, we define the perimeter of a polygon, as the sum of Manhattan distances between all adjacent pairs of points on it; if the points on the polygon are ordered as $$$p_1, p_2, ldots, p_k$$$ $$$(k geq 3)$$$, then the perimeter of the polygon is $$$d(p_1, p_2) + d(p_2, p_3) + ldots + d(p_k, p_1)$$$. For some parameter $$$k$$$, let's consider all the polygons that can be formed from the given set of points, having any $$$k$$$ vertices, such that the polygon is not self-intersecting. For each such polygon, let's consider its perimeter. Over all such perimeters, we define $$$f(k)$$$ to be the maximal perimeter. Please note, when checking whether a polygon is self-intersecting, that the edges of a polygon are still drawn as straight lines. For instance, in the following pictures: In the middle polygon, the order of points ($$$p_1, p_3, p_2, p_4$$$) is not valid, since it is a self-intersecting polygon. The right polygon (whose edges resemble the Manhattan distance) has the same order and is not self-intersecting, but we consider edges as straight lines. The correct way to draw this polygon is ($$$p_1, p_2, p_3, p_4$$$), which is the left polygon. Your task is to compute $$$f(3), f(4), ldots, f(n)$$$. In other words, find the maximum possible perimeter for each possible number of points (i.e. $$$3$$$ to $$$n$$$). Input The first line contains a single integer $$$n$$$ ($$$3 leq n leq 3cdot 10^5$$$)xa0— the number of points. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^8 leq x_i, y_i leq 10^8$$$)xa0— the coordinates of point $$$p_i$$$. The set of points is guaranteed to be convex, all points are distinct, the points are ordered in clockwise order, and there will be no three collinear points. Note In the first example, for $$$f(3)$$$, we consider four possible polygons: ($$$p_1, p_2, p_3$$$), with perimeter $$$12$$$. ($$$p_1, p_2, p_4$$$), with perimeter $$$8$$$. ($$$p_1, p_3, p_4$$$), with perimeter $$$12$$$. ($$$p_2, p_3, p_4$$$), with perimeter $$$12$$$. For $$$f(4)$$$, there is only one option, taking all the given points. Its perimeter $$$14$$$. In the second example, there is only one possible polygon. Its perimeter is $$$8$$$. | 2,100 | false | false | false | true | false | false | false | false | false | false | 5,474 |
229B | Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has _n_ planets, indexed with numbers from 1 to _n_. Jack is on the planet with index 1, and Apophis will land on the planet with index _n_. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time _t_ another traveller arrives to the planet, Jack can only pass through the stargate at time _t_u2009+u20091, unless there are more travellers arriving at time _t_u2009+u20091 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index _n_. Input The first line contains two space-separated integers: _n_ (2u2009≤u2009_n_u2009≤u2009105), the number of planets in the galaxy, and _m_ (0u2009≤u2009_m_u2009≤u2009105) — the number of pairs of planets between which Jack can travel using stargates. Then _m_ lines follow, containing three integers each: the _i_-th line contains numbers of planets _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_), which are connected through stargates, and the integer transfer time (in seconds) _c__i_ (1u2009≤u2009_c__i_u2009≤u2009104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then _n_ lines follow: the _i_-th line contains an integer _k__i_ (0u2009≤u2009_k__i_u2009≤u2009105) that denotes the number of moments of time when other travellers arrive to the planet with index _i_. Then _k__i_ distinct space-separated integers _t__ij_ (0u2009≤u2009_t__ij_u2009<u2009109) follow, sorted in ascending order. An integer _t__ij_ means that at time _t__ij_ (in seconds) another traveller arrives to the planet _i_. It is guaranteed that the sum of all _k__i_ does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet _n_. If Jack can't get to planet _n_ in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2u2009+u20095u2009=u20097 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates. | 1,700 | false | false | false | false | true | false | false | true | false | true | 8,922 |
1550A | Let's call an array $$$a$$$ consisting of $$$n$$$ positive (greater than $$$0$$$) integers beautiful if the following condition is held for every $$$i$$$ from $$$1$$$ to $$$n$$$: either $$$a_i = 1$$$, or at least one of the numbers $$$a_i - 1$$$ and $$$a_i - 2$$$ exists in the array as well. For example: the array $$$[5, 3, 1]$$$ is beautiful: for $$$a_1$$$, the number $$$a_1 - 2 = 3$$$ exists in the array; for $$$a_2$$$, the number $$$a_2 - 2 = 1$$$ exists in the array; for $$$a_3$$$, the condition $$$a_3 = 1$$$ holds; the array $$$[1, 2, 2, 2, 2]$$$ is beautiful: for $$$a_1$$$, the condition $$$a_1 = 1$$$ holds; for every other number $$$a_i$$$, the number $$$a_i - 1 = 1$$$ exists in the array; the array $$$[1, 4]$$$ is not beautiful: for $$$a_2$$$, neither $$$a_2 - 2 = 2$$$ nor $$$a_2 - 1 = 3$$$ exists in the array, and $$$a_2 e 1$$$; the array $$$[2]$$$ is not beautiful: for $$$a_1$$$, neither $$$a_1 - 1 = 1$$$ nor $$$a_1 - 2 = 0$$$ exists in the array, and $$$a_1 e 1$$$; the array $$$[2, 1, 3]$$$ is beautiful: for $$$a_1$$$, the number $$$a_1 - 1 = 1$$$ exists in the array; for $$$a_2$$$, the condition $$$a_2 = 1$$$ holds; for $$$a_3$$$, the number $$$a_3 - 2 = 1$$$ exists in the array. You are given a positive integer $$$s$$$. Find the minimum possible size of a beautiful array with the sum of elements equal to $$$s$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 5000$$$) — the number of test cases. Then $$$t$$$ lines follow, the $$$i$$$-th line contains one integer $$$s$$$ ($$$1 le s le 5000$$$) for the $$$i$$$-th test case. Output Print $$$t$$$ integers, the $$$i$$$-th integer should be the answer for the $$$i$$$-th testcase: the minimum possible size of a beautiful array with the sum of elements equal to $$$s$$$. Note Consider the example test: 1. in the first test case, the array $$$[1]$$$ meets all conditions; 2. in the second test case, the array $$$[3, 4, 1]$$$ meets all conditions; 3. in the third test case, the array $$$[1, 2, 4]$$$ meets all conditions; 4. in the fourth test case, the array $$$[1, 4, 6, 8, 10, 2, 11]$$$ meets all conditions. | 800 | true | true | false | false | false | false | false | false | false | false | 2,898 |
1609D | William arrived at a conference dedicated to cryptocurrencies. Networking, meeting new people, and using friends' connections are essential to stay up to date with the latest news from the world of cryptocurrencies. The conference has $$$n$$$ participants, who are initially unfamiliar with each other. William can introduce any two people, $$$a$$$ and $$$b$$$, who were not familiar before, to each other. William has $$$d$$$ conditions, $$$i$$$'th of which requires person $$$x_i$$$ to have a connection to person $$$y_i$$$. Formally, two people $$$x$$$ and $$$y$$$ have a connection if there is such a chain $$$p_1=x, p_2, p_3, dots, p_k=y$$$ for which for all $$$i$$$ from $$$1$$$ to $$$k - 1$$$ it's true that two people with numbers $$$p_i$$$ and $$$p_{i + 1}$$$ know each other. For every $$$i$$$ ($$$1 le i le d$$$) William wants you to calculate the maximal number of acquaintances one person can have, assuming that William satisfied all conditions from $$$1$$$ and up to and including $$$i$$$ and performed exactly $$$i$$$ introductions. The conditions are being checked after William performed $$$i$$$ introductions. The answer for each $$$i$$$ must be calculated independently. It means that when you compute an answer for $$$i$$$, you should assume that no two people have been introduced to each other yet. Input The first line contains two integers $$$n$$$ and $$$d$$$ ($$$2 le n le 10^3, 1 le d le n - 1$$$), the number of people, and number of conditions, respectively. Each of the next $$$d$$$ lines each contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n, x_i eq y_i$$$), the numbers of people which must have a connection according to condition $$$i$$$. Output Output $$$d$$$ integers. $$$i$$$th number must equal the number of acquaintances the person with the maximal possible acquaintances will have, if William performed $$$i$$$ introductions and satisfied the first $$$i$$$ conditions. Note The explanation for the first test case: In this explanation, the circles and the numbers in them denote a person with the corresponding number. The line denotes that William introduced two connected people. The person marked with red has the most acquaintances. These are not the only correct ways to introduce people. | 1,600 | false | true | true | false | false | false | false | false | false | true | 2,605 |
492D | Vanya and his friend Vova play a computer game where they need to destroy _n_ monsters to pass a level. Vanya's character performs attack with frequency _x_ hits per second and Vova's character performs attack with frequency _y_ hits per second. Each character spends fixed time to raise a weapon and then he hits (the time to raise the weapon is 1u2009/u2009_x_ seconds for the first character and 1u2009/u2009_y_ seconds for the second one). The _i_-th monster dies after he receives _a__i_ hits. Vanya and Vova wonder who makes the last hit on each monster. If Vanya and Vova make the last hit at the same time, we assume that both of them have made the last hit. Input The first line contains three integers _n_,_x_,_y_ (1u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_x_,u2009_y_u2009≤u2009106) — the number of monsters, the frequency of Vanya's and Vova's attack, correspondingly. Next _n_ lines contain integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— the number of hits needed do destroy the _i_-th monster. Output Print _n_ lines. In the _i_-th line print word "Vanya", if the last hit on the _i_-th monster was performed by Vanya, "Vova", if Vova performed the last hit, or "Both", if both boys performed it at the same time. Note In the first sample Vanya makes the first hit at time 1u2009/u20093, Vova makes the second hit at time 1u2009/u20092, Vanya makes the third hit at time 2u2009/u20093, and both boys make the fourth and fifth hit simultaneously at the time 1. In the second sample Vanya and Vova make the first and second hit simultaneously at time 1. | 1,800 | true | false | true | false | false | false | false | true | true | false | 7,865 |
1474F | Igor had a sequence $$$d_1, d_2, dots, d_n$$$ of integers. When Igor entered the classroom there was an integer $$$x$$$ written on the blackboard. Igor generated sequence $$$p$$$ using the following algorithm: 1. initially, $$$p = [x]$$$; 2. for each $$$1 leq i leq n$$$ he did the following operation $$$d_i$$$ times: if $$$d_i geq 0$$$, then he looked at the last element of $$$p$$$ (let it be $$$y$$$) and appended $$$y + 1$$$ to the end of $$$p$$$; if $$$d_i < 0$$$, then he looked at the last element of $$$p$$$ (let it be $$$y$$$) and appended $$$y - 1$$$ to the end of $$$p$$$. For example, if $$$x = 3$$$, and $$$d = [1, -1, 2]$$$, $$$p$$$ will be equal $$$[3, 4, 3, 4, 5]$$$. Igor decided to calculate the length of the longest increasing subsequence of $$$p$$$ and the number of them. A sequence $$$a$$$ is a subsequence of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements. A sequence $$$a$$$ is an increasing sequence if each element of $$$a$$$ (except the first one) is strictly greater than the previous element. For $$$p = [3, 4, 3, 4, 5]$$$, the length of longest increasing subsequence is $$$3$$$ and there are $$$3$$$ of them: $$$[underline{3}, underline{4}, 3, 4, underline{5}]$$$, $$$[underline{3}, 4, 3, underline{4}, underline{5}]$$$, $$$[3, 4, underline{3}, underline{4}, underline{5}]$$$. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 50$$$)xa0— the length of the sequence $$$d$$$. The second line contains a single integer $$$x$$$ ($$$-10^9 leq x leq 10^9$$$)xa0— the integer on the blackboard. The third line contains $$$n$$$ integers $$$d_1, d_2, ldots, d_n$$$ ($$$-10^9 leq d_i leq 10^9$$$). Note The first test case was explained in the statement. In the second test case $$$p = [100, 101, 102, 103, 104, 105, 104, 103, 102, 103, 104, 105, 106, 107, 108]$$$. In the third test case $$$p = [1, 2, ldots, 2000000000]$$$. | 3,000 | true | false | false | true | false | false | false | false | false | false | 3,305 |
163B | As you know, lemmings like jumping. For the next spectacular group jump _n_ lemmings gathered near a high rock with _k_ comfortable ledges on it. The first ledge is situated at the height of _h_ meters, the second one is at the height of 2_h_ meters, and so on (the _i_-th ledge is at the height of _i_·_h_ meters). The lemmings are going to jump at sunset, and there's not much time left. Each lemming is characterized by its climbing speed of _v__i_ meters per minute and its weight _m__i_. This means that the _i_-th lemming can climb to the _j_-th ledge in minutes. To make the jump beautiful, heavier lemmings should jump from higher ledges: if a lemming of weight _m__i_ jumps from ledge _i_, and a lemming of weight _m__j_ jumps from ledge _j_ (for _i_u2009<u2009_j_), then the inequation _m__i_u2009≤u2009_m__j_ should be fulfilled. Since there are _n_ lemmings and only _k_ ledges (_k_u2009≤u2009_n_), the _k_ lemmings that will take part in the jump need to be chosen. The chosen lemmings should be distributed on the ledges from 1 to _k_, one lemming per ledge. The lemmings are to be arranged in the order of non-decreasing weight with the increasing height of the ledge. In addition, each lemming should have enough time to get to his ledge, that is, the time of his climb should not exceed _t_ minutes. The lemmings climb to their ledges all at the same time and they do not interfere with each other. Find the way to arrange the lemmings' jump so that time _t_ is minimized. Input The first line contains space-separated integers _n_, _k_ and _h_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_h_u2009≤u2009104) — the total number of lemmings, the number of ledges and the distance between adjacent ledges. The second line contains _n_ space-separated integers _m_1,u2009_m_2,u2009...,u2009_m__n_ (1u2009≤u2009_m__i_u2009≤u2009109), where _m__i_ is the weight of _i_-th lemming. The third line contains _n_ space-separated integers _v_1,u2009_v_2,u2009...,u2009_v__n_ (1u2009≤u2009_v__i_u2009≤u2009109), where _v__i_ is the speed of _i_-th lemming. Output Print _k_ different numbers from 1 to _n_ — the numbers of the lemmings who go to ledges at heights _h_,u20092_h_,u2009...,u2009_kh_, correspondingly, if the jump is organized in an optimal way. If there are multiple ways to select the lemmings, pick any of them. Examples Input 5 3 2 1 2 3 2 1 1 2 1 2 10 Input 5 3 10 3 4 3 2 1 5 4 3 2 1 Note Let's consider the first sample case. The fifth lemming (speed 10) gets to the ledge at height 2 in minutes; the second lemming (speed 2) gets to the ledge at height 4 in 2 minutes; the fourth lemming (speed 2) gets to the ledge at height 6 in 3 minutes. All lemmings manage to occupy their positions in 3 minutes. | 2,000 | false | false | false | false | false | false | false | true | false | false | 9,214 |
746E | Eugeny has _n_ cards, each of them has exactly one integer written on it. Eugeny wants to exchange some cards with Nikolay so that the number of even integers on his cards would equal the number of odd integers, and that all these numbers would be distinct. Nikolay has _m_ cards, distinct numbers from 1 to _m_ are written on them, one per card. It means that Nikolay has exactly one card with number 1, exactly one card with number 2 and so on. A single exchange is a process in which Eugeny gives one card to Nikolay and takes another one from those Nikolay has. Your task is to find the minimum number of card exchanges and determine which cards Eugeny should exchange. Input The first line contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u20092·105, 1u2009≤u2009_m_u2009≤u2009109)xa0— the number of cards Eugeny has and the number of cards Nikolay has. It is guaranteed that _n_ is even. The second line contains a sequence of _n_ positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— the numbers on Eugeny's cards. Output If there is no answer, print -1. Otherwise, in the first line print the minimum number of exchanges. In the second line print _n_ integersxa0— Eugeny's cards after all the exchanges with Nikolay. The order of cards should coincide with the card's order in the input data. If the _i_-th card wasn't exchanged then the _i_-th number should coincide with the number from the input data. Otherwise, it is considered that this card was exchanged, and the _i_-th number should be equal to the number on the card it was exchanged to. If there are multiple answers, it is allowed to print any of them. | 1,900 | true | true | true | false | false | false | false | false | false | false | 6,813 |
1475E | Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of $$$n$$$ different bloggers. Blogger numbered $$$i$$$ has $$$a_i$$$ followers. Since Masha has a limited budget, she can only sign a contract with $$$k$$$ different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers. Help her, find the number of ways to select $$$k$$$ bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers). For example, if $$$n=4$$$, $$$k=3$$$, $$$a=[1, 3, 1, 2]$$$, then Masha has two ways to select $$$3$$$ bloggers with the maximum total number of followers: conclude contracts with bloggers with numbers $$$1$$$, $$$2$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_1 + a_2 + a_4 = 6$$$. conclude contracts with bloggers with numbers $$$2$$$, $$$3$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_2 + a_3 + a_4 = 6$$$. Since the answer can be quite large, output it modulo $$$10^9+7$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 1000$$$)xa0— the number of bloggers and how many of them you can sign a contract with. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots a_n$$$ ($$$1 le a_i le n$$$)xa0— the number of followers of each blogger. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$. Note The test case is explained in the statements. In the second test case, the following ways are valid: conclude contracts with bloggers with numbers $$$1$$$ and $$$2$$$. In this case, the number of followers will be equal to $$$a_1 + a_2 = 2$$$; conclude contracts with bloggers with numbers $$$1$$$ and $$$3$$$. In this case, the number of followers will be equal to $$$a_1 + a_3 = 2$$$; conclude contracts with bloggers with numbers $$$1$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_1 + a_4 = 2$$$; conclude contracts with bloggers with numbers $$$2$$$ and $$$3$$$. In this case, the number of followers will be equal to $$$a_2 + a_3 = 2$$$; conclude contracts with bloggers with numbers $$$2$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_2 + a_4 = 2$$$; conclude contracts with bloggers with numbers $$$3$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_3 + a_4 = 2$$$. In the third test case, the following ways are valid: concludes a contract with a blogger with the number $$$2$$$. In this case, the number of followers will be equal to $$$a_2 = 2$$$. | 1,600 | true | false | false | false | false | false | false | false | true | false | 3,300 |
766E | Mahmoud and Ehab live in a country with _n_ cities numbered from 1 to _n_ and connected by _n_u2009-u20091 undirected roads. It's guaranteed that you can reach any city from any other using these roads. Each city has a number _a__i_ attached to it. We define the distance from city _x_ to city _y_ as the xor of numbers attached to the cities on the path from _x_ to _y_ (including both _x_ and _y_). In other words if values attached to the cities on the path from _x_ to _y_ form an array _p_ of length _l_ then the distance between them is , where is bitwise xor operation. Mahmoud and Ehab want to choose two cities and make a journey from one to another. The index of the start city is always less than or equal to the index of the finish city (they may start and finish in the same city and in this case the distance equals the number attached to that city). They can't determine the two cities so they try every city as a start and every city with greater index as a finish. They want to know the total distance between all pairs of cities. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of cities in Mahmoud and Ehab's country. Then the second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009106) which represent the numbers attached to the cities. Integer _a__i_ is attached to the city _i_. Each of the next _n_u2009u2009-u2009u20091 lines contains two integers _u_ and _v_ (1u2009u2009≤u2009u2009_u_,u2009u2009_v_u2009u2009≤u2009u2009_n_, _u_u2009u2009≠u2009u2009_v_), denoting that there is an undirected road between cities _u_ and _v_. It's guaranteed that you can reach any city from any other using these roads. Output Output one number denoting the total distance between all pairs of cities. Examples Input 5 1 2 3 4 5 1 2 2 3 3 4 3 5 Input 5 10 9 8 7 6 1 2 2 3 3 4 3 5 Note A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: [ the first sample the available paths are: The total distance between all pairs of cities equals 1u2009+u20092u2009+u20093u2009+u20093u2009+u20090u2009+u20091u2009=u200910. | 2,100 | true | false | false | true | true | true | false | false | false | false | 6,719 |
1375F | This is an interactive problem. Anton and Harris are playing a game to decide which of them is the king of problemsetting. There are three piles of stones, initially containing $$$a$$$, $$$b$$$, and $$$c$$$ stones, where $$$a$$$, $$$b$$$, and $$$c$$$ are distinct positive integers. On each turn of the game, the following sequence of events takes place: The first player chooses a positive integer $$$y$$$ and provides it to the second player. The second player adds $$$y$$$ stones to one of the piles, with the condition that he cannot choose the same pile in two consecutive turns. The second player loses if, at any point, two of the piles contain the same number of stones. The first player loses if $$$1000$$$ turns have passed without the second player losing. Feeling confident in his skills, Anton decided to let Harris choose whether he wants to go first or second. Help Harris defeat Anton and become the king of problemsetting! Input The first line of input contains three distinct positive integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 le a, b, c le 10^9$$$) xa0— the initial number of stones in piles $$$1$$$, $$$2$$$, and $$$3$$$ respectively. Interaction The interaction begins by reading the integers $$$a$$$, $$$b$$$ and $$$c$$$. After reading the integers, print a single line containing either "First" or "Second", denoting who you want to play as (as first or second correspondently). On each turn, the first player (either you or the judge) must print a positive integer $$$y$$$ ($$$1 le y le 10^{12}$$$). Then, the second player must print $$$1$$$, $$$2$$$, or $$$3$$$, indicating which pile should have $$$y$$$ stones added to it. From the second turn onwards, the pile that the second player chooses must be different from the pile that they chose on the previous turn. If you are playing as Second and complete $$$1000$$$ turns without losing, or if you are playing as First and the judge has determined that it cannot make a move without losing, the interactor will print $$$0$$$ and will finish interaction. This means that your program is correct for this test case, and you should exit immediately. If you are playing as First and complete $$$1000$$$ turns without winning, or if you are playing as Second and print a move that makes two piles have the same number of stones, or if you output an invalid move as either player, the interactor will print $$$-1$$$ and will finish interaction. You will receive a Wrong Answer verdict. Make sure to exit immediately to avoid getting other verdicts. After printing something do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. In this problem, hacks are disabled. Note In the sample input, the piles initially have $$$5$$$, $$$2$$$, and $$$6$$$ stones. Harris decides to go first and provides the number $$$2$$$ to Anton. Anton adds $$$2$$$ stones to the third pile, which results in $$$5$$$, $$$2$$$, and $$$8$$$. In the next turn, Harris chooses $$$3$$$. Note that Anton cannot add the stones to the third pile since he chose the third pile in the previous turn. Anton realizes that he has no valid moves left and reluctantly recognizes Harris as the king. | 2,600 | true | false | false | false | false | true | false | false | false | false | 3,782 |
838D | There is an airplane which has _n_ rows from front to back. There will be _m_ people boarding this airplane. This airplane has an entrance at the very front and very back of the plane. Each person has some assigned seat. It is possible for multiple people to have the same assigned seat. The people will then board the plane one by one starting with person 1. Each person can independently choose either the front entrance or back entrance to enter the plane. When a person walks into the plane, they walk directly to their assigned seat and will try to sit in it. If it is occupied, they will continue walking in the direction they walked in until they are at empty seat - they will take the earliest empty seat that they can find. If they get to the end of the row without finding a seat, they will be angry. Find the number of ways to assign tickets to the passengers and board the plane without anyone getting angry. Two ways are different if there exists a passenger who chose a different entrance in both ways, or the assigned seat is different. Print this count modulo 109u2009+u20097. Input The first line of input will contain two integers _n_,u2009_m_ (1u2009≤u2009_m_u2009≤u2009_n_u2009≤u20091u2009000u2009000), the number of seats, and the number of passengers, respectively. Output Print a single number, the number of ways, modulo 109u2009+u20097. Note Here, we will denote a passenger by which seat they were assigned, and which side they came from (either "F" or "B" for front or back, respectively). For example, one valid way is 3B, 3B, 3B (i.e. all passengers were assigned seat 3 and came from the back entrance). Another valid way would be 2F, 1B, 3F. One invalid way would be 2B, 2B, 2B, since the third passenger would get to the front without finding a seat. | 2,700 | true | false | false | false | false | false | false | false | false | false | 6,411 |
1980D | GCD (Greatest Common Divisor) of two integers $$$x$$$ and $$$y$$$ is the maximum integer $$$z$$$ by which both $$$x$$$ and $$$y$$$ are divisible. For example, $$$GCD(36, 48) = 12$$$, $$$GCD(5, 10) = 5$$$, and $$$GCD(7,11) = 1$$$. Kristina has an array $$$a$$$ consisting of exactly $$$n$$$ positive integers. She wants to count the GCD of each neighbouring pair of numbers to get a new array $$$b$$$, called GCD-sequence. So, the elements of the GCD-sequence $$$b$$$ will be calculated using the formula $$$b_i = GCD(a_i, a_{i + 1})$$$ for $$$1 le i le n - 1$$$. Determine whether it is possible to remove exactly one number from the array $$$a$$$ so that the GCD sequence $$$b$$$ is non-decreasing (i.e., $$$b_i le b_{i+1}$$$ is always true). For example, let Khristina had an array $$$a$$$ = [$$$20, 6, 12, 3, 48, 36$$$]. If she removes $$$a_4 = 3$$$ from it and counts the GCD-sequence of $$$b$$$, she gets: $$$b_1 = GCD(20, 6) = 2$$$ $$$b_2 = GCD(6, 12) = 6$$$ $$$b_3 = GCD(12, 48) = 12$$$ $$$b_4 = GCD(48, 36) = 12$$$ The resulting GCD sequence $$$b$$$ = [$$$2,6,12,12$$$] is non-decreasing because $$$b_1 le b_2 le b_3 le b_4$$$. Input The first line of input data contains a single number $$$t$$$ ($$$1 le t le 10^4$$$)xa0— he number of test cases in the test. This is followed by the descriptions of the test cases. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$) — the number of elements in the array $$$a$$$. The second line of each test case contains exactly $$$n$$$ integers $$$a_i$$$ ($$$1 le a_i le 10^9$$$) — the elements of array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test case does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single line: "YES" if you can remove exactly one number from the array $$$a$$$ so that the GCD-sequence of $$$b$$$ is non-decreasing; "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as a positive answer). | 1,400 | true | true | true | false | false | false | false | false | false | false | 407 |
623E | Problem - 623E - 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 fft math *3300 No tag edit access → Contest materials . Output Print a single integerxa0— the answer to the problem modulo 109u2009+u20097. Examples Input 1 2 Output 3 Input 2 3 Output 30 Input 3 3 Output 48 | 3,300 | true | false | false | true | false | false | false | false | false | false | 7,336 |
906E | Problem - 906E - 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 *3300 No tag edit access → Contest materials ") ") . These substrings shouldn't intersect. If there are multiple answers print any. If it's impossible to restore string output -1. Example Input abcxxxdef cbaxxxfed Output 2 7 9 1 3 | 3,300 | false | false | false | true | false | false | false | false | false | false | 6,123 |
997C | Problem - 997C - 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 math *2500 No tag edit access → Contest materials xa0— the number of rows and columns in the calendar. Output Print one numberxa0— number of lucky colorings of the calendar modulo $$$998244353$$$ Examples Input 1 Output 3 Input 2 Output 63 Input 3 Output 9933 Note In the first sample any coloring is lucky, since the only column contains cells of only one color. In the second sample, there are a lot of lucky colorings, in particular, the following colorings are lucky: While these colorings are not lucky: | 2,500 | true | false | false | false | false | false | false | false | false | false | 5,716 |
1555A | PizzaForces is Petya's favorite pizzeria. PizzaForces makes and sells pizzas of three sizes: small pizzas consist of $$$6$$$ slices, medium ones consist of $$$8$$$ slices, and large pizzas consist of $$$10$$$ slices each. Baking them takes $$$15$$$, $$$20$$$ and $$$25$$$ minutes, respectively. Petya's birthday is today, and $$$n$$$ of his friends will come, so he decided to make an order from his favorite pizzeria. Petya wants to order so much pizza that each of his friends gets at least one slice of pizza. The cooking time of the order is the total baking time of all the pizzas in the order. Your task is to determine the minimum number of minutes that is needed to make pizzas containing at least $$$n$$$ slices in total. For example: if $$$12$$$ friends come to Petya's birthday, he has to order pizzas containing at least $$$12$$$ slices in total. He can order two small pizzas, containing exactly $$$12$$$ slices, and the time to bake them is $$$30$$$ minutes; if $$$15$$$ friends come to Petya's birthday, he has to order pizzas containing at least $$$15$$$ slices in total. He can order a small pizza and a large pizza, containing $$$16$$$ slices, and the time to bake them is $$$40$$$ minutes; if $$$300$$$ friends come to Petya's birthday, he has to order pizzas containing at least $$$300$$$ slices in total. He can order $$$15$$$ small pizzas, $$$10$$$ medium pizzas and $$$13$$$ large pizzas, in total they contain $$$15 cdot 6 + 10 cdot 8 + 13 cdot 10 = 300$$$ slices, and the total time to bake them is $$$15 cdot 15 + 10 cdot 20 + 13 cdot 25 = 750$$$ minutes; if only one friend comes to Petya's birthday, he can order a small pizza, and the time to bake it is $$$15$$$ minutes. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. Each testcase consists of a single line that contains a single integer $$$n$$$ ($$$1 le n le 10^{16}$$$)xa0— the number of Petya's friends. Output For each testcase, print one integerxa0— the minimum number of minutes that is needed to bake pizzas containing at least $$$n$$$ slices in total. Example Input 6 12 15 300 1 9999999999999999 3 Output 30 40 750 15 25000000000000000 15 | 900 | true | false | false | false | false | false | true | false | false | false | 2,861 |
1691C | You are given a binary string $$$s$$$ of length $$$n$$$. Let's define $$$d_i$$$ as the number whose decimal representation is $$$s_i s_{i+1}$$$ (possibly, with a leading zero). We define $$$f(s)$$$ to be the sum of all the valid $$$d_i$$$. In other words, $$$f(s) = sumlimits_{i=1}^{n-1} d_i$$$. For example, for the string $$$s = 1011$$$: $$$d_1 = 10$$$ (ten); $$$d_2 = 01$$$ (one) $$$d_3 = 11$$$ (eleven); $$$f(s) = 10 + 01 + 11 = 22$$$. In one operation you can swap any two adjacent elements of the string. Find the minimum value of $$$f(s)$$$ that can be achieved if at most $$$k$$$ operations are allowed. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). Description of the test cases follows. First line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 10^5$$$, $$$0 le k le 10^9$$$)xa0— the length of the string and the maximum number of operations allowed. The second line of each test case contains the binary string $$$s$$$ of length $$$n$$$, consisting of only zeros and ones. It is also given that sum of $$$n$$$ over all the test cases doesn't exceed $$$10^5$$$. Output For each test case, print the minimum value of $$$f(s)$$$ you can obtain with at most $$$k$$$ operations. Example Input 3 4 0 1010 7 1 0010100 5 2 00110 Note For the first example, you can't do any operation so the optimal string is $$$s$$$ itself. $$$f(s) = f(1010) = 10 + 01 + 10 = 21$$$. For the second example, one of the optimal strings you can obtain is "0011000". The string has an $$$f$$$ value of $$$22$$$. For the third example, one of the optimal strings you can obtain is "00011". The string has an $$$f$$$ value of $$$12$$$. | 1,400 | true | true | false | false | false | true | true | false | false | false | 2,134 |
939D | Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long. This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya. More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length _n_ consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (_c_1,u2009_c_2) (where _c_1 and _c_2 are some lowercase English letters), which can arbitrary number of times transform a single letter _c_1 to _c_2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the length of the letterings. The second line contains a string with length _n_, consisting of lowercase English lettersxa0— the lettering on Valya's pullover. The third line contains the lettering on Tolya's t-shirt in the same format. Output In the first line output a single integerxa0— the minimum amount of mana _t_ required for rescuing love of Valya and Tolya. In the next _t_ lines output pairs of space-separated lowercase English lettersxa0— spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order. If there are many optimal answers, output any. Examples Output 7 l e e d d c c p p o o r r a Note In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'. | 1,600 | false | true | false | false | false | false | false | false | false | true | 5,941 |
1581B | CQXYM wants to create a connected undirected graph with $$$n$$$ nodes and $$$m$$$ edges, and the diameter of the graph must be strictly less than $$$k-1$$$. Also, CQXYM doesn't want a graph that contains self-loops or multiple edges (i.e. each edge connects two different vertices and between each pair of vertices there is at most one edge). The diameter of a graph is the maximum distance between any two nodes. The distance between two nodes is the minimum number of the edges on the path which endpoints are the two nodes. CQXYM wonders whether it is possible to create such a graph. Input The input consists of multiple test cases. The first line contains an integer $$$t (1 leq t leq 10^5)$$$ — the number of test cases. The description of the test cases follows. Only one line of each test case contains three integers $$$n(1 leq n leq 10^9)$$$, $$$m$$$, $$$k$$$ $$$(0 leq m,k leq 10^9)$$$. Output For each test case, print YES if it is possible to create the graph, or print NO if it is impossible. You can print each letter in any case (upper or lower). Example Input 5 1 0 3 4 5 3 4 6 3 5 4 1 2 1 1 Note In the first test case, the graph's diameter equal to 0. In the second test case, the graph's diameter can only be 2. In the third test case, the graph's diameter can only be 1. | 1,200 | true | true | false | false | false | true | false | false | false | true | 2,726 |
266B | During the break the schoolchildren, boys and girls, formed a queue of _n_ people in the canteen. Initially the children stood in the order they entered the canteen. However, after a while the boys started feeling awkward for standing in front of the girls in the queue and they started letting the girls move forward each second. Let's describe the process more precisely. Let's say that the positions in the queue are sequentially numbered by integers from 1 to _n_, at that the person in the position number 1 is served first. Then, if at time _x_ a boy stands on the _i_-th position and a girl stands on the (_i_u2009+u20091)-th position, then at time _x_u2009+u20091 the _i_-th position will have a girl and the (_i_u2009+u20091)-th position will have a boy. The time is given in seconds. You've got the initial position of the children, at the initial moment of time. Determine the way the queue is going to look after _t_ seconds. Input The first line contains two integers _n_ and _t_ (1u2009≤u2009_n_,u2009_t_u2009≤u200950), which represent the number of children in the queue and the time after which the queue will transform into the arrangement you need to find. The next line contains string _s_, which represents the schoolchildren's initial arrangement. If the _i_-th position in the queue contains a boy, then the _i_-th character of string _s_ equals "B", otherwise the _i_-th character equals "G". Output Print string _a_, which describes the arrangement after _t_ seconds. If the _i_-th position has a boy after the needed time, then the _i_-th character _a_ must equal "B", otherwise it must equal "G". | 800 | false | false | true | false | false | true | false | false | false | false | 8,770 |
2033A | Sakurako and Kosuke decided to play some games with a dot on a coordinate line. The dot is currently located in position $$$x=0$$$. They will be taking turns, and Sakurako will be the one to start. On the $$$i$$$-th move, the current player will move the dot in some direction by $$$2cdot i-1$$$ units. Sakurako will always be moving the dot in the negative direction, whereas Kosuke will always move it in the positive direction. In other words, the following will happen: 1. Sakurako will change the position of the dot by $$$-1$$$, $$$x = -1$$$ now 2. Kosuke will change the position of the dot by $$$3$$$, $$$x = 2$$$ now 3. Sakurako will change the position of the dot by $$$-5$$$, $$$x = -3$$$ now 4. $$$cdots$$$ They will keep on playing while the absolute value of the coordinate of the dot does not exceed $$$n$$$. More formally, the game continues while $$$-nle xle n$$$. It can be proven that the game will always end. Your task is to determine who will be the one who makes the last turn. Input The first line contains one integer $$$t$$$ ($$$1le tle 100$$$)xa0— the number of games that Sakurako and Kosuke played. Each game is described by one number $$$n$$$ ($$$1 le nle 100$$$)xa0— the number that defines the condition when the game ends. Output For each of the $$$t$$$ games, output a line with the result of that game. If Sakurako makes the last turn, output "Sakurako" (without quotes); else output "Kosuke". Example Output Kosuke Sakurako Kosuke Sakurako | 800 | true | false | true | false | false | true | false | false | false | false | 68 |
1684E | You are given an array $$$a$$$ of $$$n$$$ non-negative integers. In one operation you can change any number in the array to any other non-negative integer. Let's define the cost of the array as $$$operatorname{DIFF}(a) - operatorname{MEX}(a)$$$, where $$$operatorname{MEX}$$$ of a set of non-negative integers is the smallest non-negative integer not present in the set, and $$$operatorname{DIFF}$$$ is the number of different numbers in the array. For example, $$$operatorname{MEX}({1, 2, 3}) = 0$$$, $$$operatorname{MEX}({0, 1, 2, 4, 5}) = 3$$$. You should find the minimal cost of the array $$$a$$$ if you are allowed to make at most $$$k$$$ operations. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— 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 n le 10^5$$$, $$$0 le k le 10^5$$$)xa0— the length of the array $$$a$$$ and the number of operations that you are allowed to make. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$)xa0— the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Note In the first test case no operations are needed to minimize the value of $$$operatorname{DIFF} - operatorname{MEX}$$$. In the second test case it is possible to replace $$$5$$$ by $$$1$$$. After that the array $$$a$$$ is $$$[0,, 2,, 4,, 1]$$$, $$$operatorname{DIFF} = 4$$$, $$$operatorname{MEX} = operatorname{MEX}({0, 1, 2, 4}) = 3$$$, so the answer is $$$1$$$. In the third test case one possible array $$$a$$$ is $$$[4,, 13,, 0,, 0,, 13,, 1,, 2]$$$, $$$operatorname{DIFF} = 5$$$, $$$operatorname{MEX} = 3$$$. In the fourth test case one possible array $$$a$$$ is $$$[1,, 2,, 3,, 0,, 0,, 0]$$$. | 2,100 | false | true | false | false | true | true | true | true | false | false | 2,169 |
992E | Nastya likes reading and even spends whole days in a library sometimes. Today she found a chronicle of Byteland in the library, and it stated that there lived shamans long time ago. It is known that at every moment there was exactly one shaman in Byteland, and there were _n_ shamans in total enumerated with integers from 1 to _n_ in the order they lived. Also, each shaman had a magic power which can now be expressed as an integer. The chronicle includes a list of powers of the _n_ shamans. Also, some shamans can be king-shamans, if they gathered all the power of their predecessors, i.e. their power is exactly the sum of powers of all previous shamans. Nastya is interested in whether there was at least one king-shaman in Byteland. Unfortunately many of the powers are unreadable in the list, so Nastya is doing the following: Initially she supposes some power for each shaman. After that she changes the power of some shaman _q_ times (the shamans can differ) and after that wants to check if there is at least one king-shaman in the list. If yes, she wants to know the index of any king-shaman. Unfortunately the list is too large and Nastya wants you to help her. Input The first line contains two integers _n_ and _q_ (1u2009≤u2009_n_,u2009_q_u2009≤u20092·105). The second line contains _n_ integers _a_1,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009109), where _a__i_ is the magic power of the _i_-th shaman. After that _q_ lines follow, the _i_-th of them contains two integers _p__i_ and _x__i_ (1u2009≤u2009_p__i_u2009≤u2009_n_, 0u2009≤u2009_x__i_u2009≤u2009109) that mean that the new power of the _p__i_-th shaman is _x__i_. Output Print _q_ lines, the _i_-th of them should contain u2009-u20091, if after the _i_-th change there are no shaman-kings, and otherwise a single integer _j_, where _j_ is an index of some king-shaman after the _i_-th change. If there are multiple king-shamans after each change, print the index of any of them. Examples Input 3 4 2 2 3 1 1 1 2 2 4 3 6 Input 10 7 0 3 1 4 6 2 7 8 10 1 2 5 1 3 9 36 4 10 4 9 1 2 1 0 Note In the first example powers of shamans after the first change are equal to (2,u20093). The answer equals u2009-u20091, because the sum of powers of shamans before the first shaman is equal to 0, and before the second is equal to 2. In the second example after the first change the powers are equal to (1,u20092,u20093). The answer is equal to 3, because the power of the third shaman is equal to 3, and the sum of powers of the first and the second shaman is also 1u2009+u20092u2009=u20093. After the second change the powers become equal to (2,u20092,u20093), where the answer equals 2. After the third change the powers become equal to (2,u20094,u20093), where the answer equals u2009-u20091. After the fourth change the powers become equal to (2,u20094,u20096), where the answer equals 3. | 2,500 | false | false | false | false | true | false | false | true | false | false | 5,735 |
494A | Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string _s_ written on the door consisting of characters '(', ')' and '#'. Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found out that the goal is to replace each '#' with one or more ')' characters so that the final string becomes beautiful. Below there was also written that a string is called beautiful if for each _i_ (1u2009≤u2009_i_u2009≤u2009_s_) there are no more ')' characters than '(' characters among the first _i_ characters of _s_ and also the total number of '(' characters is equal to the total number of ')' characters. Help Malek open the door by telling him for each '#' character how many ')' characters he must replace it with. Input The first line of the input contains a string _s_ (1u2009≤u2009_s_u2009≤u2009105). Each character of this string is one of the characters '(', ')' or '#'. It is guaranteed that _s_ contains at least one '#' character. Output If there is no way of replacing '#' characters which leads to a beautiful string print u2009-u20091. Otherwise for each character '#' print a separate line containing a positive integer, the number of ')' characters this character must be replaced with. If there are several possible answers, you may output any of them. Note _s_ denotes the length of the string _s_. | 1,500 | false | true | false | false | false | false | false | false | false | false | 7,858 |
83C | You already know that Valery's favorite sport is biathlon. Due to your help, he learned to shoot without missing, and his skills are unmatched at the shooting range. But now a smaller task is to be performed, he should learn to complete the path fastest. The track's map is represented by a rectangle _n_u2009×u2009_m_ in size divided into squares. Each square is marked with a lowercase Latin letter (which means the type of the plot), with the exception of the starting square (it is marked with a capital Latin letters _S_) and the terminating square (it is marked with a capital Latin letter _T_). The time of movement from one square to another is equal to 1 minute. The time of movement within the cell can be neglected. We can move from the cell only to side-adjacent ones, but it is forbidden to go beyond the map edges. Also the following restriction is imposed on the path: it is not allowed to visit more than _k_ different types of squares (squares of one type can be visited an infinite number of times). Squares marked with _S_ and _T_ have no type, so they are not counted. But _S_ must be visited exactly once — at the very beginning, and _T_ must be visited exactly once — at the very end. Your task is to find the path from the square _S_ to the square _T_ that takes minimum time. Among all shortest paths you should choose the lexicographically minimal one. When comparing paths you should lexicographically represent them as a sequence of characters, that is, of plot types. Input The first input line contains three integers _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_m_u2009≤u200950,u2009_n_·_m_u2009≥u20092,u20091u2009≤u2009_k_u2009≤u20094). Then _n_ lines contain the map. Each line has the length of exactly _m_ characters and consists of lowercase Latin letters and characters _S_ and _T_. It is guaranteed that the map contains exactly one character _S_ and exactly one character _T_. Pretest 12 is one of the maximal tests for this problem. Output If there is a path that satisfies the condition, print it as a sequence of letters — the plot types. Otherwise, print "-1" (without quotes). You shouldn't print the character _S_ in the beginning and _T_ in the end. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted. Examples Input 5 3 2 Sba ccc aac ccc abT | 2,400 | false | true | false | false | false | false | false | false | false | true | 9,551 |
115A | A company has _n_ employees numbered from 1 to _n_. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee _A_ is said to be the superior of another employee _B_ if at least one of the following is true: Employee _A_ is the immediate manager of employee _B_ Employee _B_ has an immediate manager employee _C_ such that employee _A_ is the superior of employee _C_. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager. Today the company is going to arrange a party. This involves dividing all _n_ employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees _A_ and _B_ such that _A_ is the superior of _B_. What is the minimum number of groups that must be formed? Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20092000) — the number of employees. The next _n_ lines contain the integers _p__i_ (1u2009≤u2009_p__i_u2009≤u2009_n_ or _p__i_u2009=u2009-1). Every _p__i_ denotes the immediate manager for the _i_-th employee. If _p__i_ is -1, that means that the _i_-th employee does not have an immediate manager. It is guaranteed, that no employee will be the immediate manager of him/herself (_p__i_u2009≠u2009_i_). Also, there will be no managerial cycles. Output Print a single integer denoting the minimum number of groups that will be formed in the party. Note For the first example, three groups are sufficient, for example: Employee 1 Employees 2 and 4 Employees 3 and 5 | 900 | false | false | false | false | false | false | false | false | false | true | 9,428 |
732A | Polycarp urgently needs a shovel! He comes to the shop and chooses an appropriate one. The shovel that Policarp chooses is sold for _k_ burles. Assume that there is an unlimited number of such shovels in the shop. In his pocket Polycarp has an unlimited number of "10-burle coins" and exactly one coin of _r_ burles (1u2009≤u2009_r_u2009≤u20099). What is the minimum number of shovels Polycarp has to buy so that he can pay for the purchase without any change? It is obvious that he can pay for 10 shovels without any change (by paying the requied amount of 10-burle coins and not using the coin of _r_ burles). But perhaps he can buy fewer shovels and pay without any change. Note that Polycarp should buy at least one shovel. Input The single line of input contains two integers _k_ and _r_ (1u2009≤u2009_k_u2009≤u20091000, 1u2009≤u2009_r_u2009≤u20099)xa0— the price of one shovel and the denomination of the coin in Polycarp's pocket that is different from "10-burle coins". Remember that he has an unlimited number of coins in the denomination of 10, that is, Polycarp has enough money to buy any number of shovels. Output Print the required minimum number of shovels Polycarp has to buy so that he can pay for them without any change. Note In the first example Polycarp can buy 9 shovels and pay 9·117u2009=u20091053 burles. Indeed, he can pay this sum by using 10-burle coins and one 3-burle coin. He can't buy fewer shovels without any change. In the second example it is enough for Polycarp to buy one shovel. In the third example Polycarp should buy two shovels and pay 2·15u2009=u200930 burles. It is obvious that he can pay this sum without any change. | 800 | true | false | true | false | false | true | true | false | false | false | 6,870 |
1091G | Integer factorisation is hard. The RSA Factoring Challenge offered $$$$100,000$$$ for factoring RSA-$$$1024$$$, a $$$1024$$$-bit long product of two prime numbers. To this date, nobody was able to claim the prize. We want you to factorise a $$$1024$$$-bit number. Since your programming language of choice might not offer facilities for handling large integers, we will provide you with a very simple calculator. To use this calculator, you can print queries on the standard output and retrieve the results from the standard input. The operations are as follows: + x y where $$$x$$$ and $$$y$$$ are integers between $$$0$$$ and $$$n-1$$$. Returns $$$(x+y) bmod n$$$. - x y where $$$x$$$ and $$$y$$$ are integers between $$$0$$$ and $$$n-1$$$. Returns $$$(x-y) bmod n$$$. x y where $$$x$$$ and $$$y$$$ are integers between $$$0$$$ and $$$n-1$$$. Returns $$$(x cdot y) bmod n$$$. / x y where $$$x$$$ and $$$y$$$ are integers between $$$0$$$ and $$$n-1$$$ and $$$y$$$ is coprime with $$$n$$$. Returns $$$(x cdot y^{-1}) bmod n$$$ where $$$y^{-1}$$$ is multiplicative inverse of $$$y$$$ modulo $$$n$$$. If $$$y$$$ is not coprime with $$$n$$$, then $$$-1$$$ is returned instead. sqrt x where $$$x$$$ is integer between $$$0$$$ and $$$n-1$$$ coprime with $$$n$$$. Returns $$$y$$$ such that $$$y^2 bmod n = x$$$. If there are multiple such integers, only one of them is returned. If there are none, $$$-1$$$ is returned instead. ^ x y where $$$x$$$ and $$$y$$$ are integers between $$$0$$$ and $$$n-1$$$. Returns $$${x^y bmod n}$$$. Find the factorisation of $$$n$$$ that is a product of between $$$2$$$ and $$$10$$$ distinct prime numbers, all of form $$$4x + 3$$$ for some integer $$$x$$$. Because of technical issues, we restrict number of requests to $$$100$$$. Input The only line contains a single integer $$$n$$$xa0($$$21 leq n leq 2^{1024}$$$). It is guaranteed that $$$n$$$ is a product of between $$$2$$$ and $$$10$$$ distinct prime numbers, all of form $$$4x + 3$$$ for some integer $$$x$$$. Output You can print as many queries as you wish, adhering to the time limit (see the Interaction section for more details). When you think you know the answer, output a single line of form ! k p_1 p_2 ... p_k, where $$$k$$$ is the number of prime factors of $$$n$$$, and $$$p_i$$$ are the distinct prime factors. You may print the factors in any order. Hacks input For hacks, use the following format:. The first should contain $$$k$$$ ($$$2 leq k leq 10$$$)xa0— the number of prime factors of $$$n$$$. The second should contain $$$k$$$ space separated integers $$$p_1, p_2, dots, p_k$$$ ($$$21 leq n leq 2^{1024}$$$)xa0— the prime factors of $$$n$$$. All prime factors have to be of form $$$4x + 3$$$ for some integer $$$x$$$. They all have to be distinct. Interaction After printing a query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. The number of queries is not limited. However, your program must (as always) fit in the time limit. The run time of the interactor is also counted towards the time limit. The maximum runtime of each query is given below. + x yxa0— up to $$$1$$$ ms. - x yxa0— up to $$$1$$$ ms. x yxa0— up to $$$1$$$ ms. / x yxa0— up to $$$350$$$ ms. sqrt xxa0— up to $$$80$$$ ms. ^ x yxa0— up to $$$350$$$ ms. Note that the sample input contains extra empty lines so that it easier to read. The real input will not contain any empty lines and you do not need to output extra empty lines. Example Output + 12 16- 6 10 8 15 / 5 4 sqrt 16 sqrt 5 ^ 6 12 ! 2 3 7 Note We start by reading the first line containing the integer $$$n = 21$$$. Then, we ask for: 1. $$$(12 + 16) bmod 21 = 28 bmod 21 = 7$$$. 2. $$$(6 - 10) bmod 21 = -4 bmod 21 = 17$$$. 3. $$$(8 cdot 15) bmod 21 = 120 bmod 21 = 15$$$. 4. $$$(5 cdot 4^{-1}) bmod 21 = (5 cdot 16) bmod 21 = 80 bmod 21 = 17$$$. 5. Square root of $$$16$$$. The answer is $$$11$$$, as $$$(11 cdot 11) bmod 21 = 121 bmod 21 = 16$$$. Note that the answer may as well be $$$10$$$. 6. Square root of $$$5$$$. There is no $$$x$$$ such that $$$x^2 bmod 21 = 5$$$, so the output is $$$-1$$$. 7. $$$(6^{12}) bmod 21 = 2176782336 bmod 21 = 15$$$. We conclude that our calculator is working, stop fooling around and realise that $$$21 = 3 cdot 7$$$. | 3,200 | true | false | false | false | false | false | false | false | false | false | 5,253 |
1088B | Problem - 1088B - 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 sortings *1000 No tag edit access → Contest materials $$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, ldots, a_n$$$ $$$(1 le a_i le 10^9)$$$, the elements of the array. Output Print the minimum non-zero element before each operation in a new line. Examples Input 3 5 1 2 3 Output 1 1 1 0 0 Input 4 2 10 3 5 3 Output 3 2 Note In the first sample: In the first step: the array is $$$ | 1,000 | false | false | true | false | false | false | false | false | true | false | 5,290 |
1893C | Let's define the anti-beauty of a multiset $$${b_1, b_2, ldots, b_{len}}$$$ as the number of occurrences of the number $$$len$$$ in the multiset. You are given $$$m$$$ multisets, where the $$$i$$$-th multiset contains $$$n_i$$$ distinct elements, specifically: $$$c_{i, 1}$$$ copies of the number $$$a_{i,1}$$$, $$$c_{i, 2}$$$ copies of the number $$$a_{i,2}, ldots, c_{i, n_i}$$$ copies of the number $$$a_{i, n_i}$$$. It is guaranteed that $$$a_{i, 1} < a_{i, 2} < ldots < a_{i, n_i}$$$. You are also given numbers $$$l_1, l_2, ldots, l_m$$$ and $$$r_1, r_2, ldots, r_m$$$ such that $$$1 le l_i le r_i le c_{i, 1} + ldots + c_{i, n_i}$$$. Let's create a multiset $$$X$$$, initially empty. Then, for each $$$i$$$ from $$$1$$$ to $$$m$$$, you must perform the following action exactly once: 1. Choose some $$$v_i$$$ such that $$$l_i le v_i le r_i$$$ 2. Choose any $$$v_i$$$ numbers from the $$$i$$$-th multiset and add them to the multiset $$$X$$$. You need to choose $$$v_1, ldots, v_m$$$ and the added numbers in such a way that the resulting multiset $$$X$$$ has the minimum possible anti-beauty. Input Each test consists of multiple test cases. The first line 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 a single integer $$$m$$$ ($$$1 le m le 10^5$$$)xa0— the number of given multisets. Then, for each $$$i$$$ from $$$1$$$ to $$$m$$$, a data block consisting of three lines is entered. The first line of each block contains three integers $$$n_i, l_i, r_i$$$ ($$$1 le n_i le 10^5, 1 le l_i le r_i le c_{i, 1} + ldots + c_{i, n_i} le 10^{17}$$$)xa0— the number of distinct numbers in the $$$i$$$-th multiset and the limits on the number of elements to be added to $$$X$$$ from the $$$i$$$-th multiset. The second line of the block contains $$$n_i$$$ integers $$$a_{i, 1}, ldots, a_{i, n_i}$$$ ($$$1 le a_{i, 1} < ldots < a_{i, n_i} le 10^{17}$$$)xa0— the distinct elements of the $$$i$$$-th multiset. The third line of the block contains $$$n_i$$$ integers $$$c_{i, 1}, ldots, c_{i, n_i}$$$ ($$$1 le c_{i, j} le 10^{12}$$$)xa0— the number of copies of the elements in the $$$i$$$-th multiset. It is guaranteed that the sum of the values of $$$m$$$ for all test cases does not exceed $$$10^5$$$, and also the sum of $$$n_i$$$ for all blocks of all test cases does not exceed $$$10^5$$$. Output For each test case, output the minimum possible anti-beauty of the multiset $$$X$$$ that you can achieve. Example Input 7 3 3 5 6 10 11 12 3 3 1 1 1 3 12 4 2 4 4 12 13 1 5 1 7 1000 1006 1000 1001 1002 1003 1004 1005 1006 147 145 143 143 143 143 142 1 2 48 50 48 50 25 25 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 2 1 1 2 4 8 10 11 12 13 14 3 3 3 3 2 3 4 11 12 2 2 Note In the first test case, the multisets have the following form: 1. $$${10, 10, 10, 11, 11, 11, 12}$$$. From this multiset, you need to select between $$$5$$$ and $$$6$$$ numbers. 2. $$${12, 12, 12, 12}$$$. From this multiset, you need to select between $$$1$$$ and $$$3$$$ numbers. 3. $$${12, 13, 13, 13, 13, 13}$$$. From this multiset, you need to select $$$4$$$ numbers. You can select the elements $$${10, 11, 11, 11, 12}$$$ from the first multiset, $$${12}$$$ from the second multiset, and $$${13, 13, 13, 13}$$$ from the third multiset. Thus, $$$X = {10, 11, 11, 11, 12, 12, 13, 13, 13, 13}$$$. The size of $$$X$$$ is $$$10$$$, the number $$$10$$$ appears exactly $$$1$$$ time in $$$X$$$, so the anti-beauty of $$$X$$$ is $$$1$$$. It can be shown that it is not possible to achieve an anti-beauty less than $$$1$$$. | 2,000 | false | true | true | false | false | false | true | false | false | false | 949 |
125B | Let's define a string <x> as an opening tag, where _x_ is any small letter of the Latin alphabet. Each opening tag matches a closing tag of the type </x>, where _x_ is the same letter. Tegs can be nested into each other: in this case one opening and closing tag pair is located inside another pair. Let's define the notion of a XML-text: an empty string is a XML-text if _s_ is a XML-text, then _s_'=<a>+_s_+</a> also is a XML-text, where _a_ is any small Latin letter if _s_1, _s_2 are XML-texts, then _s_1+_s_2 also is a XML-text You are given a XML-text (it is guaranteed that the text is valid), your task is to print in the following form: each tag (opening and closing) is located on a single line print before the tag 2u2009*u2009_h_ spaces, where _h_ is the level of the tag's nestedness. Input The input data consists on the only non-empty string — the XML-text, its length does not exceed 1000 characters. It is guaranteed that the text is valid. The text contains no spaces. Output Print the given XML-text according to the above-given rules. Examples Input <a><b><c></c></b></a> Output <a> <b> <c> </c> </b> </a> Input <a><b></b><d><c></c></d></a> Output <a> <b> </b> <d> <c> </c> </d> </a> | 1,000 | false | false | true | false | false | false | false | false | false | false | 9,381 |
290C | Problem - 290C - Codeforces =============== xa0 . Output Output a single real number. The answer is considered to be correct if its absolute or relative error does not exceed 10u2009-u20094. Examples Input 3 0 1 1 Output 0.666667 | 1,700 | false | false | true | false | false | false | false | false | false | false | 8,675 |
593D | Bogdan has a birthday today and mom gave him a tree consisting of _n_ vertecies. For every edge of the tree _i_, some number _x__i_ was written on it. In case you forget, a tree is a connected non-directed graph without cycles. After the present was granted, _m_ guests consecutively come to Bogdan's party. When the _i_-th guest comes, he performs exactly one of the two possible operations: 1. Chooses some number _y__i_, and two vertecies _a__i_ and _b__i_. After that, he moves along the edges of the tree from vertex _a__i_ to vertex _b__i_ using the shortest path (of course, such a path is unique in the tree). Every time he moves along some edge _j_, he replaces his current number _y__i_ by , that is, by the result of integer division _y__i_ div _x__j_. 2. Chooses some edge _p__i_ and replaces the value written in it _x__p__i_ by some positive integer _c__i_u2009<u2009_x__p__i_. As Bogdan cares about his guests, he decided to ease the process. Write a program that performs all the operations requested by guests and outputs the resulting value _y__i_ for each _i_ of the first type. Input The first line of the input contains integers, _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009200u2009000, 1u2009≤u2009_m_u2009≤u2009200u2009000)xa0— the number of vertecies in the tree granted to Bogdan by his mom and the number of guests that came to the party respectively. Next _n_u2009-u20091 lines contain the description of the edges. The _i_-th of these lines contains three integers _u__i_, _v__i_ and _x__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, _u__i_u2009≠u2009_v__i_, 1u2009≤u2009_x__i_u2009≤u20091018), denoting an edge that connects vertecies _u__i_ and _v__i_, with the number _x__i_ initially written on it. The following _m_ lines describe operations, requested by Bogdan's guests. Each description contains three or four integers and has one of the two possible forms: 1 _a__i_ _b__i_ _y__i_ corresponds to a guest, who chooses the operation of the first type. 2 _p__i_ _c__i_ corresponds to a guests, who chooses the operation of the second type. It is guaranteed that all the queries are correct, namely 1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, 1u2009≤u2009_p__i_u2009≤u2009_n_u2009-u20091, 1u2009≤u2009_y__i_u2009≤u20091018 and 1u2009≤u2009_c__i_u2009<u2009_x__p__i_, where _x__p__i_ represents a number written on edge _p__i_ at this particular moment of time that is not necessarily equal to the initial value _x__p__i_, as some decreases may have already been applied to it. The edges are numbered from 1 to _n_u2009-u20091 in the order they appear in the input. | 2,400 | true | false | false | false | true | false | false | false | false | true | 7,474 |
295C | One day Greg and his friends were walking in the forest. Overall there were _n_ people walking, including Greg. Soon he found himself in front of a river. The guys immediately decided to get across the river. Luckily, there was a boat by the river bank, just where the guys were standing. We know that the boat can hold people with the total weight of at most _k_ kilograms. Greg immediately took a piece of paper and listed there the weights of all people in his group (including himself). It turned out that each person weights either 50 or 100 kilograms. Now Greg wants to know what minimum number of times the boat needs to cross the river to transport the whole group to the other bank. The boat needs at least one person to navigate it from one bank to the other. As the boat crosses the river, it can have any non-zero number of passengers as long as their total weight doesn't exceed _k_. Also Greg is wondering, how many ways there are to transport everybody to the other side in the minimum number of boat rides. Two ways are considered distinct if during some ride they have distinct sets of people on the boat. Help Greg with this problem. Input The first line contains two integers _n_, _k_ (1u2009≤u2009_n_u2009≤u200950,u20091u2009≤u2009_k_u2009≤u20095000) — the number of people, including Greg, and the boat's weight limit. The next line contains _n_ integers — the people's weights. A person's weight is either 50 kilos or 100 kilos. You can consider Greg and his friends indexed in some way. Output In the first line print an integer — the minimum number of rides. If transporting everyone to the other bank is impossible, print an integer -1. In the second line print the remainder after dividing the number of ways to transport the people in the minimum number of rides by number 1000000007 (109u2009+u20097). If transporting everyone to the other bank is impossible, print integer 0. Note In the first test Greg walks alone and consequently, he needs only one ride across the river. In the second test you should follow the plan: 1. transport two 50 kg. people; 2. transport one 50 kg. person back; 3. transport one 100 kg. person; 4. transport one 50 kg. person back; 5. transport two 50 kg. people. That totals to 5 rides. Depending on which person to choose at step 2, we can get two distinct ways. | 2,100 | false | false | false | true | false | false | false | false | false | true | 8,649 |
708C | Tree is a connected acyclic graph. Suppose you are given a tree consisting of _n_ vertices. The vertex of this tree is called centroid if the size of each connected component that appears if this vertex is removed from the tree doesn't exceed . You are given a tree of size _n_ and can perform no more than one edge replacement. Edge replacement is the operation of removing one edge from the tree (without deleting incident vertices) and inserting one new edge (without adding new vertices) in such a way that the graph remains a tree. For each vertex you have to determine if it's possible to make it centroid by performing no more than one edge replacement. Input The first line of the input contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009400u2009000)xa0— the number of vertices in the tree. Each of the next _n_u2009-u20091 lines contains a pair of vertex indices _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_)xa0— endpoints of the corresponding edge. Output Print _n_ integers. The _i_-th of them should be equal to 1 if the _i_-th vertex can be made centroid by replacing no more than one edge, and should be equal to 0 otherwise. Note In the first sample each vertex can be made a centroid. For example, in order to turn vertex 1 to centroid one have to replace the edge (2,u20093) with the edge (1,u20093). | 2,300 | false | true | false | true | true | false | false | false | false | true | 6,987 |
301E | Problem - 301E - 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 *2800 No tag edit access → Contest materials . 2. If the inequalities 1u2009≤u2009_r_u2009≤u2009_n_ and 1u2009≤u2009_b__i_u2009≤u2009_m_ hold. 3. If we can rearrange its elements and get at least one and at most _k_ distinct good arrays. Yaroslav has three integers _n_,u2009_m_,u2009_k_. He needs to count the number of distinct great arrays. Help Yaroslav! As the answer may be rather large, print the remainder after dividing it by 1000000007 (109u2009+u20097). Two arrays are considered distinct if there is a position in which they have distinct numbers. Input The single line contains three integers _n_, _m_, _k_ (1u2009≤u2009_n_,u2009_m_,u2009_k_u2009≤u2009100). Output In a single line print the remainder after dividing the answer to the problem by number 1000000007 (109u2009+u20097). Examples Input 1 1 1 Output 0 Input 3 3 3 Output 2 | 2,800 | false | false | false | true | false | false | false | false | false | false | 8,626 |
1864G | Aquamoon has a Rubik's Square which can be seen as an $$$n imes n$$$ matrix, the elements of the matrix constitute a permutation of numbers $$$1, ldots, n^2$$$. Aquamoon can perform two operations on the matrix: Row shift, i.e. shift an entire row of the matrix several positions (at least $$$1$$$ and at most $$$n-1$$$) to the right. The elements that come out of the right border of the matrix are moved to the beginning of the row. For example, shifting a row $$$begin{pmatrix} a & b & c end{pmatrix}$$$ by $$$2$$$ positions would result in $$$begin{pmatrix} b & c & a end{pmatrix}$$$; Column shift, i.e. shift an entire column of the matrix several positions (at least $$$1$$$ and at most $$$n-1$$$) downwards. The elements that come out of the lower border of the matrix are moved to the beginning of the column. For example, shifting a column $$$begin{pmatrix} a b c end{pmatrix}$$$ by $$$2$$$ positions would result in $$$begin{pmatrix} bca end{pmatrix}$$$. The rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, the columns are numbered from $$$1$$$ to $$$n$$$ from left to right. The cell at the intersection of the $$$x$$$-th row and the $$$y$$$-th column is denoted as $$$(x, y)$$$. Aquamoon can perform several (possibly, zero) operations, but she has to obey the following restrictions: each row and each column can be shifted at most once; each integer of the matrix can be moved at most twice; the offsets of any two integers moved twice cannot be the same. Formally, if integers $$$a$$$ and $$$b$$$ have been moved twice, assuming $$$a$$$ has changed its position from $$$(x_1,y_1)$$$ to $$$(x_2,y_2)$$$, and $$$b$$$ has changed its position from $$$(x_3,y_3)$$$ to $$$(x_4,y_4)$$$, then $$$x_2-x_1 otequiv x_4-x_3 pmod{n}$$$ or $$$y_2-y_1 otequiv y_4-y_3 pmod{n}$$$. Aquamoon wonders in how many ways she can transform the Rubik's Square from the given initial state to a given target state. Two ways are considered different if the sequences of applied operations are different. Since the answer can be very large, print the result modulo $$$998,244,353$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 2cdot 10^4$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3le n le 500$$$). The $$$i$$$-th of the following $$$n$$$ lines contains $$$n$$$ integers $$$a_{i1}, ldots, a_{in}$$$, representing the $$$i$$$-th row of the initial matrix ($$$1 le a_{ij} le n^2$$$). The $$$i$$$-th of the following $$$n$$$ lines contains $$$n$$$ integers $$$b_{i1}, ldots, b_{in}$$$, representing the $$$i$$$-th row of the target matrix ($$$1 le b_{ij} le n^2$$$). It is guaranteed that both the elements of the initial matrix and the elements of the target matrix constitute a permutation of numbers $$$1, ldots, n^2$$$. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$250,000$$$. Output For each test case, if it is possible to convert the initial state to the target state respecting all the restrictions, output one integerxa0— the number of ways to do so, modulo $$$998,244,353$$$. If there is no solution, print a single integer $$$0$$$. Example Input 4 3 1 2 3 4 5 6 7 8 9 7 2 3 1 4 5 6 8 9 3 1 2 3 4 5 6 7 8 9 3 2 1 6 5 4 9 7 8 3 1 2 3 4 5 6 7 8 9 7 8 1 2 3 4 5 6 9 3 1 2 3 4 5 6 7 8 9 3 8 4 5 1 9 7 6 2 Note In the first test case, the only way to transform the initial matrix to the target one is to shift the second row by $$$1$$$ position to the right, and then shift the first column by $$$1$$$ position downwards. In the second test case, it can be shown that there is no correct way to transform the matrix, thus, the answer is $$$0$$$. | 3,100 | false | false | true | false | false | true | false | false | false | false | 1,091 |
367B | Problem - 367B - 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 *1900 No tag edit access → Contest materials ·_p_u2009≤u2009_n_;xa0_q_u2009≥u20091), such that sequence _b_ can be obtained from sequence _a__q_,u2009_a__q_u2009+u2009_p_,u2009_a__q_u2009+u20092_p_,u2009...,u2009_a__q_u2009+u2009(_m_u2009-u20091)_p_ by rearranging elements. Sereja needs to rush to the gym, so he asked to find all the described positions of _q_. Input The first line contains three integers _n_, _m_ and _p_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092·105,u20091u2009≤u2009_p_u2009≤u20092·105). The next line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). The next line contains _m_ integers _b_1, _b_2, ..., _b__m_ (1u2009≤u2009_b__i_u2009≤u2009109). Output In the first line print the number of valid _q_s. In the second line, print the valid values in the increasing order. Examples Input 5 3 1 1 2 3 2 1 1 2 3 Output 2 1 3 Input 6 3 2 1 3 2 2 3 1 1 2 3 Output 2 1 2 | 1,900 | false | false | false | false | true | false | false | true | false | false | 8,364 |
1867F | Given a tree with $$$n$$$ vertices rooted at vertex $$$1$$$, denote it as $$$G$$$. Also denote $$$P(G)$$$ as the multiset of subtrees of all vertices in tree $$$G$$$. You need to find a tree $$$G'$$$ of size $$$n$$$ rooted at vertex $$$1$$$ such that the number of subtrees in $$$P(G')$$$ that are isomorphic to any subtree in $$$P(G)$$$ is minimized. A subtree of vertex $$$v$$$ is a graph that contains all vertices for which vertex $$$v$$$ lies on the path from the root of the tree to itself, as well as all edges between these vertices. Two rooted trees are considered isomorphic if it is possible to relabel the vertices of one of them so that it becomes equal to the other, with the root of the first tree receiving the number of the root of the second tree. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 10^6$$$) - the number of vertices in tree $$$G$$$. Each of the next $$$n-1$$$ lines contains two integers $$$a$$$ and $$$b$$$ $$$(1 leq a,b leq n)$$$, indicating that there is an edge between vertices $$$a$$$ and $$$b$$$ in the tree. Output Output $$$n-1$$$ lines, each line containing two numbers $$$a$$$, $$$b$$$ $$$(1 leq a,b leq n)$$$ - the edges of tree $$$G'$$$. If there are multiple optimal answers, output any. | 2,700 | false | true | false | false | false | true | true | false | false | false | 1,069 |