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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1401D | You are given a tree that consists of $$$n$$$ nodes. You should label each of its $$$n-1$$$ edges with an integer in such way that satisfies the following conditions: each integer must be greater than $$$0$$$; the product of all $$$n-1$$$ numbers should be equal to $$$k$$$; the number of $$$1$$$-s among all $$$n-1$$$ integers must be minimum possible. Let's define $$$f(u,v)$$$ as the sum of the numbers on the simple path from node $$$u$$$ to node $$$v$$$. Also, let $$$sumlimits_{i=1}^{n-1} sumlimits_{j=i+1}^n f(i,j)$$$ be a distribution index of the tree. Find the maximum possible distribution index you can get. Since answer can be too large, print it modulo $$$10^9 + 7$$$. In this problem, since the number $$$k$$$ can be large, the result of the prime factorization of $$$k$$$ is given instead. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0— the number of nodes in the tree. Each of the next $$$n-1$$$ lines describes an edge: the $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$; $$$u_i e v_i$$$)xa0— indices of vertices connected by the $$$i$$$-th edge. Next line contains a single integer $$$m$$$ ($$$1 le m le 6 cdot 10^4$$$)xa0— the number of prime factors of $$$k$$$. Next line contains $$$m$$$ prime numbers $$$p_1, p_2, ldots, p_m$$$ ($$$2 le p_i < 6 cdot 10^4$$$) such that $$$k = p_1 cdot p_2 cdot ldots cdot p_m$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$, the sum of $$$m$$$ over all test cases doesn't exceed $$$6 cdot 10^4$$$, and the given edges for each test cases form a tree. Output Print the maximum distribution index you can get. Since answer can be too large, print it modulo $$$10^9+7$$$. Example Input 3 4 1 2 2 3 3 4 2 2 2 4 3 4 1 3 3 2 2 3 2 7 6 1 2 3 4 6 7 3 5 1 3 6 4 7 5 13 3 Note In the first test case, one of the optimal ways is on the following image: In this case, $$$f(1,2)=1$$$, $$$f(1,3)=3$$$, $$$f(1,4)=5$$$, $$$f(2,3)=2$$$, $$$f(2,4)=4$$$, $$$f(3,4)=2$$$, so the sum of these $$$6$$$ numbers is $$$17$$$. In the second test case, one of the optimal ways is on the following image: In this case, $$$f(1,2)=3$$$, $$$f(1,3)=1$$$, $$$f(1,4)=4$$$, $$$f(2,3)=2$$$, $$$f(2,4)=5$$$, $$$f(3,4)=3$$$, so the sum of these $$$6$$$ numbers is $$$18$$$. | 1,800 | true | true | true | true | false | false | false | false | true | false | 3,662 |
1409E | There are $$$n$$$ points on a plane. The $$$i$$$-th point has coordinates $$$(x_i, y_i)$$$. You have two horizontal platforms, both of length $$$k$$$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $$$y$$$-coordinate) and have integer borders. If the left border of the platform is $$$(x, y)$$$ then the right border is $$$(x + k, y)$$$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $$$y$$$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $$$y$$$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $$$t$$$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$1 le k le 10^9$$$) — the number of points and the length of each platform, respectively. The second line of the test case contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$1 le x_i le 10^9$$$), where $$$x_i$$$ is $$$x$$$-coordinate of the $$$i$$$-th point. The third line of the input contains $$$n$$$ integers $$$y_1, y_2, dots, y_n$$$ ($$$1 le y_i le 10^9$$$), where $$$y_i$$$ is $$$y$$$-coordinate of the $$$i$$$-th point. All points are distinct (there is no pair $$$1 le i < j le n$$$ such that $$$x_i = x_j$$$ and $$$y_i = y_j$$$). It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. Example Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Note The picture corresponding to the first test case of the example: Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $$$(1, -1)$$$ and $$$(2, -1)$$$ and the second one between points $$$(4, 3)$$$ and $$$(5, 3)$$$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $$$(3, 7)$$$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $$$(5, 3)$$$ doesn't fall at all because it is already on the platform. | 1,800 | false | false | false | true | false | false | false | true | true | false | 3,623 |
671C | Yasin has an array _a_ containing _n_ integers. Yasin is a 5 year old, so he loves ultimate weird things. Yasin denotes weirdness of an array as maximum _gcd_(_a__i_,u2009u2009_a__j_) value among all 1u2009≤u2009_i_u2009<u2009_j_u2009≤u2009_n_. For _n_u2009≤u20091 weirdness is equal to 0, _gcd_(_x_,u2009u2009_y_) is the greatest common divisor of integers _x_ and _y_. He also defines the ultimate weirdness of an array. Ultimate weirdness is where _f_(_i_,u2009u2009_j_) is weirdness of the new array _a_ obtained by removing all elements between _i_ and _j_ inclusive, so new array is [_a_1... _a__i_u2009-u20091,u2009_a__j_u2009+u20091... _a__n_]. Since 5 year old boys can't code, Yasin asks for your help to find the value of ultimate weirdness of the given array _a_! Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000)xa0— the number of elements in _a_. The next line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009200u2009000), where the _i_-th number is equal to the _i_-th element of the array _a_. It is guaranteed that all _a__i_ are distinct. Output Print a single line containing the value of ultimate weirdness of the array _a_. Note Consider the first sample. _f_(1,u2009u20091) is equal to 3. _f_(2,u2009u20092) is equal to 1. _f_(3,u2009u20093) is equal to 2. _f_(1,u2009u20092), _f_(1,u2009u20093) and _f_(2,u2009u20093) are equal to 0. Thus the answer is 3u2009+u20090u2009+u20090u2009+u20091u2009+u20090u2009+u20092u2009=u20096. | 2,800 | false | false | false | false | true | false | false | false | false | false | 7,138 |
865G | It's Piegirl's birthday soon, and Pieguy has decided to buy her a bouquet of flowers and a basket of chocolates. The flower shop has _F_ different types of flowers available. The _i_-th type of flower always has exactly _p__i_ petals. Pieguy has decided to buy a bouquet consisting of exactly _N_ flowers. He may buy the same type of flower multiple times. The _N_ flowers are then arranged into a bouquet. The position of the flowers within a bouquet matters. You can think of a bouquet as an ordered list of flower types. The chocolate shop sells chocolates in boxes. There are _B_ different types of boxes available. The _i_-th type of box contains _c__i_ pieces of chocolate. Pieguy can buy any number of boxes, and can buy the same type of box multiple times. He will then place these boxes into a basket. The position of the boxes within the basket matters. You can think of the basket as an ordered list of box types. Pieguy knows that Piegirl likes to pluck a petal from a flower before eating each piece of chocolate. He would like to ensure that she eats the last piece of chocolate from the last box just after plucking the last petal from the last flower. That is, the total number of petals on all the flowers in the bouquet should equal the total number of pieces of chocolate in all the boxes in the basket. How many different bouquet+basket combinations can Pieguy buy? The answer may be very large, so compute it modulo 1000000007u2009=u2009109u2009+u20097. Input The first line of input will contain integers _F_, _B_, and _N_ (1u2009≤u2009_F_u2009≤u200910,u20091u2009≤u2009_B_u2009≤u2009100,u20091u2009≤u2009_N_u2009≤u20091018), the number of types of flowers, the number of types of boxes, and the number of flowers that must go into the bouquet, respectively. The second line of input will contain _F_ integers _p_1,u2009_p_2,u2009...,u2009_p__F_ (1u2009≤u2009_p__i_u2009≤u2009109), the numbers of petals on each of the flower types. The third line of input will contain _B_ integers _c_1,u2009_c_2,u2009...,u2009_c__B_ (1u2009≤u2009_c__i_u2009≤u2009250), the number of pieces of chocolate in each of the box types. Output Print the number of bouquet+basket combinations Pieguy can buy, modulo 1000000007u2009=u2009109u2009+u20097. Examples Input 6 5 10 9 3 3 4 9 9 9 9 1 6 4 Note In the first example, there is 1 way to make a bouquet with 9 petals (3u2009+u20093u2009+u20093), and 1 way to make a basket with 9 pieces of chocolate (3u2009+u20093u2009+u20093), for 1 possible combination. There are 3 ways to make a bouquet with 13 petals (3u2009+u20095u2009+u20095,u20095u2009+u20093u2009+u20095,u20095u2009+u20095u2009+u20093), and 5 ways to make a basket with 13 pieces of chocolate (3u2009+u200910,u200910u2009+u20093,u20093u2009+u20093u2009+u20097,u20093u2009+u20097u2009+u20093,u20097u2009+u20093u2009+u20093), for 15 more combinations. Finally there is 1 way to make a bouquet with 15 petals (5u2009+u20095u2009+u20095) and 1 way to make a basket with 15 pieces of chocolate (3u2009+u20093u2009+u20093u2009+u20093u2009+u20093), for 1 more combination. Note that it is possible for multiple types of flowers to have the same number of petals. Such types are still considered different. Similarly different types of boxes may contain the same number of pieces of chocolate, but are still considered different. | 3,300 | true | false | false | false | false | false | false | false | false | false | 6,275 |
877A | Problem - 877A - 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 strings *1100 No tag edit access → Contest materials . Announcement") | 1,100 | false | false | true | false | false | false | false | false | false | false | 6,240 |
703B | Little Mishka is a great traveller and she visited many countries. After thinking about where to travel this time, she chose XXXxa0— beautiful, but little-known northern country. Here are some interesting facts about XXX: 1. XXX consists of _n_ cities, _k_ of whose (just imagine!) are capital cities. 2. All of cities in the country are beautiful, but each is beautiful in its own way. Beauty value of _i_-th city equals to _c__i_. 3. All the cities are consecutively connected by the roads, including 1-st and _n_-th city, forming a cyclic route 1u2009—u20092u2009—u2009...u2009—u2009_n_u2009—u20091. Formally, for every 1u2009≤u2009_i_u2009<u2009_n_ there is a road between _i_-th and _i_u2009+u20091-th city, and another one between 1-st and _n_-th city. 4. Each capital city is connected with each other city directly by the roads. Formally, if city _x_ is a capital city, then for every 1u2009≤u2009_i_u2009≤u2009_n_,u2009u2009_i_u2009≠u2009_x_, there is a road between cities _x_ and _i_. 5. There is at most one road between any two cities. 6. Price of passing a road directly depends on beauty values of cities it connects. Thus if there is a road between cities _i_ and _j_, price of passing it equals _c__i_·_c__j_. Mishka started to gather her things for a trip, but didn't still decide which route to follow and thus she asked you to help her determine summary price of passing each of the roads in XXX. Formally, for every pair of cities _a_ and _b_ (_a_u2009<u2009_b_), such that there is a road between _a_ and _b_ you are to find sum of products _c__a_·_c__b_. Will you help her? Input The first line of the input contains two integers _n_ and _k_ (3u2009≤u2009_n_u2009≤u2009100u2009000,u20091u2009≤u2009_k_u2009≤u2009_n_)xa0— the number of cities in XXX and the number of capital cities among them. The second line of the input contains _n_ integers _c_1,u2009_c_2,u2009...,u2009_c__n_ (1u2009≤u2009_c__i_u2009≤u200910u2009000)xa0— beauty values of the cities. The third line of the input contains _k_ distinct integers _id_1,u2009_id_2,u2009...,u2009_id__k_ (1u2009≤u2009_id__i_u2009≤u2009_n_)xa0— indices of capital cities. Indices are given in ascending order. Output Print the only integerxa0— summary price of passing each of the roads in XXX. Note This image describes first sample case: It is easy to see that summary price is equal to 17. This image describes second sample case: It is easy to see that summary price is equal to 71. | 1,400 | true | false | true | false | false | false | false | false | false | false | 7,010 |
933D | Everything red frightens Nian the monster. So do red paper and... you, red on Codeforces, potential or real. Big Banban has got a piece of paper with endless lattice points, where lattice points form squares with the same area. His most favorite closed shape is the circle because of its beauty and simplicity. Once he had obtained this piece of paper, he prepares it for paper-cutting. He drew _n_ concentric circles on it and numbered these circles from 1 to _n_ such that the center of each circle is the same lattice point and the radius of the _k_-th circle is times the length of a lattice edge. Define the degree of beauty of a lattice point as the summation of the indices of circles such that this lattice point is inside them, or on their bounds. Banban wanted to ask you the total degree of beauty of all the lattice points, but changed his mind. Defining the total degree of beauty of all the lattice points on a piece of paper with _n_ circles as _f_(_n_), you are asked to figure out . Input The first line contains one integer _m_ (1u2009≤u2009_m_u2009≤u20091012). Output In the first line print one integer representing . Note A piece of paper with 5 circles is shown in the following. There are 5 types of lattice points where the degree of beauty of each red point is 1u2009+u20092u2009+u20093u2009+u20094u2009+u20095u2009=u200915, the degree of beauty of each orange point is 2u2009+u20093u2009+u20094u2009+u20095u2009=u200914, the degree of beauty of each green point is 4u2009+u20095u2009=u20099, the degree of beauty of each blue point is 5 and the degree of beauty of each gray point is 0. Therefore, _f_(5)u2009=u20095·15u2009+u20094·14u2009+u20094·9u2009+u20098·5u2009=u2009207. Similarly, _f_(1)u2009=u20095,u2009_f_(2)u2009=u200923,u2009_f_(3)u2009=u200950,u2009_f_(4)u2009=u2009102 and consequently . | 2,900 | true | false | false | false | false | false | true | false | false | false | 5,968 |
1578G | The King wants to marry off his daughter, and he wants her husband to have the greatest innate luckiness possible. To find such a person he decided to hold a heads-or-tails tournament. If person $$$A$$$ with luckiness $$$x$$$ and person $$$B$$$ with luckiness $$$y$$$ play heads-or-tails against each other, person $$$A$$$ wins with probability $$$x/(x+y)$$$. The tournament has several rounds. Each round some participants are split into pairs. Each pair plays against each other, and the loser leaves the tournament. The participants are numbered from $$$1$$$ to $$$n$$$. During the first round, a number $$$k$$$ ($$$1 le k le n$$$) is selected such that $$$n-k/2$$$ is a power of $$$2$$$ (such $$$k$$$ always exists and is unique). Only participants numbered from $$$1$$$ to $$$k$$$ take part in the first round. It ensures that in all other rounds the number of participants is the power of $$$2$$$. During other rounds, all the participants who still have not left the tournament take part. If during some round, participants numbered $$$p_1 < ldots < p_{2m}$$$ take part, then they are split into pairs in the following manner: participant $$$p_{2i-1}$$$ plays against participant $$$p_{2i}$$$ for each $$$i$$$ from $$$1$$$ to $$$m$$$. The rounds are held until only one participant is left. He is declared the winner of the tournament and he will marry the King's daughter. The princess can't wait to find out who is her future husband. She asked every participant to tell her his luckiness. Assuming they did not lie, she wants to know the probability of each participant winning the tournament. As you are the best friend of the princess, she asks you to help her. Input The first line of the input contains the number of participants, $$$n$$$ ($$$2 le n le 3 cdot 10^5$$$). The second line of the input contains $$$n$$$ integer numbers, $$$a_1, ldots, a_{n}$$$ ($$$1 le a_i le 10^9$$$). The luckiness of the $$$i$$$-th participant equals to $$$a_i$$$. | 3,500 | true | false | false | false | false | false | false | false | false | false | 2,748 |
1487C | A big football championship will occur soon! $$$n$$$ teams will compete in it, and each pair of teams will play exactly one game against each other. There are two possible outcomes of a game: the game may result in a tie, then both teams get $$$1$$$ point; one team might win in a game, then the winning team gets $$$3$$$ points and the losing team gets $$$0$$$ points. The score of a team is the number of points it gained during all games that it played. You are interested in a hypothetical situation when all teams get the same score at the end of the championship. A simple example of that situation is when all games result in ties, but you want to minimize the number of ties as well. Your task is to describe a situation (choose the result of each game) so that all teams get the same score, and the number of ties is the minimum possible. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. Then the test cases follow. Each test case is described by one line containing one integer $$$n$$$ ($$$2 le n le 100$$$) — the number of teams. Output For each test case, print $$$frac{n(n - 1)}{2}$$$ integers describing the results of the games in the following order: the first integer should correspond to the match between team $$$1$$$ and team $$$2$$$, the second — between team $$$1$$$ and team $$$3$$$, then $$$1$$$ and $$$4$$$, ..., $$$1$$$ and $$$n$$$, $$$2$$$ and $$$3$$$, $$$2$$$ and $$$4$$$, ..., $$$2$$$ and $$$n$$$, and so on, until the game between the team $$$n - 1$$$ and the team $$$n$$$. The integer corresponding to the game between the team $$$x$$$ and the team $$$y$$$ should be $$$1$$$ if $$$x$$$ wins, $$$-1$$$ if $$$y$$$ wins, or $$$0$$$ if the game results in a tie. All teams should get the same score, and the number of ties should be the minimum possible. If there are multiple optimal answers, print any of them. It can be shown that there always exists a way to make all teams have the same score. Note In the first test case of the example, both teams get $$$1$$$ point since the game between them is a tie. In the second test case of the example, team $$$1$$$ defeats team $$$2$$$ (team $$$1$$$ gets $$$3$$$ points), team $$$1$$$ loses to team $$$3$$$ (team $$$3$$$ gets $$$3$$$ points), and team $$$2$$$ wins against team $$$3$$$ (team $$$2$$$ gets $$$3$$$ points). | 1,500 | true | true | true | false | false | true | true | false | false | true | 3,244 |
1992B | To celebrate his recovery, k1o0n has baked an enormous $$$n$$$ metres long potato casserole. Turns out, Noobish_Monk just can't stand potatoes, so he decided to ruin k1o0n's meal. He has cut it into $$$k$$$ pieces, of lengths $$$a_1, a_2, dots, a_k$$$ meters. k1o0n wasn't keen on that. Luckily, everything can be fixed. In order to do that, k1o0n can do one of the following operations: Pick a piece with length $$$a_i ge 2$$$ and divide it into two pieces with lengths $$$1$$$ and $$$a_i - 1$$$. As a result, the number of pieces will increase by $$$1$$$; Pick a slice $$$a_i$$$ and another slice with length $$$a_j=1$$$ ($$$i e j$$$) and merge them into one piece with length $$$a_i+1$$$. As a result, the number of pieces will decrease by $$$1$$$. Help k1o0n to find the minimum number of operations he needs to do in order to merge the casserole into one piece with length $$$n$$$. For example, if $$$n=5$$$, $$$k=2$$$ and $$$a = [3, 2]$$$, it is optimal to do the following: 1. Divide the piece with length $$$2$$$ into two pieces with lengths $$$2-1=1$$$ and $$$1$$$, as a result $$$a = [3, 1, 1]$$$. 2. Merge the piece with length $$$3$$$ and the piece with length $$$1$$$, as a result $$$a = [4, 1]$$$. 3. Merge the piece with length $$$4$$$ and the piece with length $$$1$$$, as a result $$$a = [5]$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Description of each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 10^9$$$, $$$2 le k le 10^5$$$)xa0— length of casserole and the number of pieces. The second line contains $$$k$$$ integers $$$a_1, a_2, ldots, a_k$$$ ($$$1 le a_i le n - 1$$$, $$$sum a_i = n$$$)xa0— lengths of pieces of casserole, which Noobish_Monk has cut. It is guaranteed that the sum of $$$k$$$ over all $$$t$$$ test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, output the minimum number of operations K1o0n needs to restore his pie after the terror of Noobish_Monk. Example Input 4 5 3 3 1 1 5 2 3 2 11 4 2 3 1 5 16 6 1 6 1 1 1 6 | 800 | true | true | false | false | false | false | false | false | true | false | 318 |
940D | We've tried solitary confinement, waterboarding and listening to Just In Beaver, to no avail. We need something extreme." "Little Alena got an array as a birthday present..." The array _b_ of length _n_ is obtained from the array _a_ of length _n_ and two integers _l_ and _r_xa0(_l_u2009≤u2009_r_) using the following procedure: _b_1u2009=u2009_b_2u2009=u2009_b_3u2009=u2009_b_4u2009=u20090. For all 5u2009≤u2009_i_u2009≤u2009_n_: _b__i_u2009=u20090 if _a__i_,u2009_a__i_u2009-u20091,u2009_a__i_u2009-u20092,u2009_a__i_u2009-u20093,u2009_a__i_u2009-u20094u2009>u2009_r_ and _b__i_u2009-u20091u2009=u2009_b__i_u2009-u20092u2009=u2009_b__i_u2009-u20093u2009=u2009_b__i_u2009-u20094u2009=u20091 _b__i_u2009=u20091 if _a__i_,u2009_a__i_u2009-u20091,u2009_a__i_u2009-u20092,u2009_a__i_u2009-u20093,u2009_a__i_u2009-u20094u2009<u2009_l_ and _b__i_u2009-u20091u2009=u2009_b__i_u2009-u20092u2009=u2009_b__i_u2009-u20093u2009=u2009_b__i_u2009-u20094u2009=u20090 _b__i_u2009=u2009_b__i_u2009-u20091 otherwise You are given arrays _a_ and _b_' of the same length. Find two integers _l_ and _r_xa0(_l_u2009≤u2009_r_), such that applying the algorithm described above will yield an array _b_ equal to _b_'. It's guaranteed that the answer exists. Input The first line of input contains a single integer _n_ (5u2009≤u2009_n_u2009≤u2009105)xa0— the length of _a_ and _b_'. The second line of input contains _n_ space separated integers _a_1,u2009...,u2009_a__n_ (u2009-u2009109u2009≤u2009_a__i_u2009≤u2009109)xa0— the elements of _a_. The third line of input contains a string of _n_ characters, consisting of 0 and 1xa0— the elements of _b_'. Note that they are not separated by spaces. Output Output two integers _l_ and _r_xa0(u2009-u2009109u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009109), conforming to the requirements described above. If there are multiple solutions, output any of them. It's guaranteed that the answer exists. Examples Input 10 -10 -9 -8 -7 -6 6 7 8 9 10 0000111110 Note In the first test case any pair of _l_ and _r_ pair is valid, if 6u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009109, in that case _b_5u2009=u20091, because _a_1,u2009...,u2009_a_5u2009<u2009_l_. | 1,600 | false | false | true | false | false | false | false | true | false | false | 5,935 |
898B | Vasya has _n_ burles. One bottle of Ber-Cola costs _a_ burles and one Bars bar costs _b_ burles. He can buy any non-negative integer number of bottles of Ber-Cola and any non-negative integer number of Bars bars. Find out if it's possible to buy some amount of bottles of Ber-Cola and Bars bars and spend exactly _n_ burles. In other words, you should find two non-negative integers _x_ and _y_ such that Vasya can buy _x_ bottles of Ber-Cola and _y_ Bars bars and _x_·_a_u2009+u2009_y_·_b_u2009=u2009_n_ or tell that it's impossible. Input First line contains single integer _n_ (1u2009≤u2009_n_u2009≤u200910u2009000u2009000)xa0— amount of money, that Vasya has. Second line contains single integer _a_ (1u2009≤u2009_a_u2009≤u200910u2009000u2009000)xa0— cost of one bottle of Ber-Cola. Third line contains single integer _b_ (1u2009≤u2009_b_u2009≤u200910u2009000u2009000)xa0— cost of one Bars bar. Output If Vasya can't buy Bars and Ber-Cola in such a way to spend exactly _n_ burles print «NO» (without quotes). Otherwise in first line print «YES» (without quotes). In second line print two non-negative integers _x_ and _y_xa0— number of bottles of Ber-Cola and number of Bars bars Vasya should buy in order to spend exactly _n_ burles, i.e. _x_·_a_u2009+u2009_y_·_b_u2009=u2009_n_. If there are multiple answers print any of them. Any of numbers _x_ and _y_ can be equal 0. Note In first example Vasya can buy two bottles of Ber-Cola and one Bars bar. He will spend exactly 2·2u2009+u20091·3u2009=u20097 burles. In second example Vasya can spend exactly _n_ burles multiple ways: buy two bottles of Ber-Cola and five Bars bars; buy four bottles of Ber-Cola and don't buy Bars bars; don't buy Ber-Cola and buy 10 Bars bars. In third example it's impossible to but Ber-Cola and Bars bars in order to spend exactly _n_ burles. | 1,100 | false | false | true | false | false | false | true | false | false | false | 6,157 |
992B | Today on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them as well. We define a pair of integers (_a_,u2009_b_) good, if _GCD_(_a_,u2009_b_)u2009=u2009_x_ and _LCM_(_a_,u2009_b_)u2009=u2009_y_, where _GCD_(_a_,u2009_b_) denotes the denotes the such that _l_u2009≤u2009_a_,u2009_b_u2009≤u2009_r_. Note that pairs (_a_,u2009_b_) and (_b_,u2009_a_) are considered different if _a_u2009≠u2009_b_. Input The only line contains four integers _l_,u2009_r_,u2009_x_,u2009_y_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009109, 1u2009≤u2009_x_u2009≤u2009_y_u2009≤u2009109). Output In the only line print the only integerxa0— the answer for the problem. Note In the first example there are two suitable good pairs of integers (_a_,u2009_b_): (1,u20092) and (2,u20091). In the second example there are four suitable good pairs of integers (_a_,u2009_b_): (1,u200912), (12,u20091), (3,u20094) and (4,u20093). In the third example there are good pairs of integers, for example, (3,u200930), but none of them fits the condition _l_u2009≤u2009_a_,u2009_b_u2009≤u2009_r_. | 1,600 | true | false | false | false | false | false | false | false | false | false | 5,738 |
1129A1 | This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of $$$n$$$ stations, enumerated from $$$1$$$ through $$$n$$$. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station $$$i$$$ is station $$$i+1$$$ if $$$1 leq i < n$$$ or station $$$1$$$ if $$$i = n$$$. It takes the train $$$1$$$ second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver $$$m$$$ candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from $$$1$$$ through $$$m$$$. Candy $$$i$$$ ($$$1 leq i leq m$$$), now at station $$$a_i$$$, should be delivered to station $$$b_i$$$ ($$$a_i eq b_i$$$). The blue numbers on the candies correspond to $$$b_i$$$ values. The image corresponds to the $$$1$$$-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 100$$$; $$$1 leq m leq 200$$$) — the number of stations and the number of candies, respectively. The $$$i$$$-th of the following $$$m$$$ lines contains two space-separated integers $$$a_i$$$ and $$$b_i$$$ ($$$1 leq a_i, b_i leq n$$$; $$$a_i eq b_i$$$) — the station that initially contains candy $$$i$$$ and the destination station of the candy, respectively. Output In the first and only line, print $$$n$$$ space-separated integers, the $$$i$$$-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station $$$i$$$. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Note Consider the second sample. If the train started at station $$$1$$$, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station $$$2$$$. This step takes $$$1$$$ second. 3. Deliver the first candy. 4. Proceed to station $$$1$$$. This step takes $$$1$$$ second. 5. Load the second candy onto the train. 6. Proceed to station $$$2$$$. This step takes $$$1$$$ second. 7. Deliver the second candy. 8. Proceed to station $$$1$$$. This step takes $$$1$$$ second. 9. Load the third candy onto the train. 10. Proceed to station $$$2$$$. This step takes $$$1$$$ second. 11. Deliver the third candy. Hence, the train needs $$$5$$$ seconds to complete the tasks. If the train were to start at station $$$2$$$, however, it would need to move to station $$$1$$$ before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is $$$5+1 = 6$$$ seconds. | 1,700 | false | true | false | false | false | false | true | false | false | false | 5,077 |
1474C | You found a useless array $$$a$$$ of $$$2n$$$ positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of $$$a$$$. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer $$$x$$$. 2. Then you do the following operation $$$n$$$ times: select two elements of array with sum equals $$$x$$$; remove them from $$$a$$$ and replace $$$x$$$ with maximum of that two numbers. For example, if initially $$$a = [3, 5, 1, 2]$$$, you can select $$$x = 6$$$. Then you can select the second and the third elements of $$$a$$$ with sum $$$5 + 1 = 6$$$ and throw them out. After this operation, $$$x$$$ equals $$$5$$$ and there are two elements in array: $$$3$$$ and $$$2$$$. You can throw them out on the next operation. Note, that you choose $$$x$$$ before the start and can't change it as you want between the operations. Determine how should you behave to throw out all elements of $$$a$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 leq n leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, dots, a_{2n}$$$ ($$$1 leq a_i leq 10^6$$$)xa0— the initial array $$$a$$$. It is guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$1000$$$. Output For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise. If it is possible to throw out all elements, print the initial value of $$$x$$$ you've chosen. Print description of $$$n$$$ operations next. For each operation, print the pair of integers you remove. Example Input 4 2 3 5 1 2 3 1 1 8 8 64 64 2 1 1 2 4 5 1 2 3 4 5 6 7 14 3 11 Output YES 6 1 5 2 3 NO NO YES 21 14 7 3 11 5 6 2 4 3 1 Note The first test case was described in the statement. In the second and third test cases, we can show that it is impossible to throw out all elements of array $$$a$$$. | 1,700 | false | true | true | false | true | true | true | false | true | false | 3,308 |
1791G2 | The only difference between the easy and hard versions are the locations you can teleport to. Consider the points $$$0,1,dots,n+1$$$ on the number line. There is a teleporter located on each of the points $$$1,2,dots,n$$$. At point $$$i$$$, you can do the following: Move left one unit: it costs $$$1$$$ coin. Move right one unit: it costs $$$1$$$ coin. Use a teleporter at point $$$i$$$, if it exists: it costs $$$a_i$$$ coins. As a result, you can choose whether to teleport to point $$$0$$$ or point $$$n+1$$$. Once you use a teleporter, you can't use it again. You have $$$c$$$ coins, and you start at point $$$0$$$. What's the most number of teleporters you can use? Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$c$$$ ($$$1 leq n leq 2cdot10^5$$$; $$$1 leq c leq 10^9$$$) xa0— the length of the array and the number of coins you have respectively. The following line contains $$$n$$$ space-separated positive integers $$$a_1,a_2,dots,a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the costs to use the teleporters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output For each test case, output the maximum number of teleporters you can use. Example Input 10 5 6 1 1 1 1 1 8 32 100 52 13 6 9 4 100 35 1 1 5 4 5 4 3 2 1 5 9 2 3 1 4 1 5 8 2 3 1 4 1 4 3 2 3 4 1 4 9 5 4 3 3 2 14 7 5 5 600000000 500000000 400000000 300000000 200000000 100000000 Output 2 3 0 1 3 2 1 1 2 2 Note In the first test case, you can move one unit to the right, use the teleporter at index $$$1$$$ and teleport to point $$$n+1$$$, move one unit to the left and use the teleporter at index $$$5$$$. You are left with $$$6-1-1-1-1 = 2$$$ coins, and wherever you teleport, you won't have enough coins to use another teleporter. You have used two teleporters, so the answer is two. In the second test case, you go four units to the right and use the teleporter to go to $$$n+1$$$, then go three units left and use the teleporter at index $$$6$$$ to go to $$$n+1$$$, and finally, you go left four times and use the teleporter. The total cost will be $$$4+6+3+4+4+9 = 30$$$, and you used three teleporters. In the third test case, you don't have enough coins to use any teleporter, so the answer is zero. | 1,900 | false | true | false | false | false | false | false | true | true | false | 1,528 |
1147B | Inaka has a disc, the circumference of which is $$$n$$$ units. The circumference is equally divided by $$$n$$$ points numbered clockwise from $$$1$$$ to $$$n$$$, such that points $$$i$$$ and $$$i + 1$$$ ($$$1 leq i < n$$$) are adjacent, and so are points $$$n$$$ and $$$1$$$. There are $$$m$$$ straight segments on the disc, the endpoints of which are all among the aforementioned $$$n$$$ points. Inaka wants to know if her image is rotationally symmetrical, i.e. if there is an integer $$$k$$$ ($$$1 leq k < n$$$), such that if all segments are rotated clockwise around the center of the circle by $$$k$$$ units, the new image will be the same as the original one. Input The first line contains two space-separated integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 100,000$$$, $$$1 leq m leq 200,000$$$)xa0— the number of points and the number of segments, respectively. The $$$i$$$-th of the following $$$m$$$ lines contains two space-separated integers $$$a_i$$$ and $$$b_i$$$ ($$$1 leq a_i, b_i leq n$$$, $$$a_i eq b_i$$$) that describe a segment connecting points $$$a_i$$$ and $$$b_i$$$. It is guaranteed that no segments coincide. Output Output one linexa0— "Yes" if the image is rotationally symmetrical, and "No" otherwise (both excluding quotation marks). You can output each letter in any case (upper or lower). Examples Input 12 6 1 3 3 7 5 7 7 11 9 11 11 3 Input 9 6 4 5 5 6 7 8 8 9 1 2 2 3 Note The first two examples are illustrated below. Both images become the same as their respective original ones after a clockwise rotation of $$$120$$$ degrees around the center. | 1,900 | false | false | false | false | false | false | true | false | false | false | 4,982 |
679D | Bearland has _n_ cities, numbered 1 through _n_. There are _m_ bidirectional roads. The _i_-th road connects two distinct cities _a__i_ and _b__i_. No two roads connect the same pair of cities. It's possible to get from any city to any other city (using one or more roads). The distance between cities _a_ and _b_ is defined as the minimum number of roads used to travel between _a_ and _b_. Limak is a grizzly bear. He is a criminal and your task is to catch him, or at least to try to catch him. You have only two days (today and tomorrow) and after that Limak is going to hide forever. Your main weapon is BCD (Bear Criminal Detector). Where you are in some city, you can use BCD and it tells you the distance between you and a city where Limak currently is. Unfortunately, BCD can be used only once a day. You don't know much about Limak's current location. You assume that he is in one of _n_ cities, chosen uniformly at random (each city with probability ). You decided for the following plan: 1. Choose one city and use BCD there. After using BCD you can try to catch Limak (but maybe it isn't a good idea). In this case you choose one city and check it. You win if Limak is there. Otherwise, Limak becomes more careful and you will never catch him (you loose). 2. Wait 24 hours to use BCD again. You know that Limak will change his location during that time. In detail, he will choose uniformly at random one of roads from his initial city, and he will use the chosen road, going to some other city. 3. Tomorrow, you will again choose one city and use BCD there. 4. Finally, you will try to catch Limak. You will choose one city and check it. You will win if Limak is there, and loose otherwise. Each time when you choose one of cities, you can choose any of _n_ cities. Let's say it isn't a problem for you to quickly get somewhere. What is the probability of finding Limak, if you behave optimally? Input The first line of the input contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009400, )xa0— the number of cities and the number of roads, respectively. Then, _m_ lines follow. The _i_-th of them contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_)xa0— cities connected by the _i_-th road. No two roads connect the same pair of cities. It's possible to get from any city to any other city. Output Print one real numberxa0— the probability of finding Limak, if you behave optimally. Your answer will be considered correct if its absolute error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct if _a_u2009-u2009_b_u2009≤u200910u2009-u20096. Note In the first sample test, there are three cities and there is a road between every pair of cities. Let's analyze one of optimal scenarios. 1. Use BCD in city 1. 2. You wait and Limak moves to some other city. 3. Use BCD again in city 1 (though it's allowed to use it in some other city). If the distance is 0 then you're sure Limak is in this city (you win). If the distance is 1 then Limak is in city 2 or city 3. Then you should guess that he is in city 2 (guessing city 3 would be fine too). You loose only if Limak was in city 2 first and then he moved to city 3. The probability of loosing is . The answer is . | 2,900 | true | false | true | false | false | false | true | false | false | true | 7,107 |
1982E | Let $$$bit(x)$$$ denote the number of ones in the binary representation of a non-negative integer $$$x$$$. A subarray of an array is called $$$k$$$-good if it consists only of numbers with no more than $$$k$$$ ones in their binary representation, i.e., a subarray $$$(l, r)$$$ of array $$$a$$$ is good if for any $$$i$$$ such that $$$l le i le r$$$ condition $$$bit(a_{i}) le k$$$ is satisfied. You are given an array $$$a$$$ of length $$$n$$$, consisting of consecutive non-negative integers starting from $$$0$$$, i.e., $$$a_{i} = i$$$ for $$$0 le i le n - 1$$$ (in $$$0$$$-based indexing). You need to count the number of $$$k$$$-good subarrays in this array. As the answer can be very large, output it modulo $$$10^{9} + 7$$$. Input Each test consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 10^{4}$$$)xa0— the number of test cases. The following lines describe the test cases. The single line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 le n le 10^{18}, 1 le k le 60$$$). Note For the first test case $$$a = [0, 1, 2, 3, 4, 5]$$$, $$$k = 1$$$. To find the answer, let's write all the numbers in binary representation: $$$$$$a = [color{green}{000}, color{green}{001}, color{green}{010}, color{red}{011}, color{green}{100}, color{red}{101}]$$$$$$ From this, it can be seen that the numbers $$$3$$$ and $$$5$$$ have $$$2 ge (k = 1)$$$ ones in their binary representation, so the answer should include all subarrays that do not contain either $$$3$$$ or $$$5$$$, which are the subarrays (in $$$0$$$-based indexing): ($$$0$$$, $$$0$$$), ($$$0$$$, $$$1$$$), ($$$0$$$, $$$2$$$), ($$$1$$$, $$$1$$$), ($$$1$$$, $$$2$$$), ($$$2$$$, $$$2$$$), ($$$4$$$, $$$4$$$). | 2,300 | true | false | false | true | false | false | true | false | false | false | 392 |
1718A2 | This is the hard version of this problem. The difference between easy and hard versions is only the constraints on $$$a_i$$$ and on $$$n$$$. You can make hacks only if both versions of the problem are solved. Burenka is the crown princess of Buryatia, and soon she will become the $$$n$$$-th queen of the country. There is an ancient tradition in Buryatiaxa0— before the coronation, the ruler must show their strength to the inhabitants. To determine the strength of the $$$n$$$-th ruler, the inhabitants of the country give them an array of $$$a$$$ of exactly $$$n$$$ numbers, after which the ruler must turn all the elements of the array into zeros in the shortest time. The ruler can do the following two-step operation any number of times: select two indices $$$l$$$ and $$$r$$$, so that $$$1 le l le r le n$$$ and a non-negative integer $$$x$$$, then for all $$$l leq i leq r$$$ assign $$$a_i := a_i oplus x$$$, where $$$oplus$$$ denotes the $$$ xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1 le n le 10^5)$$$ - the size of the array The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, cdots , a_n$$$ $$$(0 le a_i < 2^{30})$$$xa0— elements of the array. It is guaranteed that the sum of $$$n$$$ in all tests does not exceed $$$10^5$$$. Output For each test case, output a single number xa0— the minimum time that Burenka will need. Example Input 7 4 5 5 5 5 3 1 3 2 2 0 0 3 2 5 7 6 1 2 3 3 2 1 10 27 27 34 32 2 31 23 56 52 4 5 1822 1799 57 23 55 Note In the first test case, Burenka can choose segment $$$l = 1$$$, $$$r = 4$$$, and $$$x=5$$$. so it will fill the array with zeros in $$$2$$$ seconds. In the second test case, Burenka first selects segment $$$l = 1$$$, $$$r = 2$$$, and $$$x = 1$$$, after which $$$a = [0, 2, 2]$$$, and then the segment $$$l = 2$$$, $$$r = 3$$$, and $$$x=2$$$, which fills the array with zeros. In total, Burenka will spend $$$2$$$ seconds. | 1,900 | false | true | false | true | true | false | false | false | false | false | 1,974 |
1260D | You are playing a computer game, where you lead a party of $$$m$$$ soldiers. Each soldier is characterised by his agility $$$a_i$$$. The level you are trying to get through can be represented as a straight line segment from point $$$0$$$ (where you and your squad is initially located) to point $$$n + 1$$$ (where the boss is located). The level is filled with $$$k$$$ traps. Each trap is represented by three numbers $$$l_i$$$, $$$r_i$$$ and $$$d_i$$$. $$$l_i$$$ is the location of the trap, and $$$d_i$$$ is the danger level of the trap: whenever a soldier with agility lower than $$$d_i$$$ steps on a trap (that is, moves to the point $$$l_i$$$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $$$r_i$$$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers. You have $$$t$$$ seconds to complete the level — that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $$$x$$$, you may move to $$$x + 1$$$ or $$$x - 1$$$. This action consumes one second; if your location is $$$x$$$ and the location of your squad is $$$x$$$, you may move to $$$x + 1$$$ or to $$$x - 1$$$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i.u2009e. the point your squad is moving into contains a non-disarmed trap with $$$d_i$$$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $$$x$$$ and there is a trap $$$i$$$ with $$$r_i = x$$$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers. You have to choose the maximum number of soldiers such that they all can be brought from the point $$$0$$$ to the point $$$n + 1$$$ (where the boss waits) in no more than $$$t$$$ seconds. Input The first line contains four integers $$$m$$$, $$$n$$$, $$$k$$$ and $$$t$$$ ($$$1 le m, n, k, t le 2 cdot 10^5$$$, $$$n < t$$$) — the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $$$m$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_m$$$ ($$$1 le a_i le 2 cdot 10^5$$$), where $$$a_i$$$ is the agility of the $$$i$$$-th soldier. Then $$$k$$$ lines follow, containing the descriptions of traps. Each line contains three numbers $$$l_i$$$, $$$r_i$$$ and $$$d_i$$$ ($$$1 le l_i le r_i le n$$$, $$$1 le d_i le 2 cdot 10^5$$$) — the location of the trap, the location where the trap can be disarmed, and its danger level, respectively. Output Print one integer — the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $$$t$$$ seconds. Example Input 5 6 4 14 1 2 3 4 5 1 5 2 1 2 5 2 3 5 3 5 3 Note In the first example you may take soldiers with agility $$$3$$$, $$$4$$$ and $$$5$$$ with you. The course of action is as follows: go to $$$2$$$ without your squad; disarm the trap $$$2$$$; go to $$$3$$$ without your squad; disartm the trap $$$3$$$; go to $$$0$$$ without your squad; go to $$$7$$$ with your squad. The whole plan can be executed in $$$13$$$ seconds. | 1,900 | false | true | false | true | false | false | false | true | true | false | 4,391 |
1009F | Problem - 1009F - 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 data structures dsu trees *2300 No tag edit access → Contest materials is an index $$$j$$$ such that: for every $$$k < j$$$, $$$d_{x, k} < d_{x, j}$$$; for every $$$k > j$$$, $$$d_{x, k} le d_{x, j}$$$. For every vertex in the tree calculate its dominant index. Input The first line contains one integer $$$n$$$ ($$$1 le n le 10^6$$$) — the number of vertices in a tree. Then $$$n - 1$$$ lines follow, each containing two integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le n$$$, $$$x e y$$$). This line denotes an edge of the tree. It is guaranteed that these edges form a tree. Output Output $$$n$$$ numbers. $$$i$$$-th number should be equal to the dominant index of vertex $$$i$$$. Examples Input 4 1 2 2 3 3 4 Output 0 0 0 0 Input 4 1 2 1 3 1 4 Output 1 0 0 0 Input 4 1 2 2 3 2 4 Output 2 1 0 0 | 2,300 | false | false | false | false | true | false | false | false | false | false | 5,637 |
1725H | One day, you are accepted as being Dr. Chanek's assistant. The first task given by Dr. Chanek to you is to take care and store his magical stones. Dr. Chanek has $$$N$$$ magical stones with $$$N$$$ being an even number. Those magical stones are numbered from $$$1$$$ to $$$N$$$. Magical stone $$$i$$$ has a strength of $$$A_i$$$. A magical stone can be painted with two colours, namely the colour black or the colour white. You are tasked to paint the magical stones with the colour black or white and store the magical stones into a magic box with a magic coefficient $$$Z$$$ ($$$0 leq Z leq 2$$$). The painting of the magical stones must be done in a way such that there are $$$frac{N}{2}$$$ black magical stones and $$$frac{N}{2}$$$ white magical stones. Define $$$ ext{concat}(x, y)$$$ for two integers $$$x$$$ and $$$y$$$ as the result of concatenating the digits of $$$x$$$ to the left of $$$y$$$ in their decimal representation without changing the order. As an example, $$$ ext{concat}(10, 24)$$$ will result in $$$1024$$$. For a magic box with a magic coefficient $$$Z$$$, magical stone $$$i$$$ will react with magical stone $$$j$$$ if the colours of both stones are different and $$$ ext{concat}(A_i, A_j) imes ext{concat}(A_j, A_i) + A_i imes A_j equiv Z mod 3$$$. A magical stone that is reacting will be very hot and dangerous. Because of that, you must colour the magical stones and determine the magic coefficient $$$Z$$$ of the magic box in a way such that there is no magical stone that reacts, or report if it is impossible. Input The first line contains a single even integer $$$N$$$ ($$$2 le N le 10^5$$$) — the number of magical stones Dr. Chanek has. The second line contains $$$N$$$ integer $$$A_1, A_2, ldots, A_N$$$ ($$$1 leq A_i leq 10^9$$$) — the strengths of all magical stones. Output If it is not possible to satisfy the condition of the problem, output $$$-1$$$. Otherwise, output two lines. The first line contains an integer $$$Z$$$ denoting the magic coefficient of the magic box. The second line contains a string $$$S$$$ of length $$$N$$$. $$$S_i$$$ is $$$0$$$ if magical stone $$$i$$$ is coloured black or $$$1$$$ if magical stone $$$i$$$ is coloured white. If there are more than one possibilities of colouring and choosing the magic coefficient $$$Z$$$, output any of them. Note By giving the above colouring, it can be seen that: $$$i=1, j=2 longrightarrow ext{concat}(4, 10) imes ext{concat}(10, 4) + 10 imes 4 = 410 imes 104 + 40 = 42680 equiv 2 mod 3$$$ $$$i=1, j=3 longrightarrow ext{concat}(4, 9) imes ext{concat}(9, 4) + 4 imes 9 = 49 imes 94 + 36 = 4642 equiv 1 mod 3$$$ $$$i=4, j=2 longrightarrow ext{concat}(14, 10) imes ext{concat}(10, 14) + 10 imes 14 = 1410 imes 1014 + 140 = 1429880 equiv 2 mod 3$$$ $$$i=4, j=3 longrightarrow ext{concat}(14, 9) imes ext{concat}(9, 14) + 14 imes 9 = 149 imes 914 + 126 = 136312 equiv 1 mod 3$$$ Because of that, by choosing $$$Z = 0$$$, it can be guaranteed that there is no magical stone that reacts. | 1,800 | true | false | false | false | false | true | false | false | false | false | 1,939 |
1759C | Vlad came home and found out that someone had reconfigured the old thermostat to the temperature of $$$a$$$. The thermostat can only be set to a temperature from $$$l$$$ to $$$r$$$ inclusive, the temperature cannot change by less than $$$x$$$. Formally, in one operation you can reconfigure the thermostat from temperature $$$a$$$ to temperature $$$b$$$ if $$$a - b ge x$$$ and $$$l le b le r$$$. You are given $$$l$$$, $$$r$$$, $$$x$$$, $$$a$$$ and $$$b$$$. Find the minimum number of operations required to get temperature $$$b$$$ from temperature $$$a$$$, or say that it is impossible. Input The first line of input data contains the single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases in the test. The descriptions of the test cases follow. The first line of each case contains three integers $$$l$$$, $$$r$$$ and $$$x$$$ ($$$-10^9 le l le r le 10^9$$$, $$$1 le x le 10^9$$$) — range of temperature and minimum temperature change. The second line of each case contains two integers $$$a$$$ and $$$b$$$ ($$$l le a, b le r$$$) — the initial and final temperatures. Output Output $$$t$$$ numbers, each of which is the answer to the corresponding test case. If it is impossible to achieve the temperature $$$b$$$, output -1, otherwise output the minimum number of operations. Example Input 10 3 5 6 3 3 0 15 5 4 5 0 10 5 3 7 3 5 6 3 4 -10 10 11 -5 6 -3 3 4 1 0 -5 10 8 9 2 1 5 1 2 5 -1 4 3 0 2 -6 3 6 -1 -4 Output 0 2 3 -1 1 -1 3 1 3 -1 Note In the first example, the thermostat is already set up correctly. In the second example, you can achieve the desired temperature as follows: $$$4 ightarrow 10 ightarrow 5$$$. In the third example, you can achieve the desired temperature as follows: $$$3 ightarrow 8 ightarrow 2 ightarrow 7$$$. In the fourth test, it is impossible to make any operation. | 1,100 | true | true | false | false | false | false | false | false | false | false | 1,760 |
1914C | Monocarp is playing a computer game. In order to level up his character, he can complete quests. There are $$$n$$$ quests in the game, numbered from $$$1$$$ to $$$n$$$. Monocarp can complete quests according to the following rules: the $$$1$$$-st quest is always available for completion; the $$$i$$$-th quest is available for completion if all quests $$$j < i$$$ have been completed at least once. Note that Monocarp can complete the same quest multiple times. For each completion, the character gets some amount of experience points: for the first completion of the $$$i$$$-th quest, he gets $$$a_i$$$ experience points; for each subsequent completion of the $$$i$$$-th quest, he gets $$$b_i$$$ experience points. Monocarp is a very busy person, so he has free time to complete no more than $$$k$$$ quests. Your task is to calculate the maximum possible total experience Monocarp can get if he can complete no more than $$$k$$$ quests. 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 $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$1 le k le 2 cdot 10^5$$$)xa0— the number of quests and the maximum number of quests Monocarp can complete, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^3$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le 10^3$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0— the maximum possible total experience Monocarp can get if he can complete no more than $$$k$$$ quests. Example Input 4 4 7 4 3 1 2 1 1 1 1 3 2 1 2 5 3 1 8 5 5 3 2 4 1 4 2 3 1 4 7 6 4 1 4 5 4 5 10 1 5 1 2 5 1 Note In the first test case, one of the possible quest completion sequences is as follows: $$$1, 1, 2, 3, 2, 4, 4$$$; its total experience is equal to $$$underline{4} + 1 + underline{3} + underline{1} + 1 + underline{2} + 1 = 13$$$ (the underlined numbers correspond to the instances when we complete a quest for the first time). In the second test case, one of the possible quest completion sequences is as follows: $$$1, 1$$$; its total experience is equal to $$$underline{1} + 3 = 4$$$. In the third test case, one of the possible quest completion sequences is as follows: $$$1, 2, 2, 2, 3$$$; its total experience is equal to $$$underline{3} + underline{2} + 3 + 3 + underline{4} = 15$$$. | 1,100 | true | true | false | false | false | false | false | false | false | false | 817 |
553E | Kyoya Ootori wants to take the train to get to school. There are _n_ train stations and _m_ one-way train lines going between various stations. Kyoya is currently at train station 1, and the school is at station _n_. To take a train, he must pay for a ticket, and the train also takes a certain amount of time. However, the trains are not perfect and take random amounts of time to arrive at their destination. If Kyoya arrives at school strictly after _t_ time units, he will have to pay a fine of _x_. Each train line is described by a ticket price, and a probability distribution on the time the train takes. More formally, train line _i_ has ticket cost _c__i_, and a probability distribution _p__i_,u2009_k_ which denotes the probability that this train will take _k_ time units for all 1u2009≤u2009_k_u2009≤u2009_t_. Amounts of time that each of the trains used by Kyouya takes are mutually independent random values (moreover, if Kyoya travels along the same train more than once, it is possible for the train to take different amounts of time and those amounts are also independent one from another). Kyoya wants to get to school by spending the least amount of money in expectation (for the ticket price plus possible fine for being late). Of course, Kyoya has an optimal plan for how to get to school, and every time he arrives at a train station, he may recalculate his plan based on how much time he has remaining. What is the expected cost that Kyoya will pay to get to school if he moves optimally? Input The first line of input contains four integers _n_,u2009_m_,u2009_t_,u2009_x_ (2u2009u2009≤u2009u2009_n_u2009u2009≤u200950, 1u2009≤u2009_m_u2009≤u2009100, 1u2009≤u2009_t_u2009≤u200920u2009000, 0u2009≤u2009_x_u2009≤u2009106). The next 2_m_ lines contain the description of the trains. The 2_i_-th line will have 3 integers _a__i_,u2009_b__i_,u2009_c__i_, representing a one way train from station _a__i_ to _b__i_ with ticket cost _c__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_, 0u2009≤u2009_c__i_u2009≤u2009106). There will always be at least one path from any station to the school. The (2_i_u2009+u20091)-th line will contain _t_ integers, _p__i_,u20091,u2009_p__i_,u20092,u2009...,u2009_p__i_,u2009_t_ where _p__i_,u2009_k_u2009/u2009100000 is the probability that this train will take _k_ units of time to traverse (0u2009≤u2009_p__i_,u2009_k_u2009≤u2009100u2009000 for 1u2009≤u2009_k_u2009≤u2009_t_, ). It is guaranteed that there is no more than one train between each pair of platforms in each of the directions. Output Print a single real number that is equal to an optimal expected cost of getting to school. The answer will be considered correct if its relative or absolute error doesn't exceed 10u2009-u20096. Examples Input 4 4 5 1 1 2 0 50000 0 50000 0 0 2 3 0 10000 0 0 0 90000 3 4 0 100000 0 0 0 0 2 4 0 0 0 0 50000 50000 Input 4 4 5 1 1 2 100 50000 0 50000 0 0 2 3 100 10000 0 0 0 90000 3 4 100 100000 0 0 0 0 2 4 100 0 0 0 50000 50000 Note The optimal strategy in the first case is as follows: First, travel along first train line. With probability 1u2009/u20092 Kyoya will take 1 time unit. Otherwise, Kyoya will take 3 time units. If the train takes 1 time unit, travel along the 4th train line. Kyoya will make it to school in time with probability 1u2009/u20092. Otherwise, if the train takes 3 time units, travel along the 2nd train line. Kyoya will make it to school in time with probability 1u2009/u200910. Since the cost of all train lines are zero, we can just look at the probability that Kyoya will incur the penalty. The probability that Kyoya will have to pay the penalty is 1u2009/u20092u2009×u20091u2009/u20092u2009+u20091u2009/u20092u2009×u20099u2009/u200910u2009=u20097u2009/u200910. We can show that no other strategy is strictly better. The optimal strategy in the second case is to travel along 1u2009→u20092u2009→u20094 no matter what. Kyoya will incur the penalty with probability 3u2009/u20094, and the cost of the trains is 200, thus the expected cost is 200.75. | 3,200 | true | false | false | true | false | false | false | false | false | true | 7,618 |
333E | Problem - 333E - 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 bitmasks brute force geometry sortings *2500 No tag edit access → Contest materials . He can choose the centers of the circles only from the _n_ options granted by the Company. He is free to choose the radius of the circles himself (all three radiuses must be equal), but please note that the larger the radius is, the more he gets paid. Help Gerald earn as much as possible. Input The first line contains a single integer _n_ — the number of centers (3u2009≤u2009_n_u2009≤u20093000). The following _n_ lines each contain two integers _x__i_,u2009_y__i_ (u2009-u2009104u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009104) — the coordinates of potential circle centers, provided by the Company. All given points are distinct. Output Print a single real number — maximum possible radius of circles. The answer will be accepted if its relative or absolute error doesn't exceed 10u2009-u20096. Examples Input 3 0 1 1 0 1 1 Output 0.50000000000000000000 Input 7 2 -3 -2 -3 3 0 -3 -1 1 -2 2 -2 -1 0 Output 1.58113883008418980000 | 2,500 | false | false | false | false | false | false | true | true | true | false | 8,501 |
411A | Problem - 411A - Codeforces =============== xa0 ") . Each character is either a large English letter, or a small English letter, or a digit, or one of characters: "!", "?", ".", ",", "_". Output If the password is complex enough, print message "Correct" (without the quotes), otherwise print message "Too weak" (without the quotes). Examples Input abacaba Output Too weak Input X12345 Output Too weak Input CONTEST_is_STARTED!!11 Output Correct | 800 | false | false | true | false | false | false | false | false | false | false | 8,182 |
1994A | Petr, watching Sergey's stream, came up with a matrix $$$a$$$, consisting of $$$n$$$ rows and $$$m$$$ columns (the number in the $$$i$$$-th row and $$$j$$$-th column is denoted as $$$a_{i, j}$$$), which contains all integers from $$$1$$$ to $$$n cdot m$$$. But he didn't like the arrangement of the numbers, and now he wants to come up with a new matrix $$$b$$$, consisting of $$$n$$$ rows and $$$m$$$ columns, which will also contain all integers from $$$1$$$ to $$$n cdot m$$$, such that for any $$$1 leq i leq n, 1 leq j leq m$$$ it holds that $$$a_{i, j} e b_{i, j}$$$. You are given the matrix $$$a$$$, construct any matrix $$$b$$$ that meets Petr's requirements, or determine that it is impossible. Hurry up! Otherwise, he will donate all his money to the stream in search of an answer to his question. Input Each test consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0— the number of test cases. Then follows the description of the test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 10$$$)xa0— the number of rows and columns of matrix $$$a$$$. The next $$$n$$$ lines contain $$$m$$$ integers each, describing matrix $$$a$$$. The $$$i$$$-th of these lines contains the elements of matrix $$$a_{i, 1}, a_{i, 2}, ldots, a_{i, m}$$$. It is guaranteed that all numbers in matrix $$$a$$$ are distinct and $$$1 leq a_{i, j} leq n cdot m$$$. It is guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$5 cdot 10^4$$$. Output For each test case, output $$$n cdot m$$$ integersxa0— any suitable matrix $$$b$$$, or $$$-1$$$ if such a matrix does not exist. Example Input 5 1 1 1 2 1 2 1 1 5 2 4 5 3 1 2 4 1 2 3 4 5 6 7 8 3 3 4 2 1 9 8 3 6 7 5 Output -1 1 2 4 5 3 1 2 6 7 8 5 2 3 4 1 8 3 9 7 5 6 2 1 4 Note In the first test case, there is only one element in the matrix, so matrix $$$b$$$ is the only matrix and it does not fit. In the second test case $$$a_{1, 1} = 2 eq 1 = b_{1, 1}$$$, $$$a_{2, 1} = 1 eq 2 = b_{2, 1}$$$. | 800 | false | true | true | false | false | true | false | false | false | false | 305 |
1650E | Now Dmitry has a session, and he has to pass $$$n$$$ exams. The session starts on day $$$1$$$ and lasts $$$d$$$ days. The $$$i$$$th exam will take place on the day of $$$a_i$$$ ($$$1 le a_i le d$$$), all $$$a_i$$$xa0— are different. Sample, where $$$n=3$$$, $$$d=12$$$, $$$a=[3,5,9]$$$. Orangexa0— exam days. Before the first exam Dmitry will rest $$$2$$$ days, before the second he will rest $$$1$$$ day and before the third he will rest $$$3$$$ days. For the session schedule, Dmitry considers a special value $$$mu$$$ — the smallest of the rest times before the exam for all exams. For example, for the image above, $$$mu=1$$$. In other words, for the schedule, he counts exactly $$$n$$$ numbers xa0— how many days he rests between the exam $$$i-1$$$ and $$$i$$$ (for $$$i=0$$$ between the start of the session and the exam $$$i$$$). Then it finds $$$mu$$$xa0— the minimum among these $$$n$$$ numbers. Dmitry believes that he can improve the schedule of the session. He may ask to change the date of one exam (change one arbitrary value of $$$a_i$$$). Help him change the date so that all $$$a_i$$$ remain different, and the value of $$$mu$$$ is as large as possible. For example, for the schedule above, it is most advantageous for Dmitry to move the second exam to the very end of the session. The new schedule will take the form: Now the rest periods before exams are equal to $$$[2,2,5]$$$. So, $$$mu=2$$$. Dmitry can leave the proposed schedule unchanged (if there is no way to move one exam so that it will lead to an improvement in the situation). Input The first line of input data contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of input test cases. The descriptions of test cases follow. An empty line is written in the test before each case. The first line of each test case contains two integers $$$n$$$ and $$$d$$$ ($$$2 le n le 2 cdot 10^5, 1 le d le 10^9$$$)xa0— the number of exams and the length of the session, respectively. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 le a_i le d, a_i < a_{i+1}$$$), where the $$$i$$$-th number means the date of the $$$i$$$-th exam. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the maximum possible value of $$$mu$$$ if Dmitry can move any one exam to an arbitrary day. All values of $$$a_i$$$ should remain distinct. Example Input 9 3 12 3 5 9 2 5 1 5 2 100 1 2 5 15 3 6 9 12 15 3 1000000000 1 400000000 500000000 2 10 3 4 2 2 1 2 4 15 6 11 12 13 2 20 17 20 Output 2 1 1 2 99999999 3 0 1 9 Note The first sample is parsed in statement. One of the optimal schedule changes for the second sample: Initial schedule. New schedule. In the third sample, we need to move the exam from day $$$1$$$ to any day from $$$4$$$ to $$$100$$$. In the fourth sample, any change in the schedule will only reduce $$$mu$$$, so the schedule should be left as it is. In the fifth sample, we need to move the exam from day $$$1$$$ to any day from $$$100000000$$$ to $$$300000000$$$. One of the optimal schedule changes for the sixth sample: Initial schedule. New schedule. In the seventh sample, every day is exam day, and it is impossible to rearrange the schedule. | 1,900 | true | true | true | false | true | false | false | true | true | false | 2,371 |
1381A2 | This is the hard version of the problem. The difference between the versions is the constraint on $$$n$$$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$ (a binary string is a string consisting of symbols $$$0$$$ and $$$1$$$). In an operation, you select a prefix of $$$a$$$, and simultaneously invert the bits in the prefix ($$$0$$$ changes to $$$1$$$ and $$$1$$$ changes to $$$0$$$) and reverse the order of the bits in the prefix. For example, if $$$a=001011$$$ and you select the prefix of length $$$3$$$, it becomes $$$011011$$$. Then if you select the entire string, it becomes $$$001001$$$. Your task is to transform the string $$$a$$$ into $$$b$$$ in at most $$$2n$$$ operations. It can be proved that it is always possible. Input The first line contains a single integer $$$t$$$ ($$$1le tle 1000$$$) xa0— the number of test cases. Next $$$3t$$$ lines contain descriptions of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 10^5$$$) xa0— the length of the binary strings. The next two lines contain two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^5$$$. Output For each test case, output an integer $$$k$$$ ($$$0le kle 2n$$$), followed by $$$k$$$ integers $$$p_1,ldots,p_k$$$ ($$$1le p_ile n$$$). Here $$$k$$$ is the number of operations you use and $$$p_i$$$ is the length of the prefix you flip in the $$$i$$$-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have $$$01 o 11 o 00 o 10$$$. In the second test case, we have $$$01011 o 00101 o 11101 o 01000 o 10100 o 00100 o 11100$$$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $$$2$$$, which will leave $$$a$$$ unchanged. | 1,700 | false | false | true | false | true | true | false | false | false | false | 3,763 |
1428C | Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated. For example, Zookeeper can use two such operations: AABABBA $$$ o$$$ AABBA $$$ o$$$ AAA. Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string? Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 leq t leq 20000)$$$ xa0— the number of test cases. The description of the test cases follows. Each of the next $$$t$$$ lines contains a single test case each, consisting of a non-empty string $$$s$$$: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of $$$s$$$ are either 'A' or 'B'. It is guaranteed that the sum of $$$s$$$ (length of $$$s$$$) among all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integer: the length of the shortest string that Zookeeper can make. Example Input 3 AAA BABA AABBBABBBB Note For the first test case, you can't make any moves, so the answer is $$$3$$$. For the second test case, one optimal sequence of moves is BABA $$$ o$$$ BA. So, the answer is $$$2$$$. For the third test case, one optimal sequence of moves is AABBBABBBB $$$ o$$$ AABBBABB $$$ o$$$ AABBBB $$$ o$$$ ABBB $$$ o$$$ AB $$$ o$$$ (empty string). So, the answer is $$$0$$$. | 1,100 | false | true | false | false | true | false | true | false | false | false | 3,523 |
1674E | Monocarp plays "Rage of Empires II: Definitive Edition" — a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall. The wall consists of $$$n$$$ sections, aligned in a row. The $$$i$$$-th section initially has durability $$$a_i$$$. If durability of some section becomes $$$0$$$ or less, this section is considered broken. To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager — a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $$$2$$$ damage to the target section and $$$1$$$ damage to adjacent sections. In other words, if the onager shoots at the section $$$x$$$, then the durability of the section $$$x$$$ decreases by $$$2$$$, and the durability of the sections $$$x - 1$$$ and $$$x + 1$$$ (if they exist) decreases by $$$1$$$ each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections. Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him! Input The first line contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the number of sections. The second line contains the sequence of integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$), where $$$a_i$$$ is the initial durability of the $$$i$$$-th section. Output Print one integer — the minimum number of onager shots needed to break at least two sections of the wall. Note In the first example, it is possible to break the $$$2$$$-nd and the $$$4$$$-th section in $$$10$$$ shots, for example, by shooting the third section $$$10$$$ times. After that, the durabilities become $$$[20, 0, 10, 0, 20]$$$. Another way of doing it is firing $$$5$$$ shots at the $$$2$$$-nd section, and another $$$5$$$ shots at the $$$4$$$-th section. After that, the durabilities become $$$[15, 0, 20, 0, 15]$$$. In the second example, it is enough to shoot the $$$2$$$-nd section once. Then the $$$1$$$-st and the $$$3$$$-rd section will be broken. In the third example, it is enough to shoot the $$$2$$$-nd section twice (then the durabilities become $$$[5, 2, 4, 8, 5, 8]$$$), and then shoot the $$$3$$$-rd section twice (then the durabilities become $$$[5, 0, 0, 6, 5, 8]$$$). So, four shots are enough to break the $$$2$$$-nd and the $$$3$$$-rd section. | 2,000 | true | true | false | false | false | true | true | true | false | false | 2,225 |
1054C | There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 leq a_i leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$xa0— the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively. Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 leq j < i$$$), such that $$$a_i < a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i < j leq n$$$), such that $$$a_i < a_j$$$. Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it. Input On the first line there is a single integer $$$n$$$ ($$$1 leq n leq 1000$$$)xa0— the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, ldots, l_n$$$ ($$$0 leq l_i leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, ldots, r_n$$$ ($$$0 leq r_i leq n$$$), separated by spaces. Output If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$, separated by spacesxa0— the numbers of candies the children $$$1, 2, ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 leq a_i leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them. Note In the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ childxa0— $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him. In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$. In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy. | 1,500 | false | false | true | false | false | true | false | false | false | false | 5,445 |
368B | Problem - 368B - 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 data structures dp *1100 No tag edit access → Contest materials . For each number _l__i_ he wants to know how many distinct numbers are staying on the positions _l__i_, _l__i_u2009+u20091, ..., _n_. Formally, he want to find the number of distinct numbers among _a__l__i_,u2009_a__l__i_u2009+u20091,u2009...,u2009_a__n_.? Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each _l__i_. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105). The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009105) — the array elements. Next _m_ lines contain integers _l_1,u2009_l_2,u2009...,u2009_l__m_. The _i_-th line contains integer _l__i_ (1u2009≤u2009_l__i_u2009≤u2009_n_). Output Print _m_ lines — on the _i_-th line print the answer to the number _l__i_. Examples Input 10 10 1 2 3 4 1 2 3 4 100000 99999 1 2 3 4 5 6 7 8 9 10 Output 6 6 6 6 6 5 4 3 2 1 | 1,100 | false | false | false | true | true | false | false | false | false | false | 8,359 |
1958A | In Berland, coins of worth $$$1$$$, $$$3$$$ and $$$5$$$ burles are commonly used (burles are local currency). Eva has to pay exactly $$$n$$$ burles in a shop. She has an infinite amount of coins of all three types. However, she doesn't like to pay using coins worth $$$1$$$ burle — she thinks they are the most convenient to use. Help Eva to calculate the minimum number of coins worth $$$1$$$ burle she has to use, if she has to pay exactly $$$n$$$ burles. Note that she can spend any number of coins worth $$$3$$$ and/or $$$5$$$ burles. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. Each test case consists of one line, containing one integer $$$n$$$ ($$$1 le n le 100$$$). Output For each test case, print one integer — the minimum number of $$$1$$$-burle coins Eva has to use. Note In the first test case, Eva should use $$$1$$$ coin worth $$$1$$$ burle, and $$$2$$$ coins worth $$$3$$$ burles. In the second test case, Eva should use $$$1$$$ coin worth $$$3$$$ burles and $$$1$$$ coin worth $$$5$$$ burles. In the third test case, Eva should use $$$14$$$ coins worth $$$3$$$ burles. In the fourth test case, Eva should use $$$2$$$ coins worth $$$1$$$ burle. In the fifth test case, Eva should use $$$2$$$ coins worth $$$3$$$ burles and $$$1$$$ coin worth $$$5$$$ burles. | 1,100 | false | false | false | true | false | false | false | false | false | false | 526 |
323B | Problem - 323B - Codeforces =============== xa0 (_v_u2009≠u2009_u_) there exists a path from vertex _v_ to vertex _u_ consisting of no more then two edges. A directed graph without self-loops is a tournament, if there is exactly one edge between any two distinct vertices (in one out of two possible directions). Input The first line contains an integer _n_ (3u2009≤u2009_n_u2009≤u20091000), the number of the graph's vertices. Output Print -1 if there is no graph, satisfying the described conditions. Otherwise, print _n_ lines with _n_ integers in each. The numbers should be separated with spaces. That is adjacency matrix _a_ of the found tournament. Consider the graph vertices to be numbered with integers from 1 to _n_. Then _a__v_,u2009_u_u2009=u20090, if there is no edge from _v_ to _u_, and _a__v_,u2009_u_u2009=u20091 if there is one. As the output graph has to be a tournament, following equalities must be satisfied: _a__v_,u2009_u_u2009+u2009_a__u_,u2009_v_u2009=u20091 for each _v_,u2009_u_ (1u2009≤u2009_v_,u2009_u_u2009≤u2009_n_;xa0_v_u2009≠u2009_u_); _a__v_,u2009_v_u2009=u20090 for each _v_ (1u2009≤u2009_v_u2009≤u2009_n_). Examples Input 3 Output 0 1 0 0 0 1 1 0 0 Input 4 Output -1 | 2,200 | false | false | false | false | false | true | false | false | false | true | 8,543 |
858B | In a building where Polycarp lives there are equal number of flats on each floor. Unfortunately, Polycarp don't remember how many flats are on each floor, but he remembers that the flats are numbered from 1 from lower to upper floors. That is, the first several flats are on the first floor, the next several flats are on the second and so on. Polycarp don't remember the total number of flats in the building, so you can consider the building to be infinitely high (i.e. there are infinitely many floors). Note that the floors are numbered from 1. Polycarp remembers on which floors several flats are located. It is guaranteed that this information is not self-contradictory. It means that there exists a building with equal number of flats on each floor so that the flats from Polycarp's memory have the floors Polycarp remembers. Given this information, is it possible to restore the exact floor for flat _n_? Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009100, 0u2009≤u2009_m_u2009≤u2009100), where _n_ is the number of the flat you need to restore floor for, and _m_ is the number of flats in Polycarp's memory. _m_ lines follow, describing the Polycarp's memory: each of these lines contains a pair of integers _k__i_,u2009_f__i_ (1u2009≤u2009_k__i_u2009≤u2009100, 1u2009≤u2009_f__i_u2009≤u2009100), which means that the flat _k__i_ is on the _f__i_-th floor. All values _k__i_ are distinct. It is guaranteed that the given information is not self-contradictory. Output Print the number of the floor in which the _n_-th flat is located, if it is possible to determine it in a unique way. Print -1 if it is not possible to uniquely restore this floor. Note In the first example the 6-th flat is on the 2-nd floor, while the 7-th flat is on the 3-rd, so, the 6-th flat is the last on its floor and there are 3 flats on each floor. Thus, the 10-th flat is on the 4-th floor. In the second example there can be 3 or 4 flats on each floor, so we can't restore the floor for the 8-th flat. | 1,500 | false | false | true | false | false | false | true | false | false | false | 6,313 |
528D | Leonid works for a small and promising start-up that works on decoding the human genome. His duties include solving complex problems of finding certain patterns in long strings consisting of letters 'A', 'T', 'G' and 'C'. Let's consider the following scenario. There is a fragment of a human DNA chain, recorded as a string _S_. To analyze the fragment, you need to find all occurrences of string _T_ in a string _S_. However, the matter is complicated by the fact that the original chain fragment could contain minor mutations, which, however, complicate the task of finding a fragment. Leonid proposed the following approach to solve this problem. Let's write down integer _k_u2009≥u20090 — the error threshold. We will say that string _T_ occurs in string _S_ on position _i_ (1u2009≤u2009_i_u2009≤u2009_S_u2009-u2009_T_u2009+u20091), if after putting string _T_ along with this position, each character of string _T_ corresponds to the some character of the same value in string _S_ at the distance of at most _k_. More formally, for any _j_ (1u2009≤u2009_j_u2009≤u2009_T_) there must exist such _p_ (1u2009≤u2009_p_u2009≤u2009_S_), that (_i_u2009+u2009_j_u2009-u20091)u2009-u2009_p_u2009≤u2009_k_ and _S_[_p_]u2009=u2009_T_[_j_]. For example, corresponding to the given definition, string "ACAT" occurs in string "AGCAATTCAT" in positions 2, 3 and 6. Note that at _k_u2009=u20090 the given definition transforms to a simple definition of the occurrence of a string in a string. Help Leonid by calculating in how many positions the given string _T_ occurs in the given string _S_ with the given error threshold. Input The first line contains three integers _S_,u2009_T_,u2009_k_ (1u2009≤u2009_T_u2009≤u2009_S_u2009≤u2009200u2009000, 0u2009≤u2009_k_u2009≤u2009200u2009000) — the lengths of strings _S_ and _T_ and the error threshold. The second line contains string _S_. The third line contains string _T_. Both strings consist only of uppercase letters 'A', 'T', 'G' and 'C'. Note If you happen to know about the structure of the human genome a little more than the author of the problem, and you are not impressed with Leonid's original approach, do not take everything described above seriously. | 2,500 | false | false | false | false | false | false | true | false | false | false | 7,719 |
1775A1 | This is an easy version of the problem. The difference between the versions is that the string can be longer than in the easy version. You can only do hacks if both versions of the problem are passed. Kazimir Kazimirovich is a Martian gardener. He has a huge orchard of binary balanced apple trees. Recently Casimir decided to get himself three capybaras. The gardener even came up with their names and wrote them down on a piece of paper. The name of each capybara is a non-empty line consisting of letters "a" and "b". Denote the names of the capybaras by the lines $$$a$$$, $$$b$$$, and $$$c$$$. Then Casimir wrote the nonempty lines $$$a$$$, $$$b$$$, and $$$c$$$ in a row without spaces. For example, if the capybara's name was "aba", "ab", and "bb", then the string the gardener wrote down would look like "abaabbb". The gardener remembered an interesting property: either the string $$$b$$$ is lexicographically not smaller than the strings $$$a$$$ and $$$c$$$ at the same time, or the string $$$b$$$ is lexicographically not greater than the strings $$$a$$$ and $$$c$$$ at the same time. In other words, either $$$a le b$$$ and $$$c le b$$$ are satisfied, or $$$b le a$$$ and $$$b le c$$$ are satisfied (or possibly both conditions simultaneously). Here $$$le$$$ denotes the lexicographic "less than or equal to" for strings. Thus, $$$a le b$$$ means that the strings must either be equal, or the string $$$a$$$ must stand earlier in the dictionary than the string $$$b$$$. For a more detailed explanation of this operation, see "Notes" section. Today the gardener looked at his notes and realized that he cannot recover the names because they are written without spaces. He is no longer sure if he can recover the original strings $$$a$$$, $$$b$$$, and $$$c$$$, so he wants to find any triplet of names that satisfy the above property. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. The only line of a test case contains the string $$$s$$$ ($$$3 le s le 100$$$)xa0— the names of the capybaras, written together. The string consists of English letters 'a' and 'b' only. It is guaranteed that the sum of string lengths over all test cases does not exceed $$$500$$$. Output For each test case, print three strings $$$a$$$, $$$b$$$ and $$$c$$$ on a single line, separated by spacesxa0— names of capybaras, such that writing them without spaces results in a line $$$s$$$. Either $$$a le b$$$ and $$$c le b$$$, or $$$b le a$$$ and $$$b le c$$$ must be satisfied. If there are several ways to restore the names, print any of them. If the names cannot be recovered, print ":(" (without quotes). Example Output b bb a a b a a a a ab b a a bb b Note A string $$$x$$$ is lexicographically smaller than a string $$$y$$$ if and only if one of the following holds: $$$x$$$ is a prefix of $$$y$$$, but $$$x e y$$$; in the first position where $$$x$$$ and $$$y$$$ differ, the string $$$x$$$ has the letter 'a', and the string $$$y$$$ has the letter 'b'. Now let's move on to the examples. In the first test case, one of the possible ways to split the line $$$s$$$ into three linesxa0— "b", "bb", "a". In the third test case, we can see that the split satisfies two conditions at once (i.xa0e., $$$a le b$$$, $$$c le b$$$, $$$b le a$$$, and $$$b le c$$$ are true simultaneously). | 800 | false | false | true | false | false | true | true | false | false | false | 1,635 |
1056A | Arkady's morning seemed to be straight of his nightmare. He overslept through the whole morning and, still half-asleep, got into the tram that arrived the first. Some time after, leaving the tram, he realized that he was not sure about the line number of the tram he was in. During his ride, Arkady woke up several times and each time he saw the tram stopping at some stop. For each stop he knows which lines of tram stop there. Given this information, can you help Arkady determine what are the possible lines of the tram he was in? Input The first line contains a single integer $$$n$$$ ($$$2 le n le 100$$$)xa0— the number of stops Arkady saw. The next $$$n$$$ lines describe the stops. Each of them starts with a single integer $$$r$$$ ($$$1 le r le 100$$$)xa0— the number of tram lines that stop there. $$$r$$$ distinct integers follow, each one between $$$1$$$ and $$$100$$$, inclusive,xa0— the line numbers. They can be in arbitrary order. It is guaranteed that Arkady's information is consistent, i.e. there is at least one tram line that Arkady could take. Output Print all tram lines that Arkady could be in, in arbitrary order. Examples Input 3 3 1 4 6 2 1 4 5 10 5 6 4 1 Input 5 1 1 10 10 9 8 7 100 5 4 3 99 1 5 1 2 3 4 5 5 4 1 3 2 5 4 10 1 5 3 Note Consider the first example. Arkady woke up three times. The first time he saw a stop with lines $$$1$$$, $$$4$$$, $$$6$$$. The second time he saw a stop with lines $$$1$$$, $$$4$$$. The third time he saw a stop with lines $$$10$$$, $$$5$$$, $$$6$$$, $$$4$$$ and $$$1$$$. He can be in a tram of one of two lines: $$$1$$$ or $$$4$$$. | 800 | false | false | true | false | false | false | false | false | false | false | 5,432 |
1833C | Vlad was given an array $$$a$$$ of $$$n$$$ positive integers. Now he wants to build a beautiful array $$$b$$$ of length $$$n$$$ from it. Vlad considers an array beautiful if all the numbers in it are positive and have the same parity. That is, all numbers in the beautiful array are greater than zero and are either all even or all odd. To build the array $$$b$$$, Vlad can assign each $$$b_i$$$ either the value $$$a_i$$$ or $$$a_i - a_j$$$, where any $$$j$$$ from $$$1$$$ to $$$n$$$ can be chosen. To avoid trying to do the impossible, Vlad asks you to determine whether it is possible to build a beautiful array $$$b$$$ of length $$$n$$$ using his array $$$a$$$. Input The first line of input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then follow the descriptions of the test cases. The first line of each case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of the array $$$a$$$. The second line of each case contains $$$n$$$ positive integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all cases does not exceed $$$2 cdot 10^5$$$. Output Output $$$t$$$ strings, each of which is the answer to the corresponding test case. As the answer, output "YES" if Vlad can build a beautiful array $$$b$$$, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 7 5 2 6 8 4 3 5 1 4 7 6 9 4 2 6 4 10 7 5 29 13 9 10000001 11 3 5 2 1 2 4 2 5 2 4 5 4 3 4 2 5 5 4 Output NO YES YES YES YES NO NO | 800 | true | true | false | false | false | false | false | false | false | false | 1,289 |
355A | Vasya has recently found out what a digital root of a number is and he decided to share his knowledge with you. Let's assume that _S_(_n_) is the sum of digits of number _n_, for example, _S_(4098)u2009=u20094u2009+u20090u2009+u20099u2009+u20098u2009=u200921. Then the digital root of number _n_ equals to: 1. _dr_(_n_)u2009=u2009_S_(_n_), if _S_(_n_)u2009<u200910; 2. _dr_(_n_)u2009=u2009_dr_(u2009_S_(_n_)u2009), if _S_(_n_)u2009≥u200910. For example, _dr_(4098)u2009u2009=u2009u2009_dr_(21)u2009u2009=u2009u20093. Vasya is afraid of large numbers, so the numbers he works with are at most 101000. For all such numbers, he has proved that _dr_(_n_)u2009u2009=u2009u2009_S_(u2009_S_(u2009_S_(u2009_S_(_n_)u2009)u2009)u2009) (_n_u2009≤u2009101000). Now Vasya wants to quickly find numbers with the given digital root. The problem is, he hasn't learned how to do that and he asked you to help him. You task is, given numbers _k_ and _d_, find the number consisting of exactly _k_ digits (the leading zeroes are not allowed), with digital root equal to _d_, or else state that such number does not exist. Input The first line contains two integers _k_ and _d_ (1u2009≤u2009_k_u2009≤u20091000;u20020u2009≤u2009_d_u2009≤u20099). Output In a single line print either any number that meets the requirements (without the leading zeroes) or "No solution" (without the quotes), if the corresponding number does not exist. The chosen number must consist of exactly _k_ digits. We assume that number 0 doesn't contain any leading zeroes. Note For the first test sample _dr_(5881)u2009u2009=u2009u2009_dr_(22)u2009u2009=u2009u20094. For the second test sample _dr_(36172)u2009u2009=u2009u2009_dr_(19)u2009u2009=u2009u2009_dr_(10)u2009u2009=u2009u20091. | 1,100 | false | false | true | false | false | true | false | false | false | false | 8,413 |
196C | You are given a tree with _n_ vertexes and _n_ points on a plane, no three points lie on one straight line. Your task is to paint the given tree on a plane, using the given points as vertexes. That is, you should correspond each vertex of the tree to exactly one point and each point should correspond to a vertex. If two vertexes of the tree are connected by an edge, then the corresponding points should have a segment painted between them. The segments that correspond to non-adjacent edges, should not have common points. The segments that correspond to adjacent edges should have exactly one common point. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u20091500) — the number of vertexes on a tree (as well as the number of chosen points on the plane). Each of the next _n_u2009-u20091 lines contains two space-separated integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, _u__i_u2009≠u2009_v__i_) — the numbers of tree vertexes connected by the _i_-th edge. Each of the next _n_ lines contain two space-separated integers _x__i_ and _y__i_ (u2009-u2009109u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009109) — the coordinates of the _i_-th point on the plane. No three points lie on one straight line. It is guaranteed that under given constraints problem has a solution. Output Print _n_ distinct space-separated integers from 1 to _n_: the _i_-th number must equal the number of the vertex to place at the _i_-th point (the points are numbered in the order, in which they are listed in the input). If there are several solutions, print any of them. Examples Input 4 1 2 2 3 1 4 -1 -2 3 5 -3 3 2 0 Note The possible solutions for the sample are given below. | 2,200 | false | false | false | false | false | true | false | false | true | false | 9,056 |
1926F | Vladislav has a grid of size $$$7 imes 7$$$, where each cell is colored black or white. In one operation, he can choose any cell and change its color (black $$$leftrightarrow$$$ white). Find the minimum number of operations required to ensure that there are no black cells with four diagonal neighbors also being black. The left image shows that initially there are two black cells violating the condition. By flipping one cell, the grid will work. Input The first line of input contains a single integer $$$t$$$ ($$$1 leq t leq 200$$$)xa0— the number of test cases. Then follows the description of the test cases. Each test case consists of $$$7$$$ lines, each containing $$$7$$$ characters. Each of these characters is either $$$ exttt{W}$$$ or $$$ exttt{B}$$$, denoting a white or black cell, respectively. Output For each test case, output a single integerxa0— the minimum number of operations required to ensure that there are no black cells with all four diagonal neighbors also being black. Example Input 4 WWWWWWW WWWWBBB WWWWWBW WWBBBBB WWWBWWW WWBBBWW WWWWWWW WWWWWWW WWWWWWW WBBBBBW WBBBBBW WBBBBBW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WWWWWWW WBBBBBW BBBBBBB BBBBBBB WWWWWWW BBBBBBB BBBBBBB BBBBBBB Note The first test case is illustrated in the statement. The second test case is illustrated below: In the third test case, the grid already satisfies the condition. | 2,200 | false | false | true | true | false | false | true | false | false | false | 732 |
685A | Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in _n_ hours, and each hour in _m_ minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to _n_u2009-u20091, while the second has the smallest possible number of places that is necessary to display any integer from 0 to _m_u2009-u20091. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. Input The first line of the input contains two integers, given in the decimal notation, _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009109)xa0— the number of hours in one day and the number of minutes in one hour, respectively. Output Print one integer in decimal notationxa0— the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. Note In the first sample, possible pairs are: (0:u20091), (0:u20092), (1:u20090), (1:u20092). In the second sample, possible pairs are: (02:u20091), (03:u20091), (04:u20091), (05:u20091), (06:u20091). | 1,700 | true | false | false | true | false | false | true | false | false | false | 7,083 |
984B | One day Alex decided to remember childhood when computers were not too powerful and lots of people played only default games. Alex enjoyed playing Minesweeper that time. He imagined that he saved world from bombs planted by terrorists, but he rarely won. Alex has grown up since then, so he easily wins the most difficult levels. This quickly bored him, and he thought: what if the computer gave him invalid fields in the childhood and Alex could not win because of it? He needs your help to check it. A Minesweeper field is a rectangle $$$n imes m$$$, where each cell is either empty, or contains a digit from $$$1$$$ to $$$8$$$, or a bomb. The field is valid if for each cell: if there is a digit $$$k$$$ in the cell, then exactly $$$k$$$ neighboring cells have bombs. if the cell is empty, then all neighboring cells have no bombs. Two cells are neighbors if they have a common side or a corner (i.xa0e. a cell has at most $$$8$$$ neighboring cells). Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 100$$$) — the sizes of the field. The next $$$n$$$ lines contain the description of the field. Each line contains $$$m$$$ characters, each of them is "." (if this cell is empty), "*" (if there is bomb in this cell), or a digit from $$$1$$$ to $$$8$$$, inclusive. Output Print "YES", if the field is valid and "NO" otherwise. You can choose the case (lower or upper) for each letter arbitrarily. Note In the second example the answer is "NO" because, if the positions of the bombs are preserved, the first line of the field should be *2*1. You can read more about Minesweeper in [Wikipedia's article]( | 1,100 | false | false | true | false | false | false | false | false | false | false | 5,780 |
484B | , where 1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_ and _a__i_u2009≥u2009_a__j_. Input The first line contains integer _n_xa0— the length of the sequence (1u2009≤u2009_n_u2009≤u20092·105). The second line contains _n_ space-separated integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009106). Output Print the answer to the problem. | 2,100 | true | false | false | false | false | false | false | true | true | false | 7,901 |
1491G | There are $$$n$$$ coins labeled from $$$1$$$ to $$$n$$$. Initially, coin $$$c_i$$$ is on position $$$i$$$ and is facing upwards (($$$c_1, c_2, dots, c_n)$$$ is a permutation of numbers from $$$1$$$ to $$$n$$$). You can do some operations on these coins. In one operation, you can do the following: Choose $$$2$$$ distinct indices $$$i$$$ and $$$j$$$. Then, swap the coins on positions $$$i$$$ and $$$j$$$. Then, flip both coins on positions $$$i$$$ and $$$j$$$. (If they are initially faced up, they will be faced down after the operation and vice versa) Construct a sequence of at most $$$n+1$$$ operations such that after performing all these operations the coin $$$i$$$ will be on position $$$i$$$ at the end, facing up. Note that you do not need to minimize the number of operations. Input The first line contains an integer $$$n$$$ ($$$3 leq n leq 2 cdot 10^5$$$) — the number of coins. The second line contains $$$n$$$ integers $$$c_1,c_2,dots,c_n$$$ ($$$1 le c_i le n$$$, $$$c_i eq c_j$$$ for $$$i eq j$$$). Output In the first line, output an integer $$$q$$$ $$$(0 leq q leq n+1)$$$ — the number of operations you used. In the following $$$q$$$ lines, output two integers $$$i$$$ and $$$j$$$ $$$(1 leq i, j leq n, i e j)$$$ — the positions you chose for the current operation. Note Let coin $$$i$$$ facing upwards be denoted as $$$i$$$ and coin $$$i$$$ facing downwards be denoted as $$$-i$$$. The series of moves performed in the first sample changes the coins as such: $$$[~~~2,~~~1,~~~3]$$$ $$$[-3,~~~1,-2]$$$ $$$[-3,~~~2,-1]$$$ $$$[~~~1,~~~2,~~~3]$$$ In the second sample, the coins are already in their correct positions so there is no need to swap. | 2,800 | true | false | false | false | false | true | false | false | false | true | 3,216 |
679A | This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden numberxa0— an integer from interval [2,u2009100]. Your task is to say if the hidden number is prime or composite. Integer _x_u2009>u20091 is called prime if it has exactly two distinct divisors, 1 and _x_. If integer _x_u2009>u20091 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2,u2009100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2,u2009100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a queryxa0— print an integer from interval [2,u2009100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden numberxa0— one integer from the interval [2,u2009100]. Of course, his/her solution won't be able to read the hidden number from the input. Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. 59 is a divisor of the hidden number. In the interval [2,u2009100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | 1,400 | true | false | false | false | false | true | false | false | false | false | 7,110 |
333C | Gerald has a friend, Pollard. Pollard is interested in lucky tickets (ticket is a sequence of digits). At first he thought that a ticket is lucky if between some its digits we can add arithmetic signs and brackets so that the result obtained by the arithmetic expression was number 100. But he quickly analyzed all such tickets and moved on to a more general question. Now he explores _k_-lucky tickets. Pollard sais that a ticket is _k_-lucky if we can add arithmetic operation signs between its digits to the left or right of them (i.e., "+", "-", "u2009×u2009") and brackets so as to obtain the correct arithmetic expression whose value would equal _k_. For example, ticket "224201016" is 1000-lucky as (u2009-u20092u2009-u2009(2u2009+u20094))u2009×u2009(2u2009+u20090)u2009+u20091016u2009=u20091000. Pollard was so carried away by the lucky tickets that he signed up for a seminar on lucky tickets and, as far as Gerald knows, Pollard will attend it daily at 7 pm in some famous institute and will commute to it in the same tram for _m_ days. In this tram tickets have eight digits. And Gerald wants to make a surprise for Pollard: each day Pollard will receive a tram _k_-lucky ticket. The conductor has already agreed to give Pollard certain tickets during all these _m_ days and he only wants Gerald to tell him what kind of tickets to give out. In this regard, help Gerald pick exactly _m_ distinct _k_-lucky tickets. Input The single line contains two integers _k_ and _m_ (0u2009≤u2009_k_u2009≤u2009104, 1u2009≤u2009_m_u2009≤u20093·105). Output Print _m_ lines. Each line must contain exactly 8 digits — the _k_-winning ticket. The tickets may begin with 0, all tickets must be distinct. If there are more than _m_ distinct _k_-lucky tickets, print any _m_ of them. It is guaranteed that at least _m_ distinct _k_-lucky tickets exist. The tickets can be printed in any order. Examples Output 00000000 00000001 00000002 Output 00000007 00000016 00000017 00000018 | 2,700 | false | false | false | false | false | true | true | false | false | false | 8,503 |
1787H | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN TypeDB Forces 2023 (Div. 1 + Div. 2, Rated, Prizes!) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search data structures dp geometry *3300 No tag edit access → Contest materials Announcement #1 (en) TypeDB Forces 2023 (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST H. Codeforces Scoreboard time limit per test3 seconds memory limit per test512 megabytes You are participating in a Codeforces Round with $$$n$$$ problems. You spend exactly one minute to solve each problem, the time it takes to submit a problem can be ignored. You can only solve at most one problem at any time. The contest starts at time $$$0$$$, so you can make your first submission at any time $$$t ge 1$$$ minutes. Whenever you submit a problem, it is always accepted. The scoring of the $$$i$$$-th problem can be represented by three integers $$$k_i$$$, $$$b_i$$$, and $$$a_i$$$. If you solve it at time $$$t$$$ minutes, you get $$$max(b_i - k_i cdot t,a_i)$$$ points. Your task is to choose an order to solve all these $$$n$$$ problems to get the maximum possible score. You can assume the contest is long enough to solve all problems. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of problems. The $$$n$$$ lines follow, the $$$i$$$-th of them contains three integers $$$k_i$$$, $$$b_i$$$, $$$a_i$$$ ($$$1le k_i,b_i,a_ile 10^9$$$; $$$a_i < b_i$$$), denoting that you get the score of $$$max(b_i - k_i cdot t,a_i)$$$ if you solve the $$$i$$$-th task at time $$$t$$$ minutes. It's guaranteed that the sum of $$$n$$$ does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a line containing a single integer — the maximum score you can get. Example input 4 4 10000 1000000000 2006 10000 1000000000 9999 2 999991010 1010 1000000000 1000000000 999999999 6 1 8 1 9 29 4 2 14 3 4 13 1 2 19 5 10 12 5 8 4 10 1 4 19 8 1 14 3 4 15 6 2 9 6 1 11 10 2 19 12 4 19 14 10 5 12 7 5 39 12 2 39 11 3 23 15 5 30 11 3 17 13 5 29 14 3 17 11 3 36 18 3 9 8 output 3999961003 53 78 180 Note In the second test case, the points for all problems at each minute are listed below. Time $$$1$$$ $$$2$$$ $$$3$$$ $$$4$$$ $$$5$$$ $$$6$$$ Problem $$$1$$$ $$$7$$$ $$$6$$$ $$$5$$$ $$$color{red}{4}$$$ $$$3$$$ $$$2$$$ Problem $$$2$$$ $$$color{red}{20}$$$ $$$11$$$ $$$4$$$ $$$4$$$ $$$4$$$ $$$4$$$ Problem $$$3$$$ $$$12$$$ $$$10$$$ $$$color{red}{8}$$$ $$$6$$$ $$$4$$$ $$$3$$$ Problem $$$4$$$ $$$9$$$ $$$5$$$ $$$1$$$ $$$1$$$ $$$color{red}{1}$$$ $$$1$$$ Problem $$$5$$$ $$$17$$$ $$$color{red}{15}$$$ $$$13$$$ $$$11$$$ $$$9$$$ $$$7$$$ Problem $$$6$$$ $$$5$$$ $$$5$$$ $$$5$$$ $$$5$$$ $$$5$$$ $$$color{red}{5}$$$ The points displayed in red denote one of the optimal orders with the score $$$53$$$. Codeforces (c) | 3,300 | false | false | false | true | true | false | false | true | false | false | 1,556 |
269E | Emuskald is an innovative musician and always tries to push the boundaries of music production. Now he has come up with an idea for a revolutionary musical instrument — a rectangular harp. A rectangular harp is a rectangle _n_u2009×u2009_m_ consisting of _n_ rows and _m_ columns. The rows are numbered 1 to _n_ from top to bottom. Similarly the columns are numbered 1 to _m_ from left to right. String pins are spaced evenly across every side, one per unit. Thus there are _n_ pins on the left and right sides of the harp and _m_ pins on its top and bottom. The harp has exactly _n_u2009+u2009_m_ different strings, each string connecting two different pins, each on a different side of the harp. Emuskald has ordered his apprentice to construct the first ever rectangular harp. However, he didn't mention that no two strings can cross, otherwise it would be impossible to play the harp. Two strings cross if the segments connecting their pins intersect. To fix the harp, Emuskald can perform operations of two types: 1. pick two different columns and swap their pins on each side of the harp, not changing the pins that connect each string; 2. pick two different rows and swap their pins on each side of the harp, not changing the pins that connect each string; In the following example, he can fix the harp by swapping two columns: Help Emuskald complete his creation and find the permutations how the rows and columns of the harp need to be rearranged, or tell that it is impossible to do so. He can detach and reattach each string to its pins, so the physical layout of the strings doesn't matter. Input The first line of input contains two space-separated integers numbers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105), the height and width of the harp in units. Each of the following _n_u2009+u2009_m_ lines contains 4 space-separated tokens, describing a single string: two symbols _a__i_, _b__i_ and two integer numbers _p__i_, _q__i_. The pair _a__i_, _p__i_ describes the first pin, and the pair _b__i_, _q__i_ describes the second pin of the string; A pair _s_, _x_ describes the position of a single pin in a following way: 1. _s_ is equal to one of the symbols "L", "T", "R" or "B" (without quotes), which means that the pin is positioned on the left, top, right or bottom side of the harp accordingly; 2. _x_ is equal to the number of the row, if the pin is on the left or right border of the harp, and to the number of the column, if the pin is on the top or bottom border of the harp. It is guaranteed that no two different strings are connected to the same pin. Output If it is possible to rearrange the rows and columns to fix the harp, on the first line output _n_ space-separated integers — the old numbers of rows now placed from top to bottom in the fixed harp. On the second line, output _m_ space-separated integers — the old numbers of columns now placed from left to right in the fixed harp. If it is impossible to rearrange the rows and columns to fix the harp, output "No solution" (without quotes). Examples Input 3 4 L T 1 3 L B 2 2 L B 3 3 T R 1 2 T B 2 1 T R 4 1 B R 4 3 Input 3 3 L T 1 1 T R 3 1 R B 3 3 B L 1 3 L R 2 2 T B 2 2 | 3,100 | true | false | false | false | false | false | false | false | false | false | 8,754 |
293E | Problem - 293E - Codeforces =============== xa0 , such that vertices _v_ and _u_ are close. Input The first line contains three integers _n_, _l_ and _w_ (1u2009≤u2009_n_u2009≤u2009105,u20091u2009≤u2009_l_u2009≤u2009_n_,u20090u2009≤u2009_w_u2009≤u2009109). The next _n_u2009-u20091 lines contain the descriptions of the tree edges. The _i_-th line contains two integers _p__i_,u2009_w__i_ (1u2009≤u2009_p__i_u2009<u2009(_i_u2009+u20091),u20090u2009≤u2009_w__i_u2009≤u2009104), that mean that the _i_-th edge connects vertex (_i_u2009+u20091) and _p__i_ and has weight _w__i_. Consider the tree vertices indexed from 1 to _n_ in some way. Output Print a single integer — the number of close pairs. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 4 6 1 3 1 4 1 3 Output 4 Input 6 2 17 1 3 2 5 2 13 1 6 5 9 Output 9 | 2,700 | false | false | false | false | true | false | false | false | false | false | 8,657 |
1837A | You are given two integers $$$x$$$ and $$$k$$$. Grasshopper starts in a point $$$0$$$ on an OX axis. In one move, it can jump some integer distance, that is not divisible by $$$k$$$, to the left or to the right. What's the smallest number of moves it takes the grasshopper to reach point $$$x$$$? What are these moves? If there are multiple answers, print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. The only line of each testcase contains two integers $$$x$$$ and $$$k$$$ ($$$1 le x le 100$$$; $$$2 le k le 100$$$)xa0— the endpoint and the constraint on the jumps, respectively. Output For each testcase, in the first line, print a single integer $$$n$$$xa0— the smallest number of moves it takes the grasshopper to reach point $$$x$$$. In the second line, print $$$n$$$ integers, each of them not divisible by $$$k$$$. A positive integer would mean jumping to the right, a negative integer would mean jumping to the left. The endpoint after the jumps should be exactly $$$x$$$. Each jump distance should be from $$$-10^9$$$ to $$$10^9$$$. In can be shown that, for any solution with the smallest number of jumps, there exists a solution with the same number of jumps such that each jump is from $$$-10^9$$$ to $$$10^9$$$. It can be shown that the answer always exists under the given constraints. If there are multiple answers, print any of them. | 800 | true | false | false | false | false | true | false | false | false | false | 1,270 |
1475D | Polycarp often uses his smartphone. He has already installed $$$n$$$ applications on it. Application with number $$$i$$$ takes up $$$a_i$$$ units of memory. Polycarp wants to free at least $$$m$$$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring systemxa0— he assigned an integer $$$b_i$$$ to each application: $$$b_i = 1$$$xa0— regular application; $$$b_i = 2$$$xa0— important application. According to this rating system, his phone has $$$b_1 + b_2 + ldots + b_n$$$ convenience points. Polycarp believes that if he removes applications with numbers $$$i_1, i_2, ldots, i_k$$$, then he will free $$$a_{i_1} + a_{i_2} + ldots + a_{i_k}$$$ units of memory and lose $$$b_{i_1} + b_{i_2} + ldots + b_{i_k}$$$ convenience points. For example, if $$$n=5$$$, $$$m=7$$$, $$$a=[5, 3, 2, 1, 4]$$$, $$$b=[2, 1, 1, 2, 1]$$$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $$$1, 4$$$ and $$$5$$$. In this case, it will free $$$a_1+a_4+a_5=10$$$ units of memory and lose $$$b_1+b_4+b_5=5$$$ convenience points; applications with numbers $$$1$$$ and $$$3$$$. In this case, it will free $$$a_1+a_3=7$$$ units of memory and lose $$$b_1+b_3=3$$$ convenience points. applications with numbers $$$2$$$ and $$$5$$$. In this case, it will free $$$a_2+a_5=7$$$ memory units and lose $$$b_2+b_5=2$$$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $$$m$$$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le m le 10^9$$$)xa0— the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the number of memory units used by applications. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le 2$$$)xa0— the convenience points of each application. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $$$m$$$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. Example Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Note In the first test case, it is optimal to remove applications with numbers $$$2$$$ and $$$5$$$, freeing $$$7$$$ units of memory. $$$b_2+b_5=2$$$. In the second test case, by removing the only application, Polycarp will be able to clear only $$$2$$$ of memory units out of the $$$3$$$ needed. In the third test case, it is optimal to remove applications with numbers $$$1$$$, $$$2$$$, $$$3$$$ and $$$4$$$, freeing $$$10$$$ units of memory. $$$b_1+b_2+b_3+b_4=6$$$. In the fourth test case, it is optimal to remove applications with numbers $$$1$$$, $$$3$$$ and $$$4$$$, freeing $$$12$$$ units of memory. $$$b_1+b_3+b_4=4$$$. In the fifth test case, it is optimal to remove applications with numbers $$$1$$$ and $$$2$$$, freeing $$$5$$$ units of memory. $$$b_1+b_2=3$$$. | 1,800 | false | false | false | true | false | false | false | true | true | false | 3,301 |
1644C | You are given an array $$$a_1, a_2, dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$. Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$. Note that the subarray doesn't have to include all of the increased elements. Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 5000$$$)xa0— the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 5000$$$; $$$0 le x le 10^5$$$)xa0— the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^5 le a_i le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$. Output For each testcase, print $$$n + 1$$$ integersxa0— the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. Example Input 3 4 2 4 1 3 2 3 5 -2 -7 -1 10 2 -6 -1 -2 4 -6 -1 -4 4 -5 -4 Output 10 12 14 16 18 0 4 4 5 4 6 6 7 7 7 7 8 8 8 8 Note In the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k cdot x$$$ will be added to the sum. In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$. | 1,400 | false | true | true | true | false | false | true | false | false | false | 2,399 |
1975F | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 947 (Div. 1 + Div. 2) 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 bitmasks brute force combinatorics dfs and similar divide and conquer dp math *2600 No tag edit access → Contest materials Announcement (en) Tutorial #1 (en) Tutorial #2 (zh) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST F. Set time limit per test2 seconds memory limit per test512 megabytes Define the binary encoding of a finite set of natural numbers $$$T subseteq {0,1,2,ldots}$$$ as $$$f(T) = sumlimits_{i in T} 2^i$$$. For example, $$$f({0,2}) = 2^0 + 2^2 = 5$$$ and $$$f({}) = 0$$$. Notice that $$$f$$$ is a bijection from all such sets to all non-negative integers. As such, $$$f^{-1}$$$ is also defined. You are given an integer $$$n$$$ along with $$$2^n-1$$$ sets $$$V_1,V_2,ldots,V_{2^n-1}$$$. Find all sets $$$S$$$ that satisfy the following constraint: $$$S subseteq {0,1,ldots,n-1}$$$. Note that $$$S$$$ can be empty. For all non-empty subsets $$$T subseteq {0,1,ldots,n-1}$$$, $$$S cap T in V_{f(T)}$$$. Due to the large input and output, both input and output will be given in terms of binary encodings of the sets. Input The first line of input contains a single integer $$$n$$$ ($$$1 leq n leq 20$$$). The second line of input contains $$$2^n-1$$$ integers $$$v_1,v_2,ldots,v_{2^n-1}$$$ ($$$0 leq v_i < 2^{n+1}$$$)xa0— the sets $$$V_i$$$ given in their binary encoding where $$$V_i = f^{-1}(v_i)$$$. Output The first line of output should contain an integer $$$k$$$ indicating the number of possible $$$S$$$. In the following $$$k$$$ lines, you should output $$$f(S)$$$ for all possible $$$S$$$ in increasing order. Examples input 3 15 15 15 15 15 15 12 output 4 3 5 6 7 input 5 63 63 63 63 6 63 63 63 63 63 63 5 63 63 63 63 63 63 8 63 63 63 63 2 63 63 63 63 63 63 63 output 1 19 Note In the first test case, one possible $$$S$$$ is $$$f^{-1}(3) = {0,1}$$$. All the non-empty subsets $$$T subseteq {0,1,2}$$$ and the corresponding $$$S cap T$$$, $$$f(T)$$$ and $$$V_f(T)$$$ are as follows: $$$T$$$ $$$Scap T$$$ $$$f(T)$$$ $$$V_{f(T)}$$$ $$${0}$$$ $$$1$$$ $$$1$$$ $$${0,1,2,3}$$$ $$${1}$$$ $$$1$$$ $$$2$$$ $$${0,1,2,3}$$$ $$${2}$$$ $$$0$$$ $$$4$$$ $$${0,1,2,3}$$$ $$${0,1}$$$ $$$2$$$ $$$3$$$ $$${0,1,2,3}$$$ $$${0,2}$$$ $$$1$$$ $$$5$$$ $$${0,1,2,3}$$$ $$${1,2}$$$ $$$1$$$ $$$6$$$ $$${0,1,2,3}$$$ $$${0,1,2}$$$ $$$2$$$ $$$7$$$ $$${2,3}$$$ Codeforces (c) | 2,600 | true | false | false | true | false | false | true | false | false | false | 437 |
1957D | You are given an array $$$a_1, a_2, ldots, a_n$$$. Find the number of tuples ($$$x, y, z$$$) such that: $$$1 leq x leq y leq z leq n$$$, and $$$f(x, y) oplus f(y, z) > f(x, z)$$$. We define $$$f(l, r) = a_l oplus a_{l + 1} oplus ldots oplus a_{r}$$$, where $$$oplus$$$ denotes the xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output a single integer on a new linexa0— the number of described tuples. Note In the first case, there are 4 such tuples in the array $$$[6, 2, 4]$$$: ($$$1$$$, $$$2$$$, $$$2$$$): $$$(a_1 oplus a_2) oplus (a_2) = 4 oplus 2 > (a_1 oplus a_2) = 4$$$ ($$$1$$$, $$$1$$$, $$$3$$$): $$$(a_1) oplus (a_1 oplus a_2 oplus a_3) = 6 oplus 0 > (a_1 oplus a_2 oplus a_3) = 0$$$ ($$$1$$$, $$$2$$$, $$$3$$$): $$$(a_1 oplus a_2) oplus (a_2 oplus a_3) = 4 oplus 6 > (a_1 oplus a_2 oplus a_3) = 0$$$ ($$$1$$$, $$$3$$$, $$$3$$$): $$$(a_1 oplus a_2 oplus a_3) oplus (a_3) = 0 oplus 4 > (a_1 oplus a_2 oplus a_3) = 0$$$ In the second test case, there are no such tuples. | 1,900 | true | false | false | true | false | false | true | false | false | false | 530 |
651B | Problem - 651B - 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 sortings *1200 No tag edit access , such that _a__i_u2009+u20091u2009>u2009_a__i_. Input The first line of the input contains integer _n_ (1u2009≤u2009_n_u2009≤u20091000)xa0— the number of painting. The second line contains the sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20091000), where _a__i_ means the beauty of the _i_-th painting. Output Print one integerxa0— the maximum possible number of neighbouring pairs, such that _a__i_u2009+u20091u2009>u2009_a__i_, after the optimal rearrangement. Examples Input 5 20 30 10 50 40 Output 4 Input 4 200 100 100 200 Output 2 Note In the first sample, the optimal order is: 10,u200920,u200930,u200940,u200950. In the second sample, the optimal order is: 100,u2009200,u2009100,u2009200. | 1,200 | false | true | false | false | false | false | false | false | true | false | 7,211 |
1571E | A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example: bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); bracket sequences ")(", "(" and ")" are not. You are given two strings $$$s$$$ and $$$a$$$, the string $$$s$$$ has length $$$n$$$, the string $$$a$$$ has length $$$n - 3$$$. The string $$$s$$$ is a bracket sequence (i.u2009e. each element of this string is either an opening bracket character or a closing bracket character). The string $$$a$$$ is a binary string (i.u2009e. each element of this string is either 1 or 0). The string $$$a$$$ imposes some constraints on the string $$$s$$$: for every $$$i$$$ such that $$$a_i$$$ is 1, the string $$$s_i s_{i+1} s_{i+2} s_{i+3}$$$ should be a regular bracket sequence. Characters of $$$a$$$ equal to 0 don't impose any constraints. Initially, the string $$$s$$$ may or may not meet these constraints. You can perform the following operation any number of times: replace some character of $$$s$$$ with its inverse (i.u2009e. you can replace an opening bracket with a closing bracket, or vice versa). Determine if it is possible to change some characters in $$$s$$$ so that it meets all of the constraints, and if it is possible, calculate the minimum number of characters to be changed. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case consists of three lines. The first line contains one integer $$$n$$$ ($$$4 le n le 2 cdot 10^5$$$). The second line contains the string $$$s$$$, consisting of exactly $$$n$$$ characters; each character of $$$s$$$ is either '(' or ')'. The third line contains the string $$$a$$$, consisting of exactly $$$n - 3$$$ characters; each character of $$$a$$$ is either '1' or '0'. Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print one integer: the minimum number of characters that need to be changed in $$$s$$$, or $$$-1$$$ if it is impossible. Example Input 6 4 ))(( 1 4 ))(( 0 4 ()() 0 6 ))(()( 101 6 ))(()( 001 5 ((((( 11 | 1,700 | false | true | false | true | false | false | false | false | false | false | 2,788 |
1769C2 | Statement is not available on English language В этой версии задачи $$$n le 2 cdot 10^5$$$ и $$$a_i le 10^6$$$ (а также есть ограничение на сумму $$$n$$$ по наборам входных данных внутри одного теста). Вика за время работы в компании VK уже сделала $$$n$$$ коммитов в системе контроля версий. $$$i$$$-й коммит был сделан в $$$a_i$$$-й день работы Вики в компании. В некоторые дни Вика могла сделать несколько коммитов, а в другиеxa0— не сделать ни одного. Вику интересуют такие отрезки подряд идущих дней, что в каждый из этих дней у неё есть хотя бы один коммит. Чем длиннее будет самый длинный такой отрезок, тем более продуктивным сотрудником она будет себя ощущать. Недавно Вика нашла способ подкрутить время любого коммита вперёд, но не более чем на сутки. Таким образом, $$$i$$$-й коммит теперь может быть «сделан» либо в $$$a_i$$$-й, либо в $$$(a_i + 1)$$$-й день. Время каждого коммита можно подкрутить независимо от другихxa0— в частности, можно как оставить всем коммитам исходное время, так и перенести все коммиты ровно на день вперёд. Найдите длину самого длинного возможного отрезка подряд идущих дней, в каждый из которых у Вики в профиле будет отображаться хотя бы один коммит, после возможной подкрутки времени некоторых коммитов. Входные данные Каждый тест состоит из нескольких наборов входных данных. В первой строке находится одно целое число $$$t$$$ ($$$1 le t le 100$$$)xa0— количество наборов входных данных. Далее следует описание наборов входных данных. Первая строка каждого набора входных данных содержит одно целое число $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— число коммитов. Вторая строка содержит $$$n$$$ целых чисел $$$a_1, a_2, ldots, a_n$$$ в неубывающем порядке ($$$1 le a_1 le a_2 le ldots le a_n le 10^6$$$)xa0— номера дней, в которые были сделаны коммиты. Гарантируется, что сумма $$$n$$$ по всем наборам входных данных не превосходит $$$2 cdot 10^5$$$. Выходные данные Для каждого набора входных данных выведите одно целое числоxa0— максимальную возможную длину отрезка дней, в каждый из которых у Вики в профиле будет отображаться хотя бы один коммит, после возможной подкрутки времени некоторых коммитов вперёд не более чем на сутки. Пример Входные данные 3 9 1 1 3 4 6 6 6 8 10 6 1 2 3 4 5 6 5 10 10 10 10 10 Примечание В первом наборе входных данных можно поменять дату коммита в день $$$3$$$ на день $$$4$$$, дату коммита в день $$$4$$$xa0— на день $$$5$$$, а дату любого из коммитов в день $$$6$$$xa0— на день $$$7$$$. Тогда в каждый из дней $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$ и $$$8$$$ в профиле Вики будет отображаться хотя бы один коммит, и наибольший отрезок из подряд идущих дней с коммитамиxa0— $$$[4; 8]$$$xa0— будет иметь длину $$$5$$$. Во втором наборе входных данных можно либо оставить все коммиты как есть, либо перенести каждый коммит на день вперёд. В любом случае длина отрезка дней составит $$$6$$$. В третьем наборе входных данных Вика сделала много коммитов, но все в один и тот же день с номером $$$10$$$. В лучшем случае отрезок дней достигнет длины $$$2$$$xa0— если какие-то коммиты оставить на день $$$10$$$, а другие перенести на день $$$11$$$. | 1,300 | false | false | false | true | false | false | false | false | false | false | 1,681 |
1957C | You are given an $$$n imes n$$$ chessboard where you and the computer take turns alternatingly to place white rooks & black rooks on the board respectively. While placing rooks, you have to ensure that no two rooks attack each other. Two rooks attack each other if they share the same row or column regardless of color. A valid move is placing a rook on a position ($$$r$$$, $$$c$$$) such that it doesn't attack any other rook. You start first, and when you make a valid move in your turn, placing a white rook at position ($$$r$$$, $$$c$$$), the computer will mirror you and place a black rook at position ($$$c$$$, $$$r$$$) in its turn. If $$$r = c$$$, then the computer can't mirror your move, and skips its turn. You have already played $$$k$$$ moves with the computer (the computer tries to mirror these moves too), and you must continue playing the game until there are no valid moves remaining. How many different final configurations are possible when you continue the game after the $$$k$$$ moves? It is guaranteed that the $$$k$$$ moves and the implied computer moves are valid. Since the answer may be large, print it modulo $$$10^9+7$$$. Two configurations are considered different if there exists a coordinate ($$$r$$$, $$$c$$$) which has a rook in one configuration, but not in the other or the color of the rook on the coordinate is different. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 3 cdot 10^5$$$, $$$0 leq k leq n$$$)xa0— the size of the chessboard and the number of moves you have already played respectively. Each of the next $$$k$$$ lines of the test case contains two integers $$$r_i$$$ and $$$c_i$$$, denoting the $$$i$$$-th move you made. It is guaranteed that the $$$k$$$ moves and the implied computer moves are valid. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Note In the first test case, we have a $$$4 imes 4$$$ grid and you've already played $$$1$$$ move. After you and the computer play a turn, we have a white rook at ($$$1$$$, $$$2$$$), and a black rook at ($$$2$$$, $$$1$$$). There are three possible configurations from this statexa0— 1. You place a white rook at ($$$3$$$, $$$4$$$) and the computer places a black rook at ($$$4$$$, $$$3$$$) as a response. 2. You place a white rook at ($$$4$$$, $$$3$$$) and the computer places a black rook at ($$$3$$$, $$$4$$$) as a response. 3. You place a white rook at ($$$3$$$, $$$3$$$) and then at ($$$4$$$, $$$4$$$), or the other way around. They both result in the same configuration. | 1,600 | true | false | false | true | false | false | false | false | false | false | 531 |
118D | Problem - 118D - 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 *1700 No tag edit access → Contest materials ") which represent how many footmen and horsemen there are and the largest acceptable number of footmen and horsemen standing in succession, correspondingly. Output Print the number of beautiful arrangements of the army modulo 100000000 (108). That is, print the number of such ways to line up the soldiers, that no more than _k_1 footmen stand successively, and no more than _k_2 horsemen stand successively. Examples Input 2 1 1 10 Output 1 Input 2 3 1 2 Output 5 Input 2 4 1 1 Output 0 Note Let's mark a footman as 1, and a horseman as 2. In the first sample the only beautiful line-up is: 121 In the second sample 5 beautiful line-ups exist: 12122, 12212, 21212, 21221, 22121 | 1,700 | false | false | false | true | false | false | false | false | false | false | 9,413 |
1616D | You are given an array of integers $$$a_1, a_2, ldots, a_n$$$ and an integer $$$x$$$. You need to select the maximum number of elements in the array, such that for every subsegment $$$a_l, a_{l + 1}, ldots, a_r$$$ containing strictly more than one element $$$(l < r)$$$, either: At least one element on this subsegment is not selected, or $$$a_l + a_{l+1} + ldots + a_r geq x cdot (r - l + 1)$$$. Input The first line of input contains one integer $$$t$$$ ($$$1 leq t leq 10$$$): the number of test cases. The descriptions of $$$t$$$ test cases follow, three lines per test case. In the first line you are given one integer $$$n$$$ ($$$1 leq n leq 50,000$$$): the number of integers in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-100,000 leq a_i leq 100,000$$$). The third line contains one integer $$$x$$$ ($$$-100,000 leq x leq 100,000$$$). Note In the first example, one valid way to select the elements is $$$[underline{1}, 2, underline{3}, underline{4}, underline{5}]$$$. All subsegments satisfy at least one of the criteria. For example, for the subsegment $$$l = 1$$$, $$$r = 2$$$ we have that the element $$$2$$$ is not selected, satisfying the first criterion. For the subsegment $$$l = 3$$$, $$$r = 5$$$ we have $$$3 + 4 + 5 = 12 ge 2 cdot 3$$$, satisfying the second criterion. We can't select all elements, because in this case for $$$l = 1$$$, $$$r = 2$$$ all elements are selected and we have $$$a_1 + a_2 = 3 < 2 cdot 2$$$. Thus, the maximum number of selected elements is $$$4$$$. In the second example, one valid solution is $$$[underline{2}, underline{4}, 2, underline{4}, underline{2}, underline{4}, 2, underline{4}, underline{2}, underline{4}]$$$. In the third example, one valid solution is $$$[underline{-10}, -5, underline{-10}]$$$. In the fourth example, one valid solution is $$$[underline{9}, underline{9}, -3]$$$. | 2,000 | true | true | false | true | false | false | false | false | false | false | 2,553 |
936B | Petya and Vasya arranged a game. The game runs by the following rules. Players have a directed graph consisting of _n_ vertices and _m_ edges. One of the vertices contains a chip. Initially the chip is located at vertex _s_. Players take turns moving the chip along some edge of the graph. Petya goes first. Player who can't move the chip loses. If the game lasts for 106 turns the draw is announced. Vasya was performing big laboratory work in "Spelling and parts of speech" at night before the game, so he fell asleep at the very beginning of the game. Petya decided to take the advantage of this situation and make both Petya's and Vasya's moves. Your task is to help Petya find out if he can win the game or at least draw a tie. Input The first line of input contain two integers _n_ and _m_xa0— the number of vertices and the number of edges in the graph (2u2009≤u2009_n_u2009≤u2009105, 0u2009≤u2009_m_u2009≤u20092·105). The next _n_ lines contain the information about edges of the graph. _i_-th line (1u2009≤u2009_i_u2009≤u2009_n_) contains nonnegative integer _c__i_xa0— number of vertices such that there is an edge from _i_ to these vertices and _c__i_ distinct integers _a__i_,u2009_j_xa0— indices of these vertices (1u2009≤u2009_a__i_,u2009_j_u2009≤u2009_n_, _a__i_,u2009_j_u2009≠u2009_i_). It is guaranteed that the total sum of _c__i_ equals to _m_. The next line contains index of vertex _s_xa0— the initial position of the chip (1u2009≤u2009_s_u2009≤u2009_n_). Output If Petya can win print «Win» in the first line. In the next line print numbers _v_1,u2009_v_2,u2009...,u2009_v__k_ (1u2009≤u2009_k_u2009≤u2009106)xa0— the sequence of vertices Petya should visit for the winning. Vertex _v_1 should coincide with _s_. For _i_u2009=u20091... _k_u2009-u20091 there should be an edge from _v__i_ to _v__i_u2009+u20091 in the graph. There must be no possible move from vertex _v__k_. The sequence should be such that Petya wins the game. If Petya can't win but can draw a tie, print «Draw» in the only line. Otherwise print «Lose». Examples Input 5 6 2 2 3 2 4 5 1 4 1 5 0 1 Note In the first example the graph is the following: Initially the chip is located at vertex 1. In the first move Petya moves the chip to vertex 2, after that he moves it to vertex 4 for Vasya. After that he moves to vertex 5. Now it is Vasya's turn and there is no possible move, so Petya wins. In the second example the graph is the following: Initially the chip is located at vertex 2. The only possible Petya's move is to go to vertex 1. After that he has to go to 3 for Vasya. Now it's Petya's turn but he has no possible move, so Petya loses. In the third example the graph is the following: Petya can't win, but he can move along the cycle, so the players will draw a tie. | 2,100 | false | false | false | true | false | false | false | false | false | true | 5,957 |
910B | Petya has equal wooden bars of length _n_. He wants to make a frame for two equal doors. Each frame has two vertical (left and right) sides of length _a_ and one top side of length _b_. A solid (i.e. continuous without breaks) piece of bar is needed for each side. Determine a minimal number of wooden bars which are needed to make the frames for two doors. Petya can cut the wooden bars into any parts, but each side of each door should be a solid piece of a wooden bar (or a whole wooden bar). Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20091u2009000) — the length of each wooden bar. The second line contains a single integer _a_ (1u2009≤u2009_a_u2009≤u2009_n_) — the length of the vertical (left and right) sides of a door frame. The third line contains a single integer _b_ (1u2009≤u2009_b_u2009≤u2009_n_) — the length of the upper side of a door frame. Output Print the minimal number of wooden bars with length _n_ which are needed to make the frames for two doors. Note In the first example one wooden bar is enough, since the total length of all six sides of the frames for two doors is 8. In the second example 6 wooden bars is enough, because for each side of the frames the new wooden bar is needed. | 1,600 | false | true | true | false | false | false | false | false | false | false | 6,105 |
1934B | You have $$$5$$$ different types of coins, each with a value equal to one of the first $$$5$$$ triangular numbers: $$$1$$$, $$$3$$$, $$$6$$$, $$$10$$$, and $$$15$$$. These coin types are available in abundance. Your goal is to find the minimum number of these coins required such that their total value sums up to exactly $$$n$$$. We can show that the answer always exists. Input The first line contains one 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 an integer $$$n$$$ ($$$1 leq n leq 10^9$$$)xa0— the target value. Output For each test case, output a single numberxa0— the minimum number of coins required. Example Input 14 1 2 3 5 7 11 12 14 16 17 18 20 98 402931328 Output 1 2 1 3 2 2 2 3 2 3 2 2 8 26862090 Note In the first test case, for $$$n = 1$$$, the answer is $$$1$$$ since only one $$$1$$$ value coin is sufficient. $$$1 = 1 cdot 1$$$. In the fourth test case, for $$$n = 5$$$, the answer is $$$3$$$, which can be achieved using two $$$1$$$ value coins and one $$$3$$$ value coin. $$$5 = 2 cdot 1 + 1 cdot 3$$$. In the seventh test case, for $$$n = 12$$$, the answer is $$$2$$$, which can be achieved using two $$$6$$$ value coins. In the ninth test case, for $$$n = 16$$$, the answer is $$$2$$$, which can be achieved using one $$$1$$$ value coin and one $$$15$$$ value coin or using one $$$10$$$ value coin and one $$$6$$$ value coin. $$$16 = 1 cdot 1 + 1 cdot 15 = 1 cdot 6 + 1 cdot 10$$$. | 1,200 | true | true | false | true | false | false | true | false | false | false | 679 |
1673C | You are given a positive integer $$$n$$$. Let's call some positive integer $$$a$$$ without leading zeroes palindromic if it remains the same after reversing the order of its digits. Find the number of distinct ways to express $$$n$$$ as a sum of positive palindromic integers. Two ways are considered different if the frequency of at least one palindromic integer is different in them. For example, $$$5=4+1$$$ and $$$5=3+1+1$$$ are considered different but $$$5=3+1+1$$$ and $$$5=1+3+1$$$ are considered the same. Formally, you need to find the number of distinct multisets of positive palindromic integers the sum of which is equal to $$$n$$$. Since the answer can be quite large, print it modulo $$$10^9+7$$$. Input The first line of input contains a single integer $$$t$$$ ($$$1leq tleq 10^4$$$) denoting the number of testcases. Each testcase contains a single line of input containing a single integer $$$n$$$ ($$$1leq nleq 4cdot 10^4$$$)xa0— the required sum of palindromic integers. Output For each testcase, print a single integer denoting the required answer modulo $$$10^9+7$$$. Note For the first testcase, there are $$$7$$$ ways to partition $$$5$$$ as a sum of positive palindromic integers: $$$5=1+1+1+1+1$$$ $$$5=1+1+1+2$$$ $$$5=1+2+2$$$ $$$5=1+1+3$$$ $$$5=2+3$$$ $$$5=1+4$$$ $$$5=5$$$ For the second testcase, there are total $$$77$$$ ways to partition $$$12$$$ as a sum of positive integers but among them, the partitions $$$12=2+10$$$, $$$12=1+1+10$$$ and $$$12=12$$$ are not valid partitions of $$$12$$$ as a sum of positive palindromic integers because $$$10$$$ and $$$12$$$ are not palindromic. So, there are $$$74$$$ ways to partition $$$12$$$ as a sum of positive palindromic integers. | 1,500 | true | false | false | true | false | false | true | false | false | false | 2,233 |
1399A | You are given the array $$$a$$$ consisting of $$$n$$$ positive (greater than zero) integers. In one move, you can choose two indices $$$i$$$ and $$$j$$$ ($$$i e j$$$) such that the absolute difference between $$$a_i$$$ and $$$a_j$$$ is no more than one ($$$a_i - a_j le 1$$$) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one). Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 50$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 100$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. Output For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise. Example Input 5 3 1 2 2 4 5 5 5 5 3 1 2 4 4 1 3 4 4 1 100 Note In the first test case of the example, we can perform the following sequence of moves: choose $$$i=1$$$ and $$$j=3$$$ and remove $$$a_i$$$ (so $$$a$$$ becomes $$$[2; 2]$$$); choose $$$i=1$$$ and $$$j=2$$$ and remove $$$a_j$$$ (so $$$a$$$ becomes $$$[2]$$$). In the second test case of the example, we can choose any possible $$$i$$$ and $$$j$$$ any move and it doesn't matter which element we remove. In the third test case of the example, there is no way to get rid of $$$2$$$ and $$$4$$$. | 800 | false | true | false | false | false | false | false | false | true | false | 3,679 |
1267K | Karl is developing a key storage service. Each user has a positive integer key. Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one. Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide thexa0result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions. For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainderxa01 and resultxa05, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainderxa01 and resultxa00. Thus, the key 11 produces the sequence of remainders $$$[1, 2, 1]$$$ and has the fingerprint multiset $$${1, 1, 2}$$$. Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of $$${0, 0, 0, 0, 2, 3, 3, 4}$$$. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456. Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to helpxa0her. Note The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders $$$[1, 1, 2]$$$. Soxa0both numbers have the fingerprint multiset $$${1, 1, 2}$$$. | 2,100 | true | false | false | false | false | false | false | false | false | false | 4,353 |
1307D | Bessie is out grazing on the farm, which consists of $$$n$$$ fields connected by $$$m$$$ bidirectional roads. She is currently at field $$$1$$$, and will return to her home at field $$$n$$$ at the end of the day. The Cowfederation of Barns has ordered Farmer John to install one extra bidirectional road. The farm has $$$k$$$ special fields and he has decided to install the road between two different special fields. He may add the road between two special fields that already had a road directly connecting them. After the road is added, Bessie will return home on the shortest path from field $$$1$$$ to field $$$n$$$. Since Bessie needs more exercise, Farmer John must maximize the length of this shortest path. Help him! Input The first line contains integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$n-1 le m le 2 cdot 10^5$$$, $$$2 le k le n$$$) xa0— the number of fields on the farm, the number of roads, and the number of special fields. The second line contains $$$k$$$ integers $$$a_1, a_2, ldots, a_k$$$ ($$$1 le a_i le n$$$) xa0— the special fields. All $$$a_i$$$ are distinct. The $$$i$$$-th of the following $$$m$$$ lines contains integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$, $$$x_i e y_i$$$), representing a bidirectional road between fields $$$x_i$$$ and $$$y_i$$$. It is guaranteed that one can reach any field from every other field. It is also guaranteed that for any pair of fields there is at most one road connecting them. Output Output one integer, the maximum possible length of the shortest path from field $$$1$$$ to $$$n$$$ after Farmer John installs one road optimally. Examples Input 5 5 3 1 3 5 1 2 2 3 3 4 3 5 2 4 Input 5 4 2 2 4 1 2 2 3 3 4 4 5 Note The graph for the first example is shown below. The special fields are denoted by red. It is optimal for Farmer John to add a road between fields $$$3$$$ and $$$5$$$, and the resulting shortest path from $$$1$$$ to $$$5$$$ is length $$$3$$$. The graph for the second example is shown below. Farmer John must add a road between fields $$$2$$$ and $$$4$$$, and the resulting shortest path from $$$1$$$ to $$$5$$$ is length $$$3$$$. | 1,900 | false | true | false | false | true | false | false | true | true | true | 4,153 |
351C | Problem - 351C - 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 matrices *2500 No tag edit access → Contest materials . The next line contains _n_ integers: _a_0, _a_1, ..., _a__n_u2009-u20091 (1u2009≤u2009_a__i_u2009≤u200910). The next line contains _n_ integers: _b_0, _b_1, ..., _b__n_u2009-u20091 (1u2009≤u2009_b__i_u2009≤u200910). The numbers are separated by spaces. Output In a single line print the answer to the problem — the minimum required amount of ink in liters. Examples Input 2 6 1 2 2 1 Output 12 Input 1 10000000 2 3 Output 25000000 Note In the first test the optimal sequence is: ()()()()()(), the required number of ink liters is 12. | 2,500 | false | false | false | true | false | false | false | false | false | false | 8,428 |
700A | On vacations _n_ pupils decided to go on excursion and gather all together. They need to overcome the path with the length _l_ meters. Each of the pupils will go with the speed equal to _v_1. To get to the excursion quickly, it was decided to rent a bus, which has seats for _k_ people (it means that it can't fit more than _k_ people at the same time) and the speed equal to _v_2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all _n_ pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers _n_, _l_, _v_1, _v_2 and _k_ (1u2009≤u2009_n_u2009≤u200910u2009000, 1u2009≤u2009_l_u2009≤u2009109, 1u2009≤u2009_v_1u2009<u2009_v_2u2009≤u2009109, 1u2009≤u2009_k_u2009≤u2009_n_)xa0— the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real numberxa0— the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10u2009-u20096. Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10u2009/u20092u2009=u20095. | 1,900 | true | false | false | false | false | false | false | true | false | false | 7,025 |
1986C | Let's consider the following simple problem. You are given a string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters, as well as an array of indices $$$ind$$$ of length $$$m$$$ ($$$1 leq ind_i leq n$$$) and a string $$$c$$$ of length $$$m$$$, consisting of lowercase Latin letters. Then, in order, you perform the update operations, namely, during the $$$i$$$-th operation, you set $$$s_{ind_i} = c_i$$$. Note that you perform all $$$m$$$ operations from the first to the last. Of course, if you change the order of indices in the array $$$ind$$$ and/or the order of letters in the string $$$c$$$, you can get different results. Find the lexicographically smallest string $$$s$$$ that can be obtained after $$$m$$$ update operations, if you can rearrange the indices in the array $$$ind$$$ and the letters in the string $$$c$$$ as you like. A string $$$a$$$ is lexicographically less than a string $$$b$$$ if and only if one of the following conditions is met: $$$a$$$ is a prefix of $$$b$$$, but $$$a eq b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the symbol in string $$$a$$$ is earlier in the alphabet than the corresponding symbol in string $$$b$$$. Input Each test consists of several sets of input data. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of sets of input data. Then follows their description. The first line of each set of input data contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 10^5$$$)xa0— the length of the string $$$s$$$ and the number of updates. The second line of each set of input data contains a string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters. The third line of each set of input data contains $$$m$$$ integers $$$ind_1, ind_2, ldots, ind_m$$$ ($$$1 leq ind_i leq n$$$)xa0— the array of indices $$$ind$$$. The fourth line of each set of input data contains a string $$$c$$$ of length $$$m$$$, consisting of lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ over all sets of input data does not exceed $$$2 cdot 10^5$$$. Similarly, the sum of $$$m$$$ over all sets of input data does not exceed $$$2 cdot 10^5$$$. Output For each set of input data, output the lexicographically smallest string $$$s$$$ that can be obtained by rearranging the indices in the array $$$ind$$$ and the letters in the string $$$c$$$ as you like. Example Input 4 1 2 a 1 1 cb 4 4 meow 1 2 1 4 zcwz 7 4 abacaba 1 3 5 7 damn 7 10 traktor 7 6 5 4 3 2 1 6 4 2 codeforces Output b cwoz abdcmbn ccdeefo Note In the first set of input data, you can leave the array $$$ind$$$ and the string $$$c$$$ unchanged and simply perform all operations in that order. In the second set of input data, you can set the array $$$ind = [1, 1, 4, 2]$$$ and $$$c =$$$ "zczw". Then the string $$$s$$$ will change as follows: $$$meow ightarrow zeow ightarrow ceow ightarrow ceoz ightarrow cwoz$$$. In the third set of input data, you can leave the array $$$ind$$$ unchanged and set $$$c = $$$ "admn". Then the string $$$s$$$ will change as follows: $$$abacaba ightarrow abacaba ightarrow abdcaba ightarrow abdcmba ightarrow abdcmbn$$$. | 1,100 | false | true | false | false | true | false | false | false | true | false | 363 |
1906M | You are given a regular $$$N$$$-sided polygon. Label one arbitrary side as side $$$1$$$, then label the next sides in clockwise order as side $$$2$$$, $$$3$$$, $$$dots$$$, $$$N$$$. There are $$$A_i$$$ special points on side $$$i$$$. These points are positioned such that side $$$i$$$ is divided into $$$A_i + 1$$$ segments with equal length. For instance, suppose that you have a regular $$$4$$$-sided polygon, i.e., a square. The following illustration shows how the special points are located within each side when $$$A = [3, 1, 4, 6]$$$. The uppermost side is labelled as side $$$1$$$. You want to create as many non-degenerate triangles as possible while satisfying the following requirements. Each triangle consists of $$$3$$$ distinct special points (not necessarily from different sides) as its corners. Each special point can only become the corner of at most $$$1$$$ triangle. All triangles must not intersect with each other. Determine the maximum number of non-degenerate triangles that you can create. A triangle is non-degenerate if it has a positive area. Input The first line consists of an integer $$$N$$$ ($$$3 leq N leq 200,000$$$). The following line consists of $$$N$$$ integers $$$A_i$$$ ($$$1 leq A_i leq 2 cdot 10^9$$$). Output Output a single integer representing the maximum number of non-degenerate triangles that you can create. Note Explanation for the sample input/output #1 One possible construction which achieves maximum number of non-degenerate triangles can be seen in the following illustration. Explanation for the sample input/output #2 One possible construction which achieves maximum number of non-degenerate triangles can be seen in the following illustration. | 1,700 | true | true | false | false | false | false | false | false | false | false | 865 |
852F | Problem - 852F - Codeforces =============== xa0 ]( "Mirror of Bubble Cup X Finals on Codeforces") , where is the multiplicative order of the integer _a_ modulo _Q_, see notes for definition. Output You should output the array _A_ from left to right. Example Input 2 2 2 7 Output 1 2 Note The multiplicative order of a number _a_ modulo _Q_ , is the smallest natural number _x_ such that _a__x_ _mod_ _Q_u2009=u20091. For example, . | 2,200 | true | false | false | false | false | false | false | false | false | false | 6,338 |
1528D | There are $$$n$$$ cities in Shaazzzland, numbered from $$$0$$$ to $$$n-1$$$. Ghaazzzland, the immortal enemy of Shaazzzland, is ruled by AaParsa. As the head of the Ghaazzzland's intelligence agency, AaParsa is carrying out the most important spying mission in Ghaazzzland's history on Shaazzzland. AaParsa has planted $$$m$$$ transport cannons in the cities of Shaazzzland. The $$$i$$$-th cannon is planted in the city $$$a_i$$$ and is initially pointing at city $$$b_i$$$. It is guaranteed that each of the $$$n$$$ cities has at least one transport cannon planted inside it, and that no two cannons from the same city are initially pointing at the same city (that is, all pairs $$$(a_i, b_i)$$$ are distinct). AaParsa used very advanced technology to build the cannons, the cannons rotate every second. In other words, if the $$$i$$$-th cannon is pointing towards the city $$$x$$$ at some second, it will target the city $$$(x + 1) mod n$$$ at the next second. As their name suggests, transport cannons are for transportation, specifically for human transport. If you use the $$$i$$$-th cannon to launch yourself towards the city that it's currently pointing at, you'll be airborne for $$$c_i$$$ seconds before reaching your target destination. If you still don't get it, using the $$$i$$$-th cannon at the $$$s$$$-th second (using which is only possible if you are currently in the city $$$a_i$$$) will shoot you to the city $$$(b_i + s) mod n$$$ and you'll land in there after $$$c_i$$$ seconds (so you'll be there in the $$$(s + c_i)$$$-th second). Also note the cannon that you initially launched from will rotate every second but you obviously won't change direction while you are airborne. AaParsa wants to use the cannons for travelling between Shaazzzland's cities in his grand plan, and he can start travelling at second $$$0$$$. For him to fully utilize them, he needs to know the minimum number of seconds required to reach city $$$u$$$ from city $$$v$$$ using the cannons for every pair of cities $$$(u, v)$$$. Note that AaParsa can stay in a city for as long as he wants. Input The first line contains two integers $$$n$$$ and $$$m$$$ $$$(2 le n le 600 , n le m le n^2)$$$ — the number of cities and cannons correspondingly. The $$$i$$$-th line of the following $$$m$$$ lines contains three integers $$$a_i$$$, $$$b_i$$$ and $$$c_i$$$ $$$( 0 le a_i , b_i le n-1 , 1 le c_i le 10^9)$$$, denoting the cannon in the city $$$a_i$$$, which is initially pointing to $$$b_i$$$ and travelling by which takes $$$c_i$$$ seconds. It is guaranteed that each of the $$$n$$$ cities has at least one transport cannon planted inside it, and that no two cannons from the same city are initially pointing at the same city (that is, all pairs $$$(a_i, b_i)$$$ are distinct). Output Print $$$n$$$ lines, each line should contain $$$n$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line should be equal to the minimum time required to reach city $$$j$$$ from city $$$i$$$. | 2,500 | false | false | false | false | false | true | false | false | false | true | 3,020 |
1111E | You are given a tree with $$$n$$$ nodes and $$$q$$$ queries. Every query starts with three integers $$$k$$$, $$$m$$$ and $$$r$$$, followed by $$$k$$$ nodes of the tree $$$a_1, a_2, ldots, a_k$$$. To answer a query, assume that the tree is rooted at $$$r$$$. We want to divide the $$$k$$$ given nodes into at most $$$m$$$ groups such that the following conditions are met: Each node should be in exactly one group and each group should have at least one node. In any group, there should be no two distinct nodes such that one node is an ancestor (direct or indirect) of the other. You need to output the number of ways modulo $$$10^{9}+7$$$ for every query. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 10^{5}$$$)xa0— the number of vertices in the tree and the number of queries, respectively. Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n, u e v$$$), denoting an edge connecting vertex $$$u$$$ and vertex $$$v$$$. It is guaranteed that the given graph is a tree. Each of the next $$$q$$$ lines starts with three integers $$$k$$$, $$$m$$$ and $$$r$$$ ($$$1 le k, r le n$$$, $$$1 le m le min(300,k)$$$)xa0— the number of nodes, the maximum number of groups and the root of the tree for the current query, respectively. They are followed by $$$k$$$ distinct integers $$$a_1, a_2, ldots, a_k$$$ ($$$1 le a_i le n$$$), denoting the nodes of the current query. It is guaranteed that the sum of $$$k$$$ over all queries does not exceed $$$10^{5}$$$. Output Print $$$q$$$ lines, where the $$$i$$$-th line contains the answer to the $$$i$$$-th query. Examples Input 7 2 5 4 2 6 5 3 1 2 7 5 4 6 3 3 2 7 4 3 3 1 4 6 2 1 Input 7 2 4 7 2 5 4 1 5 1 5 6 4 3 3 3 2 7 1 4 2 1 6 3 2 Input 5 2 3 5 4 5 4 2 1 4 2 2 3 1 2 2 2 4 5 4 Note Consider the first example. In the first query, we have to divide the three given nodes ($$$7$$$, $$$4$$$ and $$$3$$$), into the maximum of three groups assuming that the tree is rooted at $$$2$$$. When the tree is rooted at $$$2$$$, $$$4$$$ is an ancestor of both $$$3$$$ and $$$7$$$. So we can't put all the nodes into one group. There is only $$$1$$$ way to divide the given nodes into two groups, which are $$$[4]$$$ and $$$[3, 7]$$$. Also, there is only one way to divide the given nodes into three groups, which are $$$[7]$$$, $$$[4]$$$ and $$$[3]$$$. So, there are total $$$2$$$ ways to divide the given nodes into a maximum of three groups. In the second query, when the tree is rooted at $$$4$$$, $$$6$$$ is an ancestor of $$$2$$$ and $$$2$$$ is an ancestor of $$$1$$$. So, we can't put all the given nodes into one group. | 2,500 | false | false | false | true | true | false | false | false | false | true | 5,137 |
1281A | We just discovered a new data structure in our research group: a suffix three! It's very useful for natural language processing. Given three languages and three suffixes, a suffix three can determine which language a sentence is written in. It's super simple, 100% accurate, and doesn't involve advanced machine learning algorithms. Let us tell you how it works. If a sentence ends with "po" the language is Filipino. If a sentence ends with "desu" or "masu" the language is Japanese. If a sentence ends with "mnida" the language is Korean. Given this, we need you to implement a suffix three that can differentiate Filipino, Japanese, and Korean. Oh, did I say three suffixes? I meant four. Input The first line of input contains a single integer $$$t$$$ ($$$1 leq t leq 30$$$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing a single string denoting the sentence. Spaces are represented as underscores (the symbol "_") for ease of reading. The sentence has at least $$$1$$$ and at most $$$1000$$$ characters, and consists only of lowercase English letters and underscores. The sentence has no leading or trailing underscores and no two consecutive underscores. It is guaranteed that the sentence ends with one of the four suffixes mentioned above. Output For each test case, print a single line containing either "FILIPINO", "JAPANESE", or "KOREAN" (all in uppercase, without quotes), depending on the detected language. Example Input 8 kamusta_po genki_desu ohayou_gozaimasu annyeong_hashimnida hajime_no_ippo bensamu_no_sentou_houhou_ga_okama_kenpo ang_halaman_doon_ay_sarisari_singkamasu si_roy_mustang_ay_namamasu Output FILIPINO JAPANESE JAPANESE KOREAN FILIPINO FILIPINO JAPANESE JAPANESE Note The first sentence ends with "po", so it is written in Filipino. The second and third sentences end with "desu" and "masu", so they are written in Japanese. The fourth sentence ends with "mnida", so it is written in Korean. | 800 | false | false | true | false | false | false | false | false | false | false | 4,288 |
145C | Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has sequence _a_ consisting of _n_ integers. The subsequence of the sequence _a_ is such subsequence that can be obtained from _a_ by removing zero or more of its elements. Two sequences are considered different if index sets of numbers included in them are different. That is, the values u200bof the elements u200bdo not matter in the comparison of subsequences. In particular, any sequence of length _n_ has exactly 2_n_ different subsequences (including an empty subsequence). A subsequence is considered lucky if it has a length exactly _k_ and does not contain two identical lucky numbers (unlucky numbers can be repeated any number of times). Help Petya find the number of different lucky subsequences of the sequence _a_. As Petya's parents don't let him play with large numbers, you should print the result modulo prime number 1000000007 (109u2009+u20097). Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009105). The next line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109) — the sequence _a_. Output On the single line print the single number — the answer to the problem modulo prime number 1000000007 (109u2009+u20097). Note In the first sample all 3 subsequences of the needed length are considered lucky. In the second sample there are 4 lucky subsequences. For them the sets of indexes equal (the indexation starts from 1): {1,u20093}, {1,u20094}, {2,u20093} and {2,u20094}. | 2,100 | true | false | false | true | false | false | false | false | false | false | 9,293 |
356D | When you were a child you must have been told a puzzle of bags and coins. Anyway, here's one of its versions: A horse has three bags. The first bag has one coin, the second bag has one coin and the third bag has three coins. In total, the horse has three coins in the bags. How is that possible? The answer is quite simple. The third bag contains a coin and two other bags. This problem is a generalization of the childhood puzzle. You have _n_ bags. You know that the first bag contains _a_1 coins, the second bag contains _a_2 coins, ..., the _n_-th bag contains _a__n_ coins. In total, there are _s_ coins. Find the way to arrange the bags and coins so that they match the described scenario or else state that it is impossible to do. Input The first line contains two integers _n_ and _s_ (1u2009≤u2009_n_,u2009_s_u2009≤u200970000) — the number of bags and the total number of coins. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u200970000), where _a__i_ shows the number of coins in the _i_-th bag. Output If the answer doesn't exist, print -1. Otherwise, print _n_ lines, on the _i_-th line print the contents of the _i_-th bag. The first number in the line, _c__i_ (0u2009≤u2009_c__i_u2009≤u2009_a__i_), must represent the number of coins lying directly in the _i_-th bag (the coins in the bags that are in the _i_-th bag are not taken into consideration). The second number in the line, _k__i_ (0u2009≤u2009_k__i_u2009<u2009_n_) must represent the number of bags that lie directly in the _i_-th bag (the bags that are inside the bags lying in the _i_-th bag are not taken into consideration). Next, the line must contain _k__i_ integers — the numbers of the bags that are lying directly in the _i_-th bag. The total number of coins in the solution must equal _s_. If we count the total number of coins the _i_-th bag in the solution has, we should get _a__i_. No bag can directly lie in more than one bag. The bags can be nested in more than one level (see the second test case). If there are multiple correct answers, you can print any of them. Examples Output 2 0 1 2 1 4 0 2 7 8 0 2 5 6 1 0 3 0 1 0 2 0 Note The pictures below show two possible ways to solve one test case from the statement. The left picture corresponds to the first test case, the right picture corresponds to the second one. | 2,700 | false | true | false | true | false | true | false | false | false | false | 8,408 |
66E | Little Vasya's uncle is a postman. The post offices are located on one circular road. Besides, each post office has its own gas station located next to it. Petya's uncle works as follows: in the morning he should leave the house and go to some post office. In the office he receives a portion of letters and a car. Then he must drive in the given car exactly one round along the circular road and return to the starting post office (the uncle can drive along the circle in any direction, counterclockwise or clockwise). Besides, since the car belongs to the city post, it should also be fuelled with gasoline only at the Post Office stations. The total number of stations equals to _n_. One can fuel the car at the _i_-th station with no more than _a__i_ liters of gasoline. Besides, one can fuel the car no more than once at each station. Also, the distance between the 1-st and the 2-nd station is _b_1 kilometers, the distance between the 2-nd and the 3-rd one is _b_2 kilometers, ..., between the (_n_u2009-u20091)-th and the _n_-th ones the distance is _b__n_u2009-u20091 kilometers and between the _n_-th and the 1-st one the distance is _b__n_ kilometers. Petya's uncle's high-tech car uses only one liter of gasoline per kilometer. It is known that the stations are located so that the sum of all _a__i_ is equal to the sum of all _b__i_. The _i_-th gas station and _i_-th post office are very close, so the distance between them is 0 kilometers. Thus, it becomes clear that if we start from some post offices, then it is not always possible to drive one round along a circular road. The uncle faces the following problem: to what stations can he go in the morning to be able to ride exactly one circle along the circular road and visit all the post offices that are on it? Petya, who used to attend programming classes, has volunteered to help his uncle, but his knowledge turned out to be not enough, so he asks you to help him write the program that will solve the posed problem. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105). The second line contains _n_ integers _a__i_ — amount of gasoline on the _i_-th station. The third line contains _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_. They are the distances between the 1-st and the 2-nd gas stations, between the 2-nd and the 3-rd ones, ..., between the _n_-th and the 1-st ones, respectively. The sum of all _b__i_ equals to the sum of all _a__i_ and is no more than 109. Each of the numbers _a__i_, _b__i_ is no less than 1 and no more than 109. Output Print on the first line the number _k_ — the number of possible post offices, from which the car can drive one circle along a circular road. Print on the second line _k_ numbers in the ascending order — the numbers of offices, from which the car can start. | 2,000 | false | false | false | true | true | false | false | false | false | false | 9,637 |
1991B | You are given an array $$$b$$$ of $$$n - 1$$$ integers. An array $$$a$$$ of $$$n$$$ integers is called good if $$$b_i = a_i , & , a_{i + 1}$$$ for $$$1 le i le n-1$$$, where $$$&$$$ denotes the xa0— the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n - 1$$$ integers $$$b_1, b_2, ldots, b_{n - 1}$$$ ($$$0 le b_i < 2^{30}$$$)xa0— the elements of the array $$$b$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output a single integer $$$-1$$$ if no good arrays exist. Otherwise, output $$$n$$$ space-separated integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i < 2^{30}$$$)xa0— the elements of a good array $$$a$$$. If there are multiple solutions, you may output any of them. Note In the first test case, $$$b = [1]$$$. A possible good array is $$$a=[5, 3]$$$, because $$$a_1 , & , a_2 = 5 , & , 3 = 1 = b_1$$$. In the second test case, $$$b = [2, 0]$$$. A possible good array is $$$a=[3, 2, 1]$$$, because $$$a_1 , & , a_2 = 3 , & , 2 = 2 = b_1$$$ and $$$a_2 , & , a_3 = 2 , & , 1 = 0 = b_2$$$. In the third test case, $$$b = [1, 2, 3]$$$. It can be shown that no good arrays exist, so the output is $$$-1$$$. In the fourth test case, $$$b = [3, 5, 4, 2]$$$. A possible good array is $$$a=[3, 7, 5, 6, 3]$$$. | 1,100 | false | true | false | false | false | true | false | false | false | false | 327 |
1131G | Semyon participates in the most prestigious competition of the world ocean for the title of the most dangerous shark. During this competition sharks compete in different subjects: speed swimming, masking, map navigation and many others. Now Semyon is taking part in «destruction» contest. During it, $$$m$$$ dominoes are placed in front of the shark. All dominoes are on the same line, but the height of the dominoes may vary. The distance between adjacent dominoes is $$$1$$$. Moreover, each Domino has its own cost value, expressed as an integer. The goal is to drop all the dominoes. To do this, the shark can push any domino to the left or to the right, and it will begin falling in this direction. If during the fall the domino touches other dominoes, they will also start falling in the same direction in which the original domino is falling, thus beginning a chain reaction, as a result of which many dominoes can fall. A falling domino touches another one, if and only if the distance between them was strictly less than the height of the falling domino, the dominoes do not necessarily have to be adjacent. Of course, any shark can easily drop all the dominoes in this way, so the goal is not to drop all the dominoes, but do it with a minimum cost. The cost of the destruction is the sum of the costs of dominoes that the shark needs to push to make all the dominoes fall. Simon has already won in the previous subjects, but is not smart enough to win in this one. Help Semyon and determine the minimum total cost of the dominoes he will have to push to make all the dominoes fall. Input In order to reduce input size, the heights and costs of the dominoes are described with blocks. The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq 250,000, 1 leq m leq 10^7$$$)xa0— the number of the blocks and the total number of the dominoes Semyon must drop. Then descriptions of $$$n$$$ blocks follow. Description of every block consists of three lines. The first line of block's description contains a single integer $$$k_i$$$ ($$$1 leq k_i leq 250,000, sum_{i = 1}^{n}{k_i} leq 250,000$$$)xa0— the number of dominoes in the block. The second line of block's description contains $$$k_i$$$ integers $$$a_j$$$ ($$$1 leq a_j leq m$$$)xa0— the heights of the dominoes in the blocks. The third line contains $$$k_i$$$ integers $$$c_j$$$ ($$$1 leq c_j leq 100,000$$$)xa0— the costs of the dominoes in the block. Then the domino sequence is described (from left to right). The first line of this description contains a single integer $$$q$$$ ($$$n leq q leq 250,000$$$)xa0— the number of blocks in the sequence of domino sequence. Each of the following $$$q$$$ lines contains integers $$$id_i, mul_i$$$ ($$$1 leq id_i leq n$$$, $$$1 leq mul_i leq 100,000$$$), denoting that the next $$$k_{id_i}$$$ dominoes are dominoes of the block $$$id_i$$$, with their cost multiplied by $$$mul_i$$$. It's guaranteed, that $$$sum_{i = 1}^{q}{k_{id_i}} = m$$$, and that's every block is used in the sequence at least once. Output Print exactly one integerxa0— the minimum cost to make all the dominoes fall. Examples Input 2 7 3 1 2 2 1 2 1 1 3 2 3 2 2 1 3 1 1 Input 1 1 1 1 100000 1 1 100000 Note In the first example, there are $$$7$$$ dominoes in front of the Semyon. Their heights are equal to $$$[3, 1, 2, 2, 1, 2, 2]$$$, and their costs are equal to $$$[4, 3, 6, 3, 1, 2, 1]$$$. Semyon should drop the domino with index $$$7$$$ to the left, it will fall and drop the domino $$$6$$$ as well. The domino $$$6$$$ during the fall will drop the domino $$$5$$$, however the latter will not drop any more dominoes. Then Semyon should drop domino with number $$$1$$$ to the right and it will drop dominoes $$$2$$$ and $$$3$$$ after falling. And the domino $$$3$$$ will drop the domino $$$4$$$ after falling. Hence all dominoes are fallen this way. In the second example, there is a single domino of cost $$$10000000000$$$. | 2,700 | false | false | false | true | true | false | false | false | false | false | 5,062 |
1915C | Calin has $$$n$$$ buckets, the $$$i$$$-th of which contains $$$a_i$$$ wooden squares of side length $$$1$$$. Can Calin build a square using all the given squares? Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the number of buckets. The second line of each test case contains $$$n$$$ integers $$$a_1, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the number of squares in each bucket. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output "YES" if Calin can build a square using all of the given $$$1 imes 1$$$ squares, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 5 1 9 2 14 2 7 1 2 3 4 5 6 7 6 1 3 5 7 9 11 4 2 2 2 2 Note In the first test case, Calin can build a $$$3 imes 3$$$ square. In the second test case, Calin can build a $$$4 imes 4$$$ square. In the third test case, Calin cannot build a square using all the given squares. | 800 | false | false | true | false | false | false | false | true | false | false | 808 |
1907G | In the end of the day, Anna needs to turn off the lights in the office. There are $$$n$$$ lights and $$$n$$$ light switches, but their operation scheme is really strange. The switch $$$i$$$ changes the state of light $$$i$$$, but it also changes the state of some other light $$$a_i$$$ (change the state means that if the light was on, it goes off and vice versa). Help Anna to turn all the lights off using minimal number of switches, or say it is impossible. Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Descriptions of test cases follow. The first line of each test case contains the integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0— the number of lights. The second line of each test case contains the string of $$$n$$$ characters, the initial state of the lights. Character "0" means that the corresponding light is off, and "1" means that it is on. The third line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 le a_i le n$$$, $$$a_i eq i$$$)xa0— the switch $$$i$$$ changes the states of light $$$i$$$ and light $$$a_i$$$. It is guaranteed that sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$ Output For each test case output the integer $$$k$$$, the minimal number of switches to use, then in the separate line output the list of $$$k$$$ switches. If it is impossible to turn off all the lights, output single integer $$$-1$$$. Example Input 8 5 11101 4 3 4 2 2 2 10 2 1 10 0000000011 9 10 10 7 10 9 9 9 10 2 10 1000111101 9 3 8 9 2 1 3 7 2 7 10 0001101010 5 7 6 10 8 3 6 6 2 2 10 0101100010 8 7 7 9 9 4 1 4 2 7 10 1010111010 7 9 10 7 7 2 8 6 10 4 10 1110000001 3 10 10 1 10 8 6 3 2 1 Output 3 1 5 3 -1 1 9 5 5 6 10 2 3 6 4 9 5 10 8 7 3 5 4 9 6 1 3 5 9 7 8 2 2 1 | 2,200 | false | true | true | false | false | true | true | false | false | true | 858 |
1606C | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$. Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 cdot 1 + 5 cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes. For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10; 1 le k le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 = a_1 < a_2 < dots < a_n le 9$$$). Output For each test case, print one integerxa0— the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. Example Input 4 3 13 0 1 2 2 777 0 4 3 255 0 1 3 10 1000000000 0 1 2 3 4 5 6 7 8 9 Output 59 778 148999 999999920999999999 | 1,400 | false | true | false | false | false | false | false | false | false | false | 2,627 |
1691F | You are given a tree $$$G$$$ with $$$n$$$ vertices and an integer $$$k$$$. The vertices of the tree are numbered from $$$1$$$ to $$$n$$$. For a vertex $$$r$$$ and a subset $$$S$$$ of vertices of $$$G$$$, such that $$$S = k$$$, we define $$$f(r, S)$$$ as the size of the smallest rooted subtree containing all vertices in $$$S$$$ when the tree is rooted at $$$r$$$. A set of vertices $$$T$$$ is called a rooted subtree, if all the vertices in $$$T$$$ are connected, and for each vertex in $$$T$$$, all its descendants belong to $$$T$$$. You need to calculate the sum of $$$f(r, S)$$$ over all possible distinct combinations of vertices $$$r$$$ and subsets $$$S$$$, where $$$S = k$$$. Formally, compute the following: $$$$$$sum_{r in V} sum_{S subseteq V, S = k} f(r, S),$$$$$$ where $$$V$$$ is the set of vertices in $$$G$$$. Output the answer modulo $$$10^9 + 7$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$3 le n le 2 cdot 10^5$$$, $$$1 le k le n$$$). Each of the following $$$n - 1$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le n$$$), denoting an edge between vertex $$$x$$$ and $$$y$$$. It is guaranteed that the given edges form a tree. Note The tree in the second example is given below: We have $$$21$$$ subsets of size $$$2$$$ in the given tree. Hence, $$$$$$S in left{{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {3, 4}, {3, 5}, {3, 6}, {3, 7}, {4, 5}, {4, 6}, {4, 7}, {5, 6}, {5, 7}, {6, 7} ight}.$$$$$$ And since we have $$$7$$$ vertices, $$$1 le r le 7$$$. We need to find the sum of $$$f(r, S)$$$ over all possible pairs of $$$r$$$ and $$$S$$$. Below we have listed the value of $$$f(r, S)$$$ for some combinations of $$$r$$$ and $$$S$$$. $$$r = 1$$$, $$$S = {3, 7}$$$. The value of $$$f(r, S)$$$ is $$$5$$$ and the corresponding subtree is $$${2, 3, 4, 6, 7}$$$. $$$r = 1$$$, $$$S = {5, 4}$$$. The value of $$$f(r, S)$$$ is $$$7$$$ and the corresponding subtree is $$${1, 2, 3, 4, 5, 6, 7}$$$. $$$r = 1$$$, $$$S = {4, 6}$$$. The value of $$$f(r, S)$$$ is $$$3$$$ and the corresponding subtree is $$${4, 6, 7}$$$. | 2,500 | true | false | false | true | false | false | false | false | false | false | 2,131 |
402A | You have _a_ nuts and lots of boxes. The boxes have a wonderful feature: if you put _x_ (_x_u2009≥u20090) divisors (the spacial bars that can divide a box) to it, you get a box, divided into _x_u2009+u20091 sections. You are minimalist. Therefore, on the one hand, you are against dividing some box into more than _k_ sections. On the other hand, you are against putting more than _v_ nuts into some section of the box. What is the minimum number of boxes you have to use if you want to put all the nuts in boxes, and you have _b_ divisors? Please note that you need to minimize the number of used boxes, not sections. You do not have to minimize the number of used divisors. Input The first line contains four space-separated integers _k_, _a_, _b_, _v_ (2u2009≤u2009_k_u2009≤u20091000; 1u2009≤u2009_a_,u2009_b_,u2009_v_u2009≤u20091000) — the maximum number of sections in the box, the number of nuts, the number of divisors and the capacity of each section of the box. Output Print a single integer — the answer to the problem. Note In the first sample you can act like this: Put two divisors to the first box. Now the first box has three sections and we can put three nuts into each section. Overall, the first box will have nine nuts. Do not put any divisors into the second box. Thus, the second box has one section for the last nut. In the end we've put all the ten nuts into boxes. The second sample is different as we have exactly one divisor and we put it to the first box. The next two boxes will have one section each. | 1,100 | true | true | false | false | false | false | false | false | false | false | 8,217 |
1580F | Problem - 1580F - 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 fft math *3300 No tag edit access → Contest materials , and $$$a_1+a_n<m$$$, where $$$m$$$ is a fixed integer. XYMXYM wants to know how many plans of the difficulty of the problems there are modulo $$$998,244,353$$$. Two plans of difficulty $$$a$$$ and $$$b$$$ are different only if there is an integer $$$i$$$ ($$$1 leq i leq n$$$) satisfying $$$a_i eq b_i$$$. Input A single line contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 50,000$$$, $$$1 leq m leq 10^9$$$). Output Print a single integerxa0— the number of different plans. Examples Input 3 2 Output 4 Input 5 9 Output 8105 Input 21038 3942834 Output 338529212 Note In the first test case, the valid $$$a$$$ are: $$$ | 3,300 | true | false | false | false | false | false | false | false | false | false | 2,728 |
1379C | Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly $$$n$$$ flowers. Vladimir went to a flower shop, and he was amazed to see that there are $$$m$$$ types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the $$$i$$$-th type happiness of his wife increases by $$$a_i$$$ and after receiving each consecutive flower of this type her happiness increases by $$$b_i$$$. That is, if among the chosen flowers there are $$$x_i > 0$$$ flowers of type $$$i$$$, his wife gets $$$a_i + (x_i - 1) cdot b_i$$$ additional happiness (and if there are no flowers of type $$$i$$$, she gets nothing for this particular type). Please help Vladimir to choose exactly $$$n$$$ flowers to maximize the total happiness of his wife. Input The first line contains the only integer $$$t$$$ ($$$1 leq t leq 10,000$$$), the number of test cases. It is followed by $$$t$$$ descriptions of the test cases. Each test case description starts with two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^9$$$, $$$1 le m le 100,000$$$), the number of flowers Vladimir needs to choose and the number of types of available flowers. The following $$$m$$$ lines describe the types of flowers: each line contains integers $$$a_i$$$ and $$$b_i$$$ ($$$0 le a_i, b_i le 10^9$$$) for $$$i$$$-th available type of flowers. The test cases are separated by a blank line. It is guaranteed that the sum of values $$$m$$$ among all test cases does not exceed $$$100,000$$$. Output For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly $$$n$$$ flowers optimally. Example Input 2 4 3 5 0 1 4 2 2 5 3 5 2 4 2 3 1 Note In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals $$$5 + (1 + 2 cdot 4) = 14$$$. In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals $$$(5 + 1 cdot 2) + (4 + 1 cdot 2) + 3 = 16$$$. | 2,000 | false | true | false | true | true | false | true | true | true | false | 3,776 |
1209E1 | This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular $$$n imes m$$$ matrix $$$a$$$. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $$$i$$$-th row it is equal $$$r_i$$$. What is the maximal possible value of $$$r_1+r_2+ldots+r_n$$$? Input The first line contains an integer $$$t$$$ ($$$1 le t le 40$$$), the number of test cases in the input. The first line of each test case contains integers $$$n$$$ and $$$m$$$ ($$$1 le n le 4$$$, $$$1 le m le 100$$$) — the number of rows and the number of columns in the given matrix $$$a$$$. Each of the following $$$n$$$ lines contains $$$m$$$ integers, the elements of $$$a$$$ ($$$1 le a_{i, j} le 10^5$$$). Output Print $$$t$$$ integers: answers for all test cases in the order they are given in the input. Example Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Note In the first test case, you can shift the third column down by one, this way there will be $$$r_1 = 5$$$ and $$$r_2 = 7$$$. In the second case you can don't rotate anything at all, this way there will be $$$r_1 = r_2 = 10$$$ and $$$r_3 = 9$$$. | 2,000 | false | true | false | true | false | false | true | false | true | false | 4,642 |
490E | Peter wrote on the board a strictly increasing sequence of positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit. Restore the the original sequence knowing digits remaining on the board. Input The first line of the input contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the length of the sequence. Next _n_ lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive. Output If the answer exists, print in the first line "YES" (without the quotes). Next _n_ lines must contain the sequence of positive integers — a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes). Examples Input 5 12224 12??5 12226 ?0000 ?00000 Output YES 12224 12225 12226 20000 100000 | 2,000 | false | true | true | false | false | false | true | true | false | false | 7,873 |
935A | Fafa owns a company that works on huge projects. There are _n_ employees in Fafa's company. Whenever the company has a new project to start working on, Fafa has to divide the tasks of this project among all the employees. Fafa finds doing this every time is very tiring for him. So, he decided to choose the best _l_ employees in his company as team leaders. Whenever there is a new project, Fafa will divide the tasks among only the team leaders and each team leader will be responsible of some positive number of employees to give them the tasks. To make this process fair for the team leaders, each one of them should be responsible for the same number of employees. Moreover, every employee, who is not a team leader, has to be under the responsibility of exactly one team leader, and no team leader is responsible for another team leader. Given the number of employees _n_, find in how many ways Fafa could choose the number of team leaders _l_ in such a way that it is possible to divide employees between them evenly. Input The input consists of a single line containing a positive integer _n_ (2u2009≤u2009_n_u2009≤u2009105) — the number of employees in Fafa's company. Output Print a single integer representing the answer to the problem. Note In the second sample Fafa has 3 ways: choose only 1 employee as a team leader with 9 employees under his responsibility. choose 2 employees as team leaders with 4 employees under the responsibility of each of them. choose 5 employees as team leaders with 1 employee under the responsibility of each of them. | 800 | false | false | true | false | false | false | true | false | false | false | 5,964 |
1138A | Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers $$$n$$$ pieces of sushi aligned in a row, and a customer has to choose a continuous subsegment of these sushi to buy. The pieces of sushi are of two types: either with tuna or with eel. Let's denote the type of the $$$i$$$-th from the left sushi as $$$t_i$$$, where $$$t_i = 1$$$ means it is with tuna, and $$$t_i = 2$$$ means it is with eel. Arkady does not like tuna, Anna does not like eel. Arkady wants to choose such a continuous subsegment of sushi that it has equal number of sushi of each type and each half of the subsegment has only sushi of one type. For example, subsegment $$$[2, 2, 2, 1, 1, 1]$$$ is valid, but subsegment $$$[1, 2, 1, 2, 1, 2]$$$ is not, because both halves contain both types of sushi. Find the length of the longest continuous subsegment of sushi Arkady can buy. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 100,000$$$)xa0— the number of pieces of sushi. The second line contains $$$n$$$ integers $$$t_1$$$, $$$t_2$$$, ..., $$$t_n$$$ ($$$t_i = 1$$$, denoting a sushi with tuna or $$$t_i = 2$$$, denoting a sushi with eel), representing the types of sushi from left to right. It is guaranteed that there is at least one piece of sushi of each type. Note that it means that there is at least one valid continuous segment. Output Print a single integerxa0— the maximum length of a valid continuous segment. Note In the first example Arkady can choose the subsegment $$$[2, 2, 1, 1]$$$ or the subsegment $$$[1, 1, 2, 2]$$$ with length $$$4$$$. In the second example there is no way but to choose one of the subsegments $$$[2, 1]$$$ or $$$[1, 2]$$$ with length $$$2$$$. In the third example Arkady's best choice is the subsegment $$$[1, 1, 1, 2, 2, 2]$$$. | 900 | false | true | true | false | false | false | false | true | false | false | 5,036 |
770C | Now you can take online courses in the Berland State University! Polycarp needs to pass _k_ main online courses of his specialty to get a diploma. In total _n_ courses are availiable for the passage. The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation). Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses. Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once. Input The first line contains _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009105) — the number of online-courses and the number of main courses of Polycarp's specialty. The second line contains _k_ distinct integers from 1 to _n_ — numbers of main online-courses of Polycarp's specialty. Then _n_ lines follow, each of them describes the next course: the _i_-th of them corresponds to the course _i_. Each line starts from the integer _t__i_ (0u2009≤u2009_t__i_u2009≤u2009_n_u2009-u20091) — the number of courses on which the _i_-th depends. Then there follows the sequence of _t__i_ distinct integers from 1 to _n_ — numbers of courses in random order, on which the _i_-th depends. It is guaranteed that no course can depend on itself. It is guaranteed that the sum of all values _t__i_ doesn't exceed 105. Output Print -1, if there is no the way to get a specialty. Otherwise, in the first line print the integer _m_ — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print _m_ distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them. Examples Input 6 2 5 3 0 0 0 2 2 1 1 4 1 5 Input 9 3 3 9 5 0 0 3 9 4 5 0 0 1 8 1 6 1 2 2 1 2 Note In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course. | 1,500 | false | false | true | false | false | false | false | false | false | true | 6,700 |
1998D | MOOOOOOOOOOOOOOOOO — Bessie the Cow, The Art of Racing on Islands Two of Farmer John's cows, Bessie and Elsie, are planning to race on $$$n$$$ islands. There are $$$n - 1$$$ main bridges, connecting island $$$i$$$ to island $$$i + 1$$$ for all $$$1 leq i leq n - 1$$$. Additionally, there are $$$m$$$ alternative bridges. Elsie can use both main and alternative bridges, while Bessie can only use main bridges. All bridges are one way and can only be used to travel from an island with a lower index to an island with a higher index. Initially, Elsie starts on island $$$1$$$, and Bessie starts on island $$$s$$$. The cows alternate turns, with Bessie making the first turn. Suppose the cow is on island $$$i$$$. During a cow's turn, if there are any bridges connecting island $$$i$$$ to island $$$j$$$, then the cow can move to island $$$j$$$. Then, island $$$i$$$ collapses, and all bridges connecting to island $$$i$$$ also collapse. Also, note the following: If there are no bridges connecting island $$$i$$$ to another island, then island $$$i$$$ collapses, and this cow is eliminated from the race. If the other cow is also on island $$$i$$$, then after this cow moves to another island, the island collapses, and the other cow is eliminated from the race. After an island or bridge collapses, no cows may use them. If a cow is eliminated, their turn is skipped for the rest of the race. The race ends once either cow reaches island $$$n$$$. It can be shown that regardless of the cows' strategies, at least one cow reaches island $$$n$$$. Bessie wins if and only if she reaches island $$$n$$$ first. For each $$$1 leq s leq n - 1$$$, determine whether Bessie wins if she starts the race on island $$$s$$$. Assume both cows follow optimal strategies to ensure their own respective victories. Input The first line contains $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0– the number of test cases. The first line of each test case contains $$$n$$$ and $$$m$$$ ($$$2 leq n leq 2 cdot 10^5$$$, $$$0 leq m leq 2 cdot 10^5$$$)xa0– the number of islands and the number of alternative bridges. The next $$$m$$$ lines of each test case contain $$$u$$$ and $$$v$$$ ($$$1 leq u < v leq n$$$)xa0– the islands that the alternative bridge connects. It is guaranteed all alternative bridges are distinct, and they do not coincide with the main bridges. It is guaranteed that neither the sum of $$$n$$$ nor the sum of $$$m$$$ over all test cases exceeds $$$2 cdot 10^5$$$. Output For each test case, output a binary string of length $$$n - 1$$$ on a new line. The $$$i$$$'th character is $$$1$$$ if it is possible for Bessie to win if she starts on island $$$i$$$. Otherwise, it is $$$0$$$. Note In the first test case, there are no alternative bridges for Elsie to overtake Bessie and reach island $$$n$$$ first, so Bessie will win on all islands because she always moves first. In the second case, Bessie will lose if she starts on island $$$3$$$ because: Bessie's Turn: Take a main bridge from island $$$3$$$ to island $$$4$$$. Elsie's Turn: Take a main bridge from island $$$1$$$ to island $$$2$$$. Bessie's Turn: Take a main bridge from island $$$4$$$ to island $$$5$$$. Elsie's Turn: Take an alternative bridge from island $$$2$$$ to island $$$6$$$. Elsie reaches island $$$n$$$ first. | 2,100 | false | true | false | true | true | false | false | false | false | true | 274 |
1667D | You are given a tree (connected, undirected, acyclic graph) with $$$n$$$ vertices. Two edges are adjacent if they share exactly one endpoint. In one move you can remove an arbitrary edge, if that edge is adjacent to an even number of remaining edges. Remove all of the edges, or determine that it is impossible. If there are multiple solutions, print any. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the number of vertices in the tree. Then $$$n-1$$$ lines follow. The $$$i$$$-th of them contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 le u_i,v_i le n$$$) the endpoints of the $$$i$$$-th edge. It is guaranteed that the given graph is a tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case print "NO" if it is impossible to remove all the edges. Otherwise print "YES", and in the next $$$n-1$$$ lines print a possible order of the removed edges. For each edge, print its endpoints in any order. Example Input 5 2 1 2 3 1 2 2 3 4 1 2 2 3 3 4 5 1 2 2 3 3 4 3 5 7 1 2 1 3 2 4 2 5 3 6 3 7 Output YES 2 1 NO YES 2 3 3 4 2 1 YES 3 5 2 3 2 1 4 3 NO Note Test case $$$1$$$: it is possible to remove the edge, because it is not adjacent to any other edge. Test case $$$2$$$: both edges are adjacent to exactly one edge, so it is impossible to remove any of them. So the answer is "NO". Test case $$$3$$$: the edge $$$2-3$$$ is adjacent to two other edges. So it is possible to remove it. After that removal it is possible to remove the remaining edges too. | 2,900 | false | false | false | true | false | true | false | false | false | false | 2,270 |
76C | Scientists of planet Olympia are conducting an experiment in mutation of primitive organisms. Genome of organism from this planet can be represented as a string of the first _K_ capital English letters. For each pair of types of genes they assigned _a__i_,u2009_j_ — a risk of disease occurence in the organism provided that genes of these types are adjacent in the genome, where _i_ — the 1-based index of the first gene and _j_ — the index of the second gene. The gene 'A' has index 1, 'B' has index 2 and so on. For example, _a_3,u20092 stands for the risk of 'CB' fragment. Risk of disease occurence in the organism is equal to the sum of risks for each pair of adjacent genes in the genome. Scientists have already obtained a base organism. Some new organisms can be obtained by mutation of this organism. Mutation involves removal of all genes of some particular types. Such removal increases the total risk of disease occurence additionally. For each type of genes scientists determined _t__i_ — the increasement of the total risk of disease occurence provided by removal of all genes having type with index _i_. For example, _t_4 stands for the value of additional total risk increasement in case of removing all the 'D' genes. Scientists want to find a number of different organisms that can be obtained from the given one which have the total risk of disease occurence not greater than _T_. They can use only the process of mutation described above. Two organisms are considered different if strings representing their genomes are different. Genome should contain at least one gene. Input The first line of the input contains three integer numbers _N_ (1u2009≤u2009_N_u2009≤u2009200u2009000) — length of the genome of base organism, _K_ (1u2009≤u2009_K_u2009≤u200922) — the maximal index of gene type in the genome and _T_ (1u2009≤u2009_T_u2009≤u20092·109) — maximal allowable risk of disease occurence. The second line contains the genome of the given organism. It is a string of the first _K_ capital English letters having length _N_. The third line contains _K_ numbers _t_1,u2009_t_2,u2009...,u2009_t__K_, where _t__i_ is additional risk value of disease occurence provided by removing of all genes of the _i_-th type. The following _K_ lines contain the elements of the given matrix _a__i_,u2009_j_. The _i_-th line contains _K_ numbers. The _j_-th number of the _i_-th line stands for a risk of disease occurence for the pair of genes, first of which corresponds to the _i_-th letter and second of which corresponds to the _j_-th letter. The given matrix is not necessarily symmetrical. All the numbers in the input are integer, non-negative and all of them except _T_ are not greater than 109. It is guaranteed that the maximal possible risk of organism that can be obtained from the given organism is strictly smaller than 231. Output Output the number of organisms that can be obtained from the base one and which have the total risk of disease occurence not greater than _T_. Examples Input 5 3 13 BACAC 4 1 2 1 2 3 2 3 4 3 4 10 Note Explanation: one can obtain the following organisms (risks are stated in brackets): BACAC (11), ACAC (10), BAA (5), B (6), AA (4). | 2,700 | true | false | false | true | false | false | false | false | false | false | 9,584 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.