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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1340E | Unfortunately, a mistake was found in the proof of the author's solution to this problem. Currently, we don't know the absolutely correct solution. However, you can solve this task, but if your solution passes all the tests, it is not guaranteed to be correct. If your solution has passed all the tests and you are sure that it is correct, you can write to one of the contest authors about it. Surely you all read the book "Alice in Wonderland". In this task, Nastya got to the country of Three strange Bees. The bees are strange because their honeycombs are pentagonal. Nastya got there illegally, so she wants bees not to catch her. Help the bees punish the intruder! This is an interactive problem. A beehive is a connected undirected graph where bees and Nastya can move along the edges. A graph satisfies two properties: The degree of any of its vertex is no more than $$$3$$$. For each edge, there exists a cycle of length not greater than $$$5$$$ passing through this edge. There are three bees and Nastya. You play for bees. Firstly, you choose the vertices where you put the bees. Then Nastya chooses another vertex in which she will initially appear. One move is first moving the bees, then Nastya, in turn: 1. For each of your bees, you can either move each one along some edge from the vertex they are currently staying or leave it in place. 2. Then Nastya will necessarily move along some edge of the graph from the vertex she is currently staying/. You win if at least one of the bees and Nastya are in the same vertex at any time of the game. If this situation does not occur after $$$n$$$ moves, then you lose. Several bees can be in the same vertex. Input The first line contains two integers $$$n$$$ $$$(4 leq n leq 5000)$$$ and $$$m$$$ $$$(n leq m leq 3n)$$$ xa0— the number of vertices and edges in the graph. Each of the next $$$m$$$ lines contains two integers $$$v$$$ and $$$u$$$ $$$(1 leq v, u leq n)$$$, which mean that there is an edge between the vertices $$$v$$$ and $$$u$$$. It is guaranteed that the graph is connected, does not contain loops that the degree of any vertex does not exceed $$$3$$$ and a cycle of length no more than $$$5$$$ passes through each edge. Note that the graph may contain multiple edges. Interaction At each turn, you must output exactly three vertices $$$a, b, c$$$ $$$(1 leq a, b, c leq n)$$$. For the first time, $$$3$$$ vertices displayed will indicate which vertices you originally placed bees on. In response, you will receive the vertex where the jury placed Nastya. Each next $$$3$$$ vertices will indicate where the $$$3$$$ bees move at your turn. Each of the bees can, regardless of other bees, both remain at the current vertex and move along the edge. After the next output of $$$3$$$ vertices, in response, you get the number of the new vertex to which Nastya went. As soon as one of the bees is at the same vertex with Nastya or you have reached the limit on the number of moves, your program should stop working. That is if you made a move, and one of the bees ended up at the same vertex with Nastya, your program must stop working, or if Nastya made a move and ended up at the same vertex with one of the bees, you should not make your move and the program should stop working. If the number of moves exceeds limit ($$$n$$$, where $$$n$$$ is the number of vertices), you will get the Wrong Answer verdict. Your solution may receive the verdict Idleness Limit Exceeded if you don't output anything or forget to flush the output buffer. To flush the output buffer, you need to do the following immediately after printing the query and the line end: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; for other languages see documentation. In this problem interactor is adaptive. This means that depending on all your previous moves, Nastya's behavior may change. Hacks are not available for this problem. Examples Input 5 5 1 2 2 3 3 4 4 5 5 1 4 5 Input 8 9 1 2 2 3 3 4 4 5 5 1 5 6 6 7 7 8 8 4 1 5 Note Let Nastya be a green chip, and three numbered red ones are three bees. In the first test, the movement of the heroes looks like this. After selecting the starting vertices. The first move after the bees move. The first move after the Nastya's move. The first bee caught Nastya. In the second test, the movement of the heroes looks like this. After selecting the starting vertices. The first move after the bees move. The first move after the Nastya's move. The second move after the bees move. The first bee caught Nastya. | 3,000 | false | false | false | false | false | false | false | false | false | true | 4,003 |
516D | Drazil and Varda are the earthworm couple. They want to find a good place to bring up their children. They found a good ground containing nature hole. The hole contains many rooms, some pairs of rooms are connected by small tunnels such that earthworm can move between them. Let's consider rooms and small tunnels as the vertices and edges in a graph. This graph is a tree. In the other words, any pair of vertices has an unique path between them. Each room that is leaf in the graph is connected with a ground by a vertical tunnel. Here, leaf is a vertex that has only one outgoing edge in the graph. Each room is large enough only to fit one earthworm living in it. Earthworm can't live in a tunnel. Drazil and Varda have a plan to educate their children. They want all their children to do morning exercises immediately after getting up! When the morning is coming, all earthworm children get up in the same time, then each of them chooses the farthest path to the ground for gathering with others (these children are lazy, so they all want to do exercises as late as possible). Drazil and Varda want the difference between the time first earthworm child arrives outside and the time the last earthworm child arrives outside to be not larger than _l_ (otherwise children will spread around the ground and it will be hard to keep them exercising together). Also, The rooms that are occupied by their children should form a connected set. In the other words, for any two rooms that are occupied with earthworm children, all rooms that lie on the path between them should be occupied with earthworm children too. How many children Drazil and Varda may have at most in order to satisfy all conditions above? Drazil and Varda want to know the answer for many different choices of _l_. (Drazil and Varda don't live in the hole with their children) Input The first line contains one integer _n_ denoting how many rooms there are in the hole (2u2009≤u2009_n_u2009≤u2009105). Then there are _n_u2009-u20091 lines following. Each of these lines contains three integers _x_, _y_, _v_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_, 1u2009≤u2009_v_u2009≤u2009106) denoting there is a small tunnel between room _x_ and room _y_ that takes time _v_ to pass. Suppose that the time for an earthworm to get out to the ground from any leaf room is the same. The next line contains an integer _q_ (1u2009≤u2009_q_u2009≤u200950), denoting the number of different value of _l_ you need to process. The last line contains _q_ numbers, each number denoting a value of _l_ (1u2009≤u2009_l_u2009≤u20091011). Output You should print _q_ lines. Each line should contain one integer denoting the answer for a corresponding value of _l_. Examples Input 5 1 2 3 2 3 4 4 5 3 3 4 2 5 1 2 3 4 5 Input 12 5 9 3 2 1 7 11 7 2 6 5 5 2 5 3 6 7 2 1 4 4 8 5 7 1 3 8 11 12 3 10 8 2 10 13 14 14 13 13 4 6 7 2 1 Note For the first sample the hole looks like the following. Rooms 1 and 5 are leaves, so they contain a vertical tunnel connecting them to the ground. The lengths of farthest path from rooms 1u2009–u20095 to the ground are 12,u20099,u20097,u20099,u200912 respectively. If l = 1, we may only choose any single room. If l = 2..4, we may choose rooms 2, 3, and 4 as the answer. If l = 5, we may choose all rooms. | 2,800 | false | false | false | true | false | false | false | false | false | false | 7,770 |
148E | During her tantrums the princess usually smashes some collectable porcelain. Every furious shriek is accompanied with one item smashed. The collection of porcelain is arranged neatly on _n_ shelves. Within each shelf the items are placed in one row, so that one can access only the outermost items — the leftmost or the rightmost item, not the ones in the middle of the shelf. Once an item is taken, the next item on that side of the shelf can be accessed (see example). Once an item is taken, it can't be returned to the shelves. You are given the values of all items. Your task is to find the maximal damage the princess' tantrum of _m_ shrieks can inflict on the collection of porcelain. Input The first line of input data contains two integers _n_ (1u2009≤u2009_n_u2009≤u2009100) and _m_ (1u2009≤u2009_m_u2009≤u200910000). The next _n_ lines contain the values of the items on the shelves: the first number gives the number of items on this shelf (an integer between 1 and 100, inclusive), followed by the values of the items (integers between 1 and 100, inclusive), in the order in which they appear on the shelf (the first number corresponds to the leftmost item, the last one — to the rightmost one). The total number of items is guaranteed to be at least _m_. Output Output the maximal total value of a tantrum of _m_ shrieks. Note In the first case there are two shelves, each with three items. To maximize the total value of the items chosen, one can take two items from the left side of the first shelf and one item from the right side of the second shelf. In the second case there is only one shelf, so all three items are taken from it — two from the left side and one from the right side. | 1,900 | false | false | false | true | false | false | false | false | false | false | 9,282 |
810B | Summer holidays! Someone is going on trips, someone is visiting grandparents, but someone is trying to get a part-time job. This summer Noora decided that she wants to earn some money, and took a job in a shop as an assistant. Shop, where Noora is working, has a plan on the following _n_ days. For each day sales manager knows exactly, that in _i_-th day _k__i_ products will be put up for sale and exactly _l__i_ clients will come to the shop that day. Also, the manager is sure, that everyone, who comes to the shop, buys exactly one product or, if there aren't any left, leaves the shop without buying anything. Moreover, due to the short shelf-life of the products, manager established the following rule: if some part of the products left on the shelves at the end of the day, that products aren't kept on the next day and are sent to the dump. For advertising purposes manager offered to start a sell-out in the shop. He asked Noora to choose any _f_ days from _n_ next for sell-outs. On each of _f_ chosen days the number of products were put up for sale would be doubled. Thus, if on _i_-th day shop planned to put up for sale _k__i_ products and Noora has chosen this day for sell-out, shelves of the shop would keep 2·_k__i_ products. Consequently, there is an opportunity to sell two times more products on days of sell-out. Noora's task is to choose _f_ days to maximize total number of sold products. She asks you to help her with such a difficult problem. Input The first line contains two integers _n_ and _f_ (1u2009≤u2009_n_u2009≤u2009105,u20090u2009≤u2009_f_u2009≤u2009_n_) denoting the number of days in shop's plan and the number of days that Noora has to choose for sell-out. Each line of the following _n_ subsequent lines contains two integers _k__i_,u2009_l__i_ (0u2009≤u2009_k__i_,u2009_l__i_u2009≤u2009109) denoting the number of products on the shelves of the shop on the _i_-th day and the number of clients that will come to the shop on _i_-th day. Note In the first example we can choose days with numbers 2 and 4 for sell-out. In this case new numbers of products for sale would be equal to [2,u20096,u20092,u20092] respectively. So on the first day shop will sell 1 product, on the secondxa0— 5, on the thirdxa0— 2, on the fourthxa0— 2. In total 1u2009+u20095u2009+u20092u2009+u20092u2009=u200910 product units. In the second example it is possible to sell 5 products, if you choose third day for sell-out. | 1,300 | false | true | false | false | false | false | false | false | true | false | 6,522 |
1452D | There are $$$n + 2$$$ towns located on a coordinate line, numbered from $$$0$$$ to $$$n + 1$$$. The $$$i$$$-th town is located at the point $$$i$$$. You build a radio tower in each of the towns $$$1, 2, dots, n$$$ with probability $$$frac{1}{2}$$$ (these events are independent). After that, you want to set the signal power on each tower to some integer from $$$1$$$ to $$$n$$$ (signal powers are not necessarily the same, but also not necessarily different). The signal from a tower located in a town $$$i$$$ with signal power $$$p$$$ reaches every city $$$c$$$ such that $$$c - i < p$$$. After building the towers, you want to choose signal powers in such a way that: towns $$$0$$$ and $$$n + 1$$$ don't get any signal from the radio towers; towns $$$1, 2, dots, n$$$ get signal from exactly one radio tower each. For example, if $$$n = 5$$$, and you have built the towers in towns $$$2$$$, $$$4$$$ and $$$5$$$, you may set the signal power of the tower in town $$$2$$$ to $$$2$$$, and the signal power of the towers in towns $$$4$$$ and $$$5$$$ to $$$1$$$. That way, towns $$$0$$$ and $$$n + 1$$$ don't get the signal from any tower, towns $$$1$$$, $$$2$$$ and $$$3$$$ get the signal from the tower in town $$$2$$$, town $$$4$$$ gets the signal from the tower in town $$$4$$$, and town $$$5$$$ gets the signal from the tower in town $$$5$$$. Calculate the probability that, after building the towers, you will have a way to set signal powers to meet all constraints. Output Print one integer — the probability that there will be a way to set signal powers so that all constraints are met, taken modulo $$$998244353$$$. Formally, the probability can be expressed as an irreducible fraction $$$frac{x}{y}$$$. You have to print the value of $$$x cdot y^{-1} bmod 998244353$$$, where $$$y^{-1}$$$ is an integer such that $$$y cdot y^{-1} bmod 998244353 = 1$$$. Note The real answer for the first example is $$$frac{1}{4}$$$: with probability $$$frac{1}{4}$$$, the towers are built in both towns $$$1$$$ and $$$2$$$, so we can set their signal powers to $$$1$$$. The real answer for the second example is $$$frac{1}{4}$$$: with probability $$$frac{1}{8}$$$, the towers are built in towns $$$1$$$, $$$2$$$ and $$$3$$$, so we can set their signal powers to $$$1$$$; with probability $$$frac{1}{8}$$$, only one tower in town $$$2$$$ is built, and we can set its signal power to $$$2$$$. The real answer for the third example is $$$frac{5}{32}$$$. Note that even though the previous explanations used equal signal powers for all towers, it is not necessarily so. For example, if $$$n = 5$$$ and the towers are built in towns $$$2$$$, $$$4$$$ and $$$5$$$, you may set the signal power of the tower in town $$$2$$$ to $$$2$$$, and the signal power of the towers in towns $$$4$$$ and $$$5$$$ to $$$1$$$. | 1,600 | true | false | false | true | false | false | false | false | false | false | 3,418 |
2028C | Alice is at the Mad Hatter's tea party! There is a long sheet cake made up of $$$n$$$ sections with tastiness values $$$a_1, a_2, ldots, a_n$$$. There are $$$m$$$ creatures at the tea party, excluding Alice. Alice will cut the cake into $$$m + 1$$$ pieces. Formally, she will partition the cake into $$$m + 1$$$ subarrays, where each subarray consists of some number of adjacent sections. The tastiness of a piece is the sum of tastiness of its sections. Afterwards, she will divvy these $$$m + 1$$$ pieces up among the $$$m$$$ creatures and herself (her piece can be empty). However, each of the $$$m$$$ creatures will only be happy when the tastiness of its piece is $$$v$$$ or more. Alice wants to make sure every creature is happy. Limited by this condition, she also wants to maximize the tastiness of her own piece. Can you help Alice find the maximum tastiness her piece can have? If there is no way to make sure every creature is happy, output $$$-1$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n, m, v$$$ ($$$1le mle nle 2cdot 10^5$$$; $$$1le vle 10^9$$$) — the number of sections, the number of creatures, and the creatures' minimum requirement for tastiness, respectively. The next line contains $$$n$$$ space separated integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$) — the tastinesses of the sections. The sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output the maximum tastiness Alice can achieve for her piece, or $$$-1$$$ if there is no way to make sure every creature is happy. Example Input 7 6 2 1 1 1 10 1 1 10 6 2 2 1 1 10 1 1 10 6 2 3 1 1 10 1 1 10 6 2 10 1 1 10 1 1 10 6 2 11 1 1 10 1 1 10 6 2 12 1 1 10 1 1 10 6 2 12 1 1 1 1 10 10 Note For the first test case, Alice can give the first and second section as their own pieces, and then take the remaining $$$10 + 1 + 1 + 10 = 22$$$ tastiness for herself. We can show that she cannot do any better. For the second test case, Alice could give the first and second section as one piece, and the sixth section as one piece. She can then take the remaining $$$10 + 1 + 1 = 12$$$ tastiness for herself. We can show that she cannot do any better. For the seventh test case, Alice cannot give each creature a piece of at least $$$12$$$ tastiness. | 1,600 | false | true | false | true | false | false | false | true | false | false | 101 |
750A | Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be _n_ problems, sorted by difficulty, i.e. problem 1 is the easiest and problem _n_ is the hardest. Limak knows it will take him 5·_i_ minutes to solve the _i_-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs _k_ minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u200910, 1u2009≤u2009_k_u2009≤u2009240)xa0— the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5u2009+u200910u2009=u200915 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5u2009+u200910u2009+u200915u2009+u200920u2009=u200950 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. | 800 | true | false | true | false | false | false | true | true | false | false | 6,793 |
1016E | There is a light source on the plane. This source is so small that it can be represented as point. The light source is moving from point $$$(a, s_y)$$$ to the $$$(b, s_y)$$$ $$$(s_y < 0)$$$ with speed equal to $$$1$$$ unit per second. The trajectory of this light source is a straight segment connecting these two points. There is also a fence on $$$OX$$$ axis represented as $$$n$$$ segments $$$(l_i, r_i)$$$ (so the actual coordinates of endpoints of each segment are $$$(l_i, 0)$$$ and $$$(r_i, 0)$$$). The point $$$(x, y)$$$ is in the shade if segment connecting $$$(x,y)$$$ and the current position of the light source intersects or touches with any segment of the fence. You are given $$$q$$$ points. For each point calculate total time of this point being in the shade, while the light source is moving from $$$(a, s_y)$$$ to the $$$(b, s_y)$$$. Input First line contains three space separated integers $$$s_y$$$, $$$a$$$ and $$$b$$$ ($$$-10^9 le s_y < 0$$$, $$$1 le a < b le 10^9$$$) — corresponding coordinates of the light source. Second line contains single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — number of segments in the fence. Next $$$n$$$ lines contain two integers per line: $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i < r_i le 10^9$$$, $$$r_{i - 1} < l_i$$$) — segments in the fence in increasing order. Segments don't intersect or touch each other. Next line contains single integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$) — number of points to check. Next $$$q$$$ lines contain two integers per line: $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le 10^9$$$) — points to process. Output Print $$$q$$$ lines. The $$$i$$$-th line should contain one real number — total time of the $$$i$$$-th point being in the shade, while the light source is moving from $$$(a, s_y)$$$ to the $$$(b, s_y)$$$. The answer is considered as correct if its absolute of relative error doesn't exceed $$$10^{-6}$$$. Example Input -3 1 6 2 2 4 6 7 5 3 1 1 3 6 1 6 4 7 6 Output 5.000000000000000 3.000000000000000 0.000000000000000 1.500000000000000 2.000000000000000 Note The 1-st point is always in the shade; the 2-nd point is in the shade while light source is moving from $$$(3, -3)$$$ to $$$(6, -3)$$$; the 3-rd point is in the shade while light source is at point $$$(6, -3)$$$. the 4-th point is in the shade while light source is moving from $$$(1, -3)$$$ to $$$(2.5, -3)$$$ and at point $$$(6, -3)$$$; the 5-th point is in the shade while light source is moving from $$$(1, -3)$$$ to $$$(2.5, -3)$$$ and from $$$(5.5, -3)$$$ to $$$(6, -3)$$$; | 2,400 | false | false | false | false | false | false | false | true | false | false | 5,608 |
1775B | The gardener Kazimir Kazimirovich has an array of $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$. He wants to check if there are two different subsequences $$$a$$$ and $$$b$$$ of the original array, for which $$$f(a) = f(b)$$$, where $$$f(x)$$$ is the elements. Two subsequences are considered different if the sets of indexes of their elements in the original sequence are different, that is, the values of the elements are not considered when comparing the subsequences. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the size of the array $$$c$$$. The description of the array $$$c$$$ in this problem is given implicitly to speed up input. The $$$(i + 1)$$$-st of the following $$$n$$$ lines of the test case begins with an integer $$$k_i$$$ ($$$1 le k_i le 10^5$$$)xa0— the number of set bits in the number $$$c_i$$$. Next follow $$$k_i$$$ distinct integers $$$p_{i, 1}, p_{i, 2}, dots, p_{i, k_i}$$$ ($$$1 le p_i le 2 cdot 10^5$$$)xa0—the numbers of bits that are set to one in number $$$c_i$$$. In other words, $$$c_i = 2^{p_{i, 1}} + 2^{p_{i, 2}} + ldots + 2^{p_{i, k_i}}$$$. It is guaranteed that the total sum of $$$k_i$$$ in all tests does not exceed $$$10^5$$$. Output For each set of input, print "Yes" if there exist two different subsequences for which $$$f(a) = f(b)$$$, and "No" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 5 3 2 1 5 2 2 4 2 2 3 2 2 1 2 1 2 4 3 1 2 4 2 2 4 4 1 2 5 6 2 2 5 5 3 3 1 2 3 2 5 3 5 7 2 3 1 4 5 1 2 6 3 5 3 2 6 3 2 1 1 1 2 Note It can be proven that in the first test case there are no two different subsequences $$$a$$$ and $$$b$$$ for which $$$f(a) = f(b)$$$. In the second test case, one of the possible answers are following subsequences: the subsequence $$$a$$$ formed by the element at position $$$1$$$, and the subsequence $$$b$$$ formed by the elements at positions $$$1$$$ and $$$2$$$. In the third test case, one of the possible answers are following subsequences: the subsequence $$$a$$$ formed by elements at positions $$$1$$$, $$$2$$$, $$$3$$$ and $$$4$$$, and the subsequence $$$b$$$ formed by elements at positions $$$2$$$, $$$3$$$ and $$$4$$$. | 1,300 | false | false | false | false | false | true | false | false | false | false | 1,633 |
1371E1 | # Asterism (Easy Version) Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes This is the easy version of the problem. The difference between versions is the constraints on n and ai. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has ai candies. Yuzu is going to determine a permutation P . A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, {2, 3, 1, 5, 4} is a permutation, but {1, 2, 2} is not a permutation (2 appears twice in the array) and {1, 3, 4} is also not a permutation (because n = 3 but there is the number 4 in the array). After that, she will do n duels with the enemies with the following rules: • If Yuzu has equal or more number of candies than enemy Pi, she wins the duel and gets 1 candy .Otherwise, she loses the duel and gets nothing. • The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels . How many valid permutations P exist? This problem was easy and wasn’t interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let’s define f (x) as the number of valid permutations for the integer x.You are given n, a and a prime number p ≤ n. Let’s call a positive integer x good , if the value f (x) is not divisible by p. Find all good integers x.Your task is to solve this problem made by Akari. # Input The first line contains two integers n, p (2 ≤ p ≤ n ≤ 2000) . It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p). The second line contains n integers a1, a 2, . . . , a n (1 ≤ ai ≤ 2000) . # Output In the first line, print the number of good integers x.In the second line, output all good integers x in the ascending order .It is guaranteed that the number of good integers x does not exceed 10 5. # Examples standard input standard output 3 2 3 4 5 134 3 2 3 5 6 23 4 4 3 9 1 1 1 0 Page 1 of 2 Note In the first test, p = 2 . • If x ≤ 2, there are no valid permutations for Yuzu. So f (x) = 0 for all x ≤ 2. The number 0 is divisible by 2, so all integers x ≤ 2 are not good. • If x = 3 , {1, 2, 3} is the only valid permutation for Yuzu. So f (3) = 1 , so the number 3 is good. • If x = 4 , {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1} are all valid permutations for Yuzu. So f (4) = 4 , so the number 4 is not good. • If x ≥ 5, all 6 permutations are valid for Yuzu. So f (x) = 6 for all x ≥ 5, so all integers x ≥ 5 are not good. So, the only good number is 3.In the third test, for all positive integers x the value f (x) is divisible by p = 3 . Page 2 of 2 | 1,900 | true | false | false | false | false | false | true | true | true | false | 3,810 |
799B | A new pack of _n_ t-shirts came to a shop. Each of the t-shirts is characterized by three integers _p__i_, _a__i_ and _b__i_, where _p__i_ is the price of the _i_-th t-shirt, _a__i_ is front color of the _i_-th t-shirt and _b__i_ is back color of the _i_-th t-shirt. All values _p__i_ are distinct, and values _a__i_ and _b__i_ are integers from 1 to 3. _m_ buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the _j_-th buyer we know his favorite color _c__j_. A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served. You are to compute the prices each buyer will pay for t-shirts. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000)xa0— the number of t-shirts. The following line contains sequence of integers _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u20091u2009000u2009000u2009000), where _p__i_ equals to the price of the _i_-th t-shirt. The following line contains sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20093), where _a__i_ equals to the front color of the _i_-th t-shirt. The following line contains sequence of integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009≤u2009_b__i_u2009≤u20093), where _b__i_ equals to the back color of the _i_-th t-shirt. The next line contains single integer _m_ (1u2009≤u2009_m_u2009≤u2009200u2009000)xa0— the number of buyers. The following line contains sequence _c_1,u2009_c_2,u2009...,u2009_c__m_ (1u2009≤u2009_c__j_u2009≤u20093), where _c__j_ equals to the favorite color of the _j_-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served. | 1,400 | false | false | true | false | true | false | false | false | false | false | 6,575 |
1361F | # Johnny and New Toy Input file: standard input Output file: standard output Time limit: 15 seconds Memory limit: 1024 megabytes Johnny has a new toy. As you may guess, it is a little bit extraordinary. The toy is a permutation P of numbers from 1 to n, written in one row next to each other. For each i from 1 to n − 1 between Pi and Pi+1 there is a weight Wi written, and those weights form a permutation of numbers from 1 to n − 1. There are also extra weights W0 = Wn = 0 .The instruction defines subsegment [L, R ] as good if WL−1 < Wi and WR < Wi for any i in {L, L + 1 , . . . , R − 1}. For such subsegment it also defines WM as minimum of set {WL, W L+1 , . . . , W R−1}.Now the fun begins. In one move, the player can choose one of the good subsegments, cut it into [L, M ] and [M + 1 , R ] and swap the two parts. More precisely, before one move the chosen subsegment of our toy looks like: WL−1, P L, W L, . . . , W M −1, P M , W M , P M +1 , W M +1 , . . . , W R−1, P R, W R and afterwards it looks like this: WL−1, P M +1 , W M +1 , . . . , W R−1, P R, W M , P L, W L, . . . , W M −1, P M , W R Such a move can be performed multiple times (possibly zero), and the goal is to achieve the minimum number of inversions in P .Johnny’s younger sister Megan thinks that the rules are too complicated, so she wants to test her brother by choosing some pair of indices X and Y , and swapping PX and PY (X might be equal Y ). After each sister’s swap, Johnny wonders, what is the minimal number of inversions that he can achieve, starting with current P and making legal moves? You can assume that the input is generated randomly . P and W were chosen independently and equiprobably over all permutations; also, Megan’s requests were chosen independently and equiprobably over all pairs of indices. # Input The first line contains single integer n (2 ≤ n ≤ 2 · 10 5) denoting the length of the toy. The second line contains n distinct integers P1, P 2, . . . , P n (1 ≤ Pi ≤ n) denoting the initial permutation P . The third line contains n−1 distinct integers W1, W 2, . . . , W n−1 (1 ≤ Wi ≤ n−1) denoting the weights. The fourth line contains single integer q (1 ≤ q ≤ 5 · 10 4) x16 the number of Megan’s swaps. The following q lines contain two integers X and Y (1 ≤ X, Y ≤ n) x16 the indices of elements of P to swap. The queries aren’t independent; after each of them, the permutation is changed. # Output Output q lines. The i-th line should contain exactly one integer x16 the minimum number of inversions in permutation, which can be obtained by starting with the P after first i queries and making moves described in the game’s instruction. Page 1 of 2 Examples standard input standard output 33 2 1 2 1 31 3 3 2 3 1 01054 3 2 5 1 3 2 1 4 75 4 5 2 1 5 2 4 2 4 4 3 3 3 3121233 # Note Consider the first sample. After the first query, P is sorted, so we already achieved a permutation with no inversions. After the second query, P is equal to [ 1, 3, 2], it has one inversion, it can be proven that it is impossible to achieve 0 inversions. In the end, P is equal to [ 2, 3, 1]; we can make a move on the whole permutation, as it is a good subsegment itself, which results in P equal to [ 1, 2, 3], which has 0 inversions. Page 2 of 2 | 3,300 | true | false | true | false | true | false | false | false | false | false | 3,872 |
289B | Problem - 289B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force dp implementation sortings ternary search *1400 No tag edit access → Contest materials — the matrix sizes and the _d_ parameter. Next _n_ lines contain the matrix: the _j_-th integer in the _i_-th row is the matrix element _a__ij_ (1u2009≤u2009_a__ij_u2009≤u2009104). Output In a single line print a single integer — the minimum number of moves the penguin needs to make all matrix elements equal. If that is impossible, print "-1" (without the quotes). Examples Input 2 2 2 2 4 6 8 Output 4 Input 1 2 7 6 7 Output -1 | 1,400 | false | false | true | true | false | false | true | false | true | false | 8,678 |
771A | Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are _n_ members, numbered 1 through _n_. _m_ pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers _n_ and _m_ (3u2009≤u2009_n_u2009≤u2009150u2009000, )xa0— the number of members and the number of pairs of members that are friends. The _i_-th of the next _m_ lines contains two distinct integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_). Members _a__i_ and _b__i_ are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2,u20093) are friends and members (3,u20094) are friends, while members (2,u20094) are not. | 1,500 | false | false | false | false | false | false | false | false | false | true | 6,698 |
1777C | A school has to decide on its team for an international quiz. There are $$$n$$$ students in the school. We can describe the students using an array $$$a$$$ where $$$a_i$$$ is the smartness of the $$$i$$$-th ($$$1 le i le n$$$) student. There are $$$m$$$ topics $$$1, 2, 3, ldots, m$$$ from which the quiz questions will be formed. The $$$i$$$-th student is considered proficient in a topic $$$T$$$ if $$$(a_i bmod T) = 0$$$. Otherwise, he is a rookie in that topic. We say that a team of students is collectively proficient in all the topics if for every topic there is a member of the team proficient in this topic. Find a team that is collectively proficient in all the topics such that the maximum difference between the smartness of any two students in that team is minimized. Output this difference. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains $$$n$$$ and $$$m$$$ ($$$1 le n,m le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print the answer on a new line. If there is no solution, output $$$-1$$$. Example Input 3 2 4 3 7 4 2 3 7 2 9 5 7 6 4 3 5 7 Note In the first test case, we have participants with smartnesses $$$3$$$ and $$$7$$$, and $$$m = 4$$$. Thus, there is no student with smartness divisible by $$$2$$$. Since $$$2 leq m$$$, there is no way to choose a team. In the second test case, we can select the participant with smartness $$$2$$$ to be the only one on the team. This way the team will be collectively proficient in both topics $$$1$$$ and $$$2$$$. In the third test case, consider the team with participants of smartnesses $$$4, 5, 6, 7$$$. This way the team will be collectively proficient in all topics $$$1, 2, ldots, 7$$$. | 1,700 | true | false | false | false | false | false | false | true | true | false | 1,612 |
1043D | Acingel is a small town. There was only one doctor herexa0— Miss Ada. She was very friendly and nobody has ever said something bad about her, so who could've expected that Ada will be found dead in her house? Mr Gawry, world-famous detective, is appointed to find the criminal. He asked $$$m$$$ neighbours of Ada about clients who have visited her in that unlucky day. Let's number the clients from $$$1$$$ to $$$n$$$. Each neighbour's testimony is a permutation of these numbers, which describes the order in which clients have been seen by the asked neighbour. However, some facts are very suspiciousxa0– how it is that, according to some of given permutations, some client has been seen in the morning, while in others he has been seen in the evening? "In the morning some of neighbours must have been sleeping!"xa0— thinks Gawryxa0— "and in the evening there's been too dark to see somebody's face...". Now he wants to delete some prefix and some suffix (both prefix and suffix can be empty) in each permutation, so that they'll be non-empty and equal to each other after thatxa0— some of the potential criminals may disappear, but the testimony won't stand in contradiction to each other. In how many ways he can do it? Two ways are called different if the remaining common part is different. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 100,000$$$, $$$1 le m le 10$$$)xa0— the number of suspects and the number of asked neighbors. Each of the next $$$m$$$ lines contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$). It is guaranteed that these integers form a correct permutation (that is, each number from $$$1$$$ to $$$n$$$ appears exactly once). Output Output a single integer denoting the number of ways to delete some prefix and some suffix of each permutation (possibly empty), such that the remaining parts will be equal and non-empty. Examples Input 5 6 1 2 3 4 5 2 3 1 4 5 3 4 5 1 2 3 5 4 2 1 2 3 5 4 1 1 2 3 4 5 Note In the first example, all possible common parts are $$$[1]$$$, $$$[2]$$$, $$$[3]$$$ and $$$[2, 3]$$$. In the second and third examples, you can only leave common parts of length $$$1$$$. | 1,700 | true | false | false | false | false | false | true | false | false | false | 5,480 |
138C | One day Natalia was walking in the woods when she met a little mushroom gnome. The gnome told her the following story: Everybody knows that the mushroom gnomes' power lies in the magic mushrooms that grow in the native woods of the gnomes. There are _n_ trees and _m_ magic mushrooms in the woods: the _i_-th tree grows at a point on a straight line with coordinates _a__i_ and has the height of _h__i_, the _j_-th mushroom grows at the point with coordinates _b__j_ and has magical powers _z__j_. But one day wild mushroommunchers, the sworn enemies of mushroom gnomes unleashed a terrible storm on their home forest. As a result, some of the trees began to fall and crush the magic mushrooms. The supreme oracle of mushroom gnomes calculated in advance the probability for each tree that it will fall to the left, to the right or will stand on. If the tree with the coordinate _x_ and height _h_ falls to the left, then all the mushrooms that belong to the right-open interval [_x_u2009-u2009_h_,u2009_x_), are destroyed. If a tree falls to the right, then the mushrooms that belong to the left-open interval (_x_,u2009_x_u2009+u2009_h_] are destroyed. Only those mushrooms that are not hit by a single tree survive. Knowing that all the trees fall independently of each other (i.e., all the events are mutually independent, and besides, the trees do not interfere with other trees falling in an arbitrary direction), the supreme oracle was also able to quickly calculate what would be the expectation of the total power of the mushrooms which survived after the storm. His calculations ultimately saved the mushroom gnomes from imminent death. Natalia, as a good Olympiad programmer, got interested in this story, and she decided to come up with a way to quickly calculate the expectation of the sum of the surviving mushrooms' power. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_m_u2009≤u2009104) — the number of trees and mushrooms, respectively. Each of the next _n_ lines contain four integers — _a__i_, _h__i_, _l__i_, _r__i_ (_a__i_u2009≤u2009109, 1u2009≤u2009_h__i_u2009≤u2009109, 0u2009≤u2009_l__i_,u2009_r__i_,u2009_l__i_u2009+u2009_r__i_u2009≤u2009100) which represent the coordinate of the _i_-th tree, its height, the percentage of the probabilities that the tree falls to the left and to the right, respectively (the remaining percentage is the probability that the tree will stand on). Each of next _m_ lines contain two integers _b__j_, _z__j_ (_b__j_u2009≤u2009109, 1u2009≤u2009_z__j_u2009≤u2009103) which represent the coordinate and the magical power of the _j_-th mushroom, respectively. An arbitrary number of trees and mushrooms can grow in one point. Output Print a real number — the expectation of the total magical power of the surviving mushrooms. The result is accepted with relative or absolute accuracy 10u2009-u20094. Examples Input 2 1 2 2 50 50 4 2 50 50 3 1 Note It is believed that the mushroom with the coordinate _x_ belongs to the right-open interval [_l_,u2009_r_) if and only if _l_u2009≤u2009_x_u2009<u2009_r_. Similarly, the mushroom with the coordinate _x_ belongs to the left-open interval (_l_,u2009_r_] if and only if _l_u2009<u2009_x_u2009≤u2009_r_. In the first test the mushroom survives with the probability of 50%, depending on where the single tree falls. In the second test the mushroom survives only if neither of the two trees falls on it. It occurs with the probability of 50% u2009×u2009 50% = 25%. Pretest №12 is the large test with 105 trees and one mushroom. | 2,200 | false | false | false | false | true | false | false | true | true | false | 9,323 |
1147A | Alice and Bob are playing a game on a line with $$$n$$$ cells. There are $$$n$$$ cells labeled from $$$1$$$ through $$$n$$$. For each $$$i$$$ from $$$1$$$ to $$$n-1$$$, cells $$$i$$$ and $$$i+1$$$ are adjacent. Alice initially has a token on some cell on the line, and Bob tries to guess where it is. Bob guesses a sequence of line cell numbers $$$x_1, x_2, ldots, x_k$$$ in order. In the $$$i$$$-th question, Bob asks Alice if her token is currently on cell $$$x_i$$$. That is, Alice can answer either "YES" or "NO" to each Bob's question. At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions. Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all. You are given $$$n$$$ and Bob's questions $$$x_1, ldots, x_k$$$. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions. Let $$$(a,b)$$$ denote a scenario where Alice starts at cell $$$a$$$ and ends at cell $$$b$$$. Two scenarios $$$(a_i, b_i)$$$ and $$$(a_j, b_j)$$$ are different if $$$a_i eq a_j$$$ or $$$b_i eq b_j$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n,k leq 10^5$$$)xa0— the number of cells and the number of questions Bob asked. The second line contains $$$k$$$ integers $$$x_1, x_2, ldots, x_k$$$ ($$$1 leq x_i leq n$$$)xa0— Bob's questions. Output Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions. Note The notation $$$(i,j)$$$ denotes a scenario where Alice starts at cell $$$i$$$ and ends at cell $$$j$$$. In the first example, the valid scenarios are $$$(1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5)$$$. For example, $$$(3,4)$$$ is valid since Alice can start at cell $$$3$$$, stay there for the first three questions, then move to cell $$$4$$$ after the last question. $$$(4,5)$$$ is valid since Alice can start at cell $$$4$$$, stay there for the first question, the move to cell $$$5$$$ for the next two questions. Note that $$$(4,5)$$$ is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once. In the second example, Alice has no valid scenarios. In the last example, all $$$(i,j)$$$ where $$$i-j leq 1$$$ except for $$$(42, 42)$$$ are valid scenarios. | 1,500 | false | false | false | false | false | false | false | false | false | true | 4,983 |
1215C | Monocarp has got two strings $$$s$$$ and $$$t$$$ having equal length. Both strings consist of lowercase Latin letters "a" and "b". Monocarp wants to make these two strings $$$s$$$ and $$$t$$$ equal to each other. He can do the following operation any number of times: choose an index $$$pos_1$$$ in the string $$$s$$$, choose an index $$$pos_2$$$ in the string $$$t$$$, and swap $$$s_{pos_1}$$$ with $$$t_{pos_2}$$$. You have to determine the minimum number of operations Monocarp has to perform to make $$$s$$$ and $$$t$$$ equal, and print any optimal sequence of operations — or say that it is impossible to make these strings equal. Input The first line contains one integer $$$n$$$ $$$(1 le n le 2 cdot 10^{5})$$$ — the length of $$$s$$$ and $$$t$$$. The second line contains one string $$$s$$$ consisting of $$$n$$$ characters "a" and "b". The third line contains one string $$$t$$$ consisting of $$$n$$$ characters "a" and "b". Output If it is impossible to make these strings equal, print $$$-1$$$. Otherwise, in the first line print $$$k$$$ — the minimum number of operations required to make the strings equal. In each of the next $$$k$$$ lines print two integers — the index in the string $$$s$$$ and the index in the string $$$t$$$ that should be used in the corresponding swap operation. Note In the first example two operations are enough. For example, you can swap the third letter in $$$s$$$ with the third letter in $$$t$$$. Then $$$s = $$$ "abbb", $$$t = $$$ "aaab". Then swap the third letter in $$$s$$$ and the second letter in $$$t$$$. Then both $$$s$$$ and $$$t$$$ are equal to "abab". In the second example it's impossible to make two strings equal. | 1,500 | false | true | false | false | false | true | false | false | false | false | 4,601 |
1358E | Levian works as an accountant in a large company. Levian knows how much the company has earned in each of the $$$n$$$ consecutive months — in the $$$i$$$-th month the company had income equal to $$$a_i$$$ (positive income means profit, negative income means loss, zero income means no change). Because of the general self-isolation, the first $$$lceil frac{n}{2} ceil$$$ months income might have been completely unstable, but then everything stabilized and for the last $$$lfloor frac{n}{2} floor$$$ months the income was the same. Levian decided to tell the directors $$$n-k+1$$$ numbers — the total income of the company for each $$$k$$$ consecutive months. In other words, for each $$$i$$$ between $$$1$$$ and $$$n-k+1$$$ he will say the value $$$a_i + a_{i+1} + ldots + a_{i + k - 1}$$$. For example, if $$$a=[-1, 0, 1, 2, 2]$$$ and $$$k=3$$$ he will say the numbers $$$0, 3, 5$$$. Unfortunately, if at least one total income reported by Levian is not a profit (income $$$le 0$$$), the directors will get angry and fire the failed accountant. Save Levian's career: find any such $$$k$$$, that for each $$$k$$$ months in a row the company had made a profit, or report that it is impossible. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 5cdot 10^5$$$) — the number of months for which Levian must account. The second line contains $$$lceil{frac{n}{2}} ceil$$$ integers $$$a_1, a_2, ldots, a_{lceil{frac{n}{2}} ceil}$$$, where $$$a_i$$$ ($$$-10^9 le a_i le 10^9$$$) — the income of the company in the $$$i$$$-th month. Third line contains a single integer $$$x$$$ ($$$-10^9 le x le 10^9$$$) — income in every month from $$$lceil{frac{n}{2}} ceil + 1$$$ to $$$n$$$. Output In a single line, print the appropriate integer $$$k$$$ or $$$-1$$$, if it does not exist. If there are multiple possible answers, you can print any. Note In the first example, $$$k=2$$$ and $$$k=3$$$ satisfy: in the first case, Levian will report the numbers $$$1, 1$$$, and in the second case — one number $$$3$$$. In the second example, there is no such $$$k$$$. In the third example, the only answer is $$$k=4$$$: he will report the numbers $$$1,2,3$$$. | 2,400 | false | true | true | false | true | true | false | false | false | false | 3,893 |
858A | Problem - 858A - Codeforces =============== xa0 ]( "54580") Editorial") . Output Print the _k_-rounding of _n_. Examples Input 375 4 Output 30000 Input 10000 1 Output 10000 Input 38101 0 Output 38101 Input 123456789 8 Output 12345678900000000 | 1,100 | true | false | false | false | false | false | true | false | false | false | 6,314 |
1679A | Spring has come, and the management of the AvtoBus bus fleet has given the order to replace winter tires with summer tires on all buses. You own a small bus service business and you have just received an order to replace $$$n$$$ tires. You know that the bus fleet owns two types of buses: with two axles (these buses have $$$4$$$ wheels) and with three axles (these buses have $$$6$$$ wheels). You don't know how many buses of which type the AvtoBus bus fleet owns, so you wonder how many buses the fleet might have. You have to determine the minimum and the maximum number of buses that can be in the fleet if you know that the total number of wheels for all buses is $$$n$$$. Input The first line contains an integer $$$t$$$ ($$$1 le t le 1,000$$$)xa0— the number of test cases. The following lines contain description of test cases. The only line of each test case contains one integer $$$n$$$ ($$$1 le n le 10^{18}$$$)xa0— the total number of wheels for all buses. Output For each test case print the answer in a single line using the following format. Print two integers $$$x$$$ and $$$y$$$ ($$$1 le x le y$$$)xa0— the minimum and the maximum possible number of buses that can be in the bus fleet. If there is no suitable number of buses for the given $$$n$$$, print the number $$$-1$$$ as the answer. Example Input 4 4 7 24 998244353998244352 Output 1 1 -1 4 6 166374058999707392 249561088499561088 Note In the first test case the total number of wheels is $$$4$$$. It means that there is the only one bus with two axles in the bus fleet. In the second test case it's easy to show that there is no suitable number of buses with $$$7$$$ wheels in total. In the third test case the total number of wheels is $$$24$$$. The following options are possible: Four buses with three axles. Three buses with two axles and two buses with three axles. Six buses with two axles. So the minimum number of buses is $$$4$$$ and the maximum number of buses is $$$6$$$. | 900 | true | true | false | false | false | false | true | false | false | false | 2,197 |
1945F | As soon as everyone in the camp fell asleep, Kirill sneaked out of the tent and went to the Wise Oak to gather mushrooms. It is known that there are $$$n$$$ mushrooms growing under the Oak, each of which has magic power $$$v_i$$$. Kirill really wants to make a magical elixir of maximum strength from the mushrooms. The strength of the elixir is equal to the product of the number of mushrooms in it and the minimum magic power among these mushrooms. To prepare the elixir, Kirill will sequentially pick one mushroom growing under the Oak. Kirill can gather mushrooms in any order. However, it's not that simple. The Wise Oak informed Kirill of a permutation of numbers $$$p$$$ from $$$1$$$ to $$$n$$$. If Kirill picks only $$$k$$$ mushrooms, then the magic power of all mushrooms with indices $$$p_1, p_2, dots, p_{k - 1}$$$ will become $$$0$$$. Kirill will not use mushrooms with zero magic power to prepare the elixir. Your task is to help Kirill gather mushrooms in such a way that he can brew the elixir of maximum possible strength. However, Kirill is a little scared to stay near the oak for too long, so out of all the suitable options for gathering mushrooms, he asks you to find the one with the minimum number of mushrooms. A permutation of length $$$n$$$ is an array consisting of $$$n$$$ different integers from $$$1$$$ to $$$n$$$ in any order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears in the array twice) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$, but $$$4$$$ appears in the array). Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 200,000$$$)xa0— the number of mushrooms. The second line contains an array $$$v$$$ of size $$$n$$$ ($$$1le v_i le 10^9$$$)xa0— the magic powers of the mushrooms. The third line contains a permutation $$$p$$$ of numbers from $$$1$$$ to $$$n$$$. It is guaranteed that the sum of the values of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Note In the first example, you need to take the mushrooms with indices $$$1$$$ and $$$2$$$, so the strength of the elixir is equal to $$$2 cdot min(a_1, a_2) = 2 cdot min(9, 8) = 2 cdot 8 = 16$$$. Note that the magic power of the mushroom with index $$$3$$$ after picking two mushrooms will become $$$0$$$. | 1,900 | false | false | false | false | true | false | false | false | true | false | 608 |
86D | An array of positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_ is given. Let us consider its arbitrary subarray _a__l_,u2009_a__l_u2009+u20091...,u2009_a__r_, where 1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_. For every positive integer _s_ denote by _K__s_ the number of occurrences of _s_ into the subarray. We call the power of the subarray the sum of products _K__s_·_K__s_·_s_ for every positive integer _s_. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite. You should calculate the power of _t_ given subarrays. Input First line contains two integers _n_ and _t_ (1u2009≤u2009_n_,u2009_t_u2009≤u2009200000) — the array length and the number of queries correspondingly. Second line contains _n_ positive integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009106) — the elements of the array. Next _t_ lines contain two positive integers _l_, _r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_) each — the indices of the left and the right ends of the corresponding subarray. Output Output _t_ lines, the _i_-th line of the output should contain single positive integer — the power of the _i_-th query subarray. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d). Examples Input 8 3 1 1 2 2 1 3 1 1 2 7 1 6 2 7 Note Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored): Then _K_1u2009=u20093, _K_2u2009=u20092, _K_3u2009=u20091, so the power is equal to 32·1u2009+u200922·2u2009+u200912·3u2009=u200920. | 2,200 | true | false | true | false | true | false | false | false | false | false | 9,538 |
400B | Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload". The field for the new game is a rectangle table of size _n_u2009×u2009_m_. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs: some dwarf in one of the chosen lines is located in the rightmost cell of his row; some dwarf in the chosen lines is located in the cell with the candy. The point of the game is to transport all the dwarves to the candy cells. Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game. Input The first line of the input contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u20091000;xa02u2009≤u2009_m_u2009≤u20091000). Next _n_ lines each contain _m_ characters — the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S". Output In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field. | 1,200 | false | false | true | false | false | false | true | false | false | false | 8,226 |
212A | There is a developed network of flights between Berland and Beerland. All of them belong to the Berland state company BerAvia. Each flight connects some Berland city with some Beerland city. For each flight airplanes fly in both directions. Changes are coming to Berland — the state decided to privatize BerAvia, namely, to sell out all flights to _t_ private companies. Each of these companies wants to get the maximal number of flights, so if the Berland flights are sold unevenly, Berland can be accused of partiality. Berland Government decided to sell the flights as evenly as possible between the _t_ companies. The unevenness of the distribution of flights between companies is calculated as follows. For each city _i_ (both Berland and Beerland) we'll calculate the value of where _a__ij_ is the number of flights from city _i_, which belong to company _j_. The sum of _w__i_ for all cities in both countries is called the unevenness of the distribution. The distribution with the minimal unevenness is the most even one. Help the Berland government come up with the most even distribution plan of selling flights. Input The first input line contains four integers _n_,u2009_m_,u2009_k_ and _t_ (1u2009≤u2009_n_,u2009_m_,u2009_t_u2009≤u2009200;1u2009≤u2009_k_u2009≤u20095000), where _n_,u2009_m_ are the numbers of cities in Berland and Beerland, correspondingly, _k_ is the number of flights between them, and _t_ is the number of private companies. Next _k_ lines describe the flights, one per line, as pairs of positive integers _x__i_,u2009_y__i_ (1u2009≤u2009_x__i_u2009≤u2009_n_;1u2009≤u2009_y__i_u2009≤u2009_m_), where _x__i_ and _y__i_ are the indexes of cities in Berland and Beerland, correspondingly, connected by the _i_-th flight. There is at most one flight between any pair of cities, each flight connects cities of different countries. The cities in Berland are indexed from 1 to _n_, and in Beerland — from 1 to _m_. Output Print the unevenness of the sought plan on the first line. On the second line print a sequence of _k_ integers _c_1,u2009_c_2,u2009...,u2009_c__k_ (1u2009≤u2009_c__i_u2009≤u2009_t_), where _c__i_ is the index of the company that should buy the _i_-th flight. Assume that the flights are indexed from 1 to _k_ in the order they appear in the input. If there are multiple solutions, print any of them. Examples Input 3 5 8 2 1 4 1 3 3 3 1 2 1 1 2 1 1 5 2 2 | 3,000 | false | false | false | false | false | false | false | false | false | true | 8,993 |
1482E | Alice is visiting New York City. To make the trip fun, Alice will take photos of the city skyline and give the set of photos as a present to Bob. However, she wants to find the set of photos with maximum beauty and she needs your help. There are $$$n$$$ buildings in the city, the $$$i$$$-th of them has positive height $$$h_i$$$. All $$$n$$$ building heights in the city are different. In addition, each building has a beauty value $$$b_i$$$. Note that beauty can be positive or negative, as there are ugly buildings in the city too. A set of photos consists of one or more photos of the buildings in the skyline. Each photo includes one or more buildings in the skyline that form a contiguous segment of indices. Each building needs to be in exactly one photo. This means that if a building does not appear in any photo, or if a building appears in more than one photo, the set of pictures is not valid. The beauty of a photo is equivalent to the beauty $$$b_i$$$ of the shortest building in it. The total beauty of a set of photos is the sum of the beauty of all photos in it. Help Alice to find the maximum beauty a valid set of photos can have. Input The first line contains an integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$), the number of buildings on the skyline. The second line contains $$$n$$$ distinct integers $$$h_1, h_2, ldots, h_n$$$ ($$$1 le h_i le n$$$). The $$$i$$$-th number represents the height of building $$$i$$$. The third line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$-10^9 le b_i le 10^9$$$). The $$$i$$$-th number represents the beauty of building $$$i$$$. Output Print one number representing the maximum beauty Alice can achieve for a valid set of photos of the skyline. Examples Input 5 1 2 3 5 4 1 5 3 2 4 Input 5 1 4 3 2 5 -3 4 -10 2 7 Input 10 4 7 3 2 5 1 9 10 6 8 -4 40 -46 -8 -16 4 -10 41 12 3 Note In the first example, Alice can achieve maximum beauty by taking five photos, each one containing one building. In the second example, Alice can achieve a maximum beauty of $$$10$$$ by taking four pictures: three just containing one building, on buildings $$$1$$$, $$$2$$$ and $$$5$$$, each photo with beauty $$$-3$$$, $$$4$$$ and $$$7$$$ respectively, and another photo containing building $$$3$$$ and $$$4$$$, with beauty $$$2$$$. In the third example, Alice will just take one picture of the whole city. In the fourth example, Alice can take the following pictures to achieve maximum beauty: photos with just one building on buildings $$$1$$$, $$$2$$$, $$$8$$$, $$$9$$$, and $$$10$$$, and a single photo of buildings $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, and $$$7$$$. | 2,100 | false | false | false | true | true | false | false | false | false | false | 3,263 |
113B | Problem - 113B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force data structures hashing strings *2000 No tag edit access → Contest materials , the given string _t_ has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him! Input The input file consists of three lines. The first line contains string _t_. The second and the third lines contain the _s__begin_ and _s__end_ identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters. Output Output the only number — the amount of different substrings of _t_ that start with _s__begin_ and end with _s__end_. Examples Input round ro ou Output 1 Input codeforces code forca Output 0 Input abababab a b Output 4 Input aba ab ba Output 1 Note In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab. In the fourth sample identificators intersect. | 2,000 | false | false | false | false | true | false | true | false | false | false | 9,434 |
493A | Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card. Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya. Input The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct. Next follows number _n_ (1u2009≤u2009_n_u2009≤u200990) — the number of fouls. Each of the following _n_ lines contains information about a foul in the following form: first goes number _t_ (1u2009≤u2009_t_u2009≤u200990) — the minute when the foul occurs; then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player; then goes the player's number _m_ (1u2009≤u2009_m_u2009≤u200999); then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given. The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute. Output For each event when a player received his first red card in a chronological order print a string containing the following information: The name of the team to which the player belongs; the player's number in his team; the minute when he received the card. If no player received a card, then you do not need to print anything. It is possible case that the program will not print anything to the output (if there were no red cards). Examples Input MC CSKA 9 28 a 3 y 62 h 25 y 66 h 42 y 70 h 25 y 77 a 4 y 79 a 25 y 82 h 42 r 89 h 16 y 90 a 13 r Output MC 25 70 MC 42 82 CSKA 13 90 | 1,300 | false | false | true | false | false | false | false | false | false | false | 7,863 |
585E | Vitalik the philatelist has a birthday today! As he is a regular customer in a stamp store called 'Robin Bobin', the store management decided to make him a gift. Vitalik wants to buy one stamp and the store will give him a non-empty set of the remaining stamps, such that the greatest common divisor (GCD) of the price of the stamps they give to him is more than one. If the GCD of prices of the purchased stamp and prices of present stamps set will be equal to 1, then Vitalik will leave the store completely happy. The store management asks you to count the number of different situations in which Vitalik will leave the store completely happy. Since the required number of situations can be very large, you need to find the remainder of this number modulo 109u2009+u20097. The situations are different if the stamps purchased by Vitalik are different, or if one of the present sets contains a stamp that the other present does not contain. Input The first line of the input contains integer _n_ (2u2009≤u2009_n_u2009≤u20095·105) — the number of distinct stamps, available for sale in the 'Robin Bobin' store. The second line contains a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (2u2009≤u2009_a__i_u2009≤u2009107), where _a__i_ is the price of the _i_-th stamp. Output Print a single integer — the remainder of the sought number of situations modulo 109u2009+u20097. Note In the first sample the following situations are possible: Vitalik buys the 1-st stamp, the store gives him the 2-nd stamp as a present; Vitalik buys the 3-rd stamp, the store gives him the 2-nd stamp as a present; Vitalik buys the 2-nd stamp, the store gives him the 1-st stamp as a present; Vitalik buys the 2-nd stamp, the store gives him the 3-rd stamp as a present; Vitalik buys the 2-nd stamp, the store gives him the 1-st and 3-rd stamps as a present. | 2,900 | true | false | false | false | false | false | false | false | false | false | 7,501 |
706D | Author has gone out of the stories about Vasiliy, so here is just a formal task description. You are given _q_ queries and a multiset _A_, initially containing only integer 0. There are three types of queries: 1. "+ x"xa0— add integer _x_ to multiset _A_. 2. "- x"xa0— erase one occurrence of integer _x_ from multiset _A_. It's guaranteed that at least one _x_ is present in the multiset _A_ before this query. 3. "? x"xa0— you are given integer _x_ and need to compute the value , i.e. the maximum value of bitwise exclusive OR (also know as XOR) of integer _x_ and some integer _y_ from the multiset _A_. Multiset is a set, where equal elements are allowed. Input The first line of the input contains a single integer _q_ (1u2009≤u2009_q_u2009≤u2009200u2009000)xa0— the number of queries Vasiliy has to perform. Each of the following _q_ lines of the input contains one of three characters '+', '-' or '?' and an integer _x__i_ (1u2009≤u2009_x__i_u2009≤u2009109). It's guaranteed that there is at least one query of the third type. Note, that the integer 0 will always be present in the set _A_. Output For each query of the type '?' print one integerxa0— the maximum value of bitwise exclusive OR (XOR) of integer _x__i_ and some integer from the multiset _A_. Example Input 10 + 8 + 9 + 11 + 6 + 1 ? 3 - 8 ? 3 ? 8 ? 11 Note After first five operations multiset _A_ contains integers 0, 8, 9, 11, 6 and 1. The answer for the sixth query is integer xa0— maximum among integers , , , and . | 1,800 | false | false | false | false | true | false | false | true | false | false | 6,996 |
635A | Paul is at the orchestra. The string section is arranged in an _r_u2009×u2009_c_ rectangular grid and is filled with violinists with the exception of _n_ violists. Paul really likes violas, so he would like to take a picture including at least _k_ of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take. Two pictures are considered to be different if the coordinates of corresponding rectangles are different. Input The first line of input contains four space-separated integers _r_, _c_, _n_, _k_ (1u2009≤u2009_r_,u2009_c_,u2009_n_u2009≤u200910, 1u2009≤u2009_k_u2009≤u2009_n_)xa0— the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively. The next _n_ lines each contain two integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_u2009≤u2009_r_, 1u2009≤u2009_y__i_u2009≤u2009_c_): the position of the _i_-th viola. It is guaranteed that no location appears more than once in the input. Output Print a single integerxa0— the number of photographs Paul can take which include at least _k_ violas. Note We will use '*' to denote violinists and '#' to denote violists. In the first sample, the orchestra looks as follows *# * Paul can take a photograph of just the viola, the 1u2009×u20092 column containing the viola, the 2u2009×u20091 row containing the viola, or the entire string section, for 4 pictures total. In the second sample, the orchestra looks as follows # *# # Paul must take a photograph of the entire section. In the third sample, the orchestra looks the same as in the second sample. | 1,100 | false | false | true | false | false | false | true | false | false | false | 7,266 |
1178F2 | This is the second subtask of problem F. The only differences between this and the first subtask are the constraints on the value of $$$m$$$ and the time limit. It is sufficient to solve this subtask in order to hack it, but you need to solve both subtasks in order to hack the first one. There are $$$n+1$$$ distinct colours in the universe, numbered $$$0$$$ through $$$n$$$. There is a strip of paper $$$m$$$ centimetres long initially painted with colour $$$0$$$. Alice took a brush and painted the strip using the following process. For each $$$i$$$ from $$$1$$$ to $$$n$$$, in this order, she picks two integers $$$0 leq a_i < b_i leq m$$$, such that the segment $$$[a_i, b_i]$$$ is currently painted with a single colour, and repaints it with colour $$$i$$$. Alice chose the segments in such a way that each centimetre is now painted in some colour other than $$$0$$$. Formally, the segment $$$[i-1, i]$$$ is painted with colour $$$c_i$$$ ($$$c_i eq 0$$$). Every colour other than $$$0$$$ is visible on the strip. Count the number of different pairs of sequences $$${a_i}_{i=1}^n$$$, $$${b_i}_{i=1}^n$$$ that result in this configuration. Since this number may be large, output it modulo $$$998244353$$$. Input The first line contains a two integers $$$n$$$, $$$m$$$ ($$$1 leq n leq 500$$$, $$$n leq m leq 10^6$$$)xa0— the number of colours excluding the colour $$$0$$$ and the length of the paper, respectively. The second line contains $$$m$$$ space separated integers $$$c_1, c_2, ldots, c_m$$$ ($$$1 leq c_i leq n$$$)xa0— the colour visible on the segment $$$[i-1, i]$$$ after the process ends. It is guaranteed that for all $$$j$$$ between $$$1$$$ and $$$n$$$ there is an index $$$k$$$ such that $$$c_k = j$$$. Output Output a single integerxa0— the number of ways Alice can perform the painting, modulo $$$998244353$$$. Examples Input 8 17 1 3 2 2 7 8 2 5 5 4 4 4 1 1 6 1 1 Note In the first example, there are $$$5$$$ ways, all depicted in the figure below. Here, $$$0$$$ is white, $$$1$$$ is red, $$$2$$$ is green and $$$3$$$ is blue. Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour $$$2$$$. In the second example, Alice must first paint segment 0 3 with colour $$$1$$$ and then segment 1 2 with colour $$$2$$$. | 2,600 | false | false | false | true | false | false | false | false | false | false | 4,824 |
1070M | Once upon a time Algoland and Berland were a single country, but those times are long gone. Now they are two different countries, but their cities are scattered on a common territory. All cities are represented as points on the Cartesian plane. Algoland consists of $$$a$$$ cities numbered from $$$1$$$ to $$$a$$$. The coordinates of the $$$i$$$-th Algoland city are a pair of integer numbers $$$(xa_i, ya_i)$$$. Similarly, Berland consists of $$$b$$$ cities numbered from $$$1$$$ to $$$b$$$. The coordinates of the $$$j$$$-th Berland city are a pair of integer numbers $$$(xb_j, yb_j)$$$. No three of the $$$a+b$$$ mentioned cities lie on a single straight line. As the first step to unite the countries, Berland decided to build several bidirectional freeways. Each freeway is going to be a line segment that starts in a Berland city and ends in an Algoland city. Freeways can't intersect with each other at any point except freeway's start or end. Moreover, the freeways have to connect all $$$a+b$$$ cities. Here, connectivity means that one can get from any of the specified $$$a+b$$$ cities to any other of the $$$a+b$$$ cities using freeways. Note that all freeways are bidirectional, which means that one can drive on each of them in both directions. Mayor of each of the $$$b$$$ Berland cities allocated a budget to build the freeways that start from this city. Thus, you are given the numbers $$$r_1, r_2, dots, r_b$$$, where $$$r_j$$$ is the number of freeways that are going to start in the $$$j$$$-th Berland city. The total allocated budget is very tight and only covers building the minimal necessary set of freeways. In other words, $$$r_1+r_2+dots+r_b=a+b-1$$$. Help Berland to build the freeways so that: each freeway is a line segment connecting a Berland city and an Algoland city, no freeways intersect with each other except for the freeway's start or end, freeways connect all $$$a+b$$$ cities (all freeways are bidirectional), there are $$$r_j$$$ freeways that start from the $$$j$$$-th Berland city. Input Input contains one or several test cases. The first input line contains a single integer number $$$t$$$ ($$$1 le t le 3000$$$) — number of test cases. Then, $$$t$$$ test cases follow. Solve test cases separately, test cases are completely independent and do not affect each other. Each test case starts with a line containing space-separated integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 3000$$$) — numbers of Algoland cities and number of Berland cities correspondingly. The next line contains $$$b$$$ space-separated integers $$$r_1, r_2, dots, r_b$$$ ($$$1 le r_b le a$$$) where $$$r_j$$$ is the number of freeways, that should start in the $$$j$$$-th Berland city. It is guaranteed that $$$r_1+r_2+dots+r_b=a+b-1$$$. The next $$$a$$$ lines contain coordinates of the Algoland cities — pairs of space-separated integers $$$xa_i, ya_i$$$ ($$$-10000 le xa_i, ya_i le 10000$$$). The next $$$b$$$ lines contain coordinates of the Berland cities — pairs of space-separated integers $$$xb_i, yb_i$$$ ($$$-10000 le xb_i, yb_i le 10000$$$). All cities are located at distinct points, no three of the $$$a+b$$$ cities lie on a single straight line. Sum of values $$$a$$$ across all test cases doesn't exceed $$$3000$$$. Sum of values $$$b$$$ across all test cases doesn't exceed $$$3000$$$. Output For each of the $$$t$$$ test cases, first print "YES" if there is an answer or "NO" otherwise. If there is an answer, print the freeway building plan in the next $$$a+b-1$$$ lines. Each line of the plan should contain two space-separated integers $$$j$$$ and $$$i$$$ which means that a freeway from the $$$j$$$-th Berland city to the $$$i$$$-th Algoland city should be built. If there are multiple solutions, print any. Example Input 2 2 3 1 1 2 0 0 1 1 1 2 3 2 4 0 1 1 1 0 0 0 1 Output YES 2 2 1 2 3 2 3 1 YES 1 1 | 3,000 | false | false | false | false | false | true | false | false | false | false | 5,355 |
815C | On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to _b_ dollars. The supermarket sells _n_ goods. The _i_-th good can be bought for _c__i_ dollars. Of course, each good can only be bought once. Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given _n_ coupons. If Karen purchases the _i_-th good, she can use the _i_-th coupon to decrease its price by _d__i_. Of course, a coupon cannot be used without buying the corresponding good. There is, however, a constraint with the coupons. For all _i_u2009≥u20092, in order to use the _i_-th coupon, Karen must also use the _x__i_-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon). Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget _b_? Input The first line of input contains two integers _n_ and _b_ (1u2009≤u2009_n_u2009≤u20095000, 1u2009≤u2009_b_u2009≤u2009109), the number of goods in the store and the amount of money Karen has, respectively. The next _n_ lines describe the items. Specifically: The _i_-th line among these starts with two integers, _c__i_ and _d__i_ (1u2009≤u2009_d__i_u2009<u2009_c__i_u2009≤u2009109), the price of the _i_-th good and the discount when using the coupon for the _i_-th good, respectively. If _i_u2009≥u20092, this is followed by another integer, _x__i_ (1u2009≤u2009_x__i_u2009<u2009_i_), denoting that the _x__i_-th coupon must also be used before this coupon can be used. Output Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget. Note In the first test case, Karen can purchase the following 4 items: Use the first coupon to buy the first item for 10u2009-u20099u2009=u20091 dollar. Use the third coupon to buy the third item for 12u2009-u20092u2009=u200910 dollars. Use the fourth coupon to buy the fourth item for 20u2009-u200918u2009=u20092 dollars. Buy the sixth item for 2 dollars. The total cost of these goods is 15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here. In the second test case, Karen has enough money to use all the coupons and purchase everything. | 2,400 | false | false | false | true | false | false | true | false | false | false | 6,498 |
1983E | Alice and Bob are playing a game. There are $$$n$$$ balls, out of which $$$k$$$ are special. Each ball has a value associated with it. The players play turn by turn. In each turn, the player randomly picks a ball and adds the value of the ball to their score, which is $$$0$$$ at the beginning of the game. The selected ball is removed from the game. If the ball was special, the same player takes the next turn if at least one ball is remaining. If the ball picked was not special, the next player plays their turn. They play this game until no balls are remaining in the game. Alice plays first. Find the expected score that both the players have at the end of the game modulo $$$10^9+7$$$. Formally, let $$$M = 10^9+7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Input There are multiple test cases. The first line of the input contains an integer $$$t$$$, the number of test cases ($$$1 le t le 2 cdot 10^5$$$). Each test case description is on a new line. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ in the respective order separated by a space ($$$1 le k le n le 4 cdot 10^5$$$). The second line of the test case contains $$$n$$$ integers: $$$v_1, v_2, ldots, v_n$$$, the value for each ball separated by spaces. The first $$$k$$$ balls are special ($$$1 le v_i le 10^7$$$). The sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output Output two integers per test case in a new line, the expected score of Alice and the expected score of Bob modulo $$$10^9+7$$$. Examples Input 5 1 1 732507 2 2 5817860 5398510 5 1 2122894 4951549 2750585 7821535 3214167 8 4 1405323 5069867 6883092 6972029 328406 2478975 7628890 9973340 4 2 9662050 3566134 3996473 9872255 Output 732507 0 11216370 0 810642660 210218077 722402997 318336932 349086489 678010430 Input 5 3 3 1095611 8219204 7773462 2 1 8176490 2774103 3 1 9178636 5138057 3367761 12 9 7597698 6843019 2298534 1522386 4969588 1340345 3967362 9152890 6689668 9986080 4745473 7407325 10 5 6986368 2397882 5804127 6980694 3740836 3215836 5195724 3179261 4136769 4544231 Output 17088277 0 6862348 4088245 677038671 340645790 36949997 29570371 725118051 321063684 Note In the first test case, Alice's expected score is $$$45$$$, and Bob's is $$$30$$$ at the end of the game. | 2,300 | true | false | false | false | false | false | false | false | false | false | 386 |
1705D | Mark has just purchased a rack of $$$n$$$ lightbulbs. The state of the lightbulbs can be described with binary string $$$s = s_1s_2dots s_n$$$, where $$$s_i= exttt{1}$$$ means that the $$$i$$$-th lightbulb is turned on, while $$$s_i= exttt{0}$$$ means that the $$$i$$$-th lightbulb is turned off. Unfortunately, the lightbulbs are broken, and the only operation he can perform to change the state of the lightbulbs is the following: Select an index $$$i$$$ from $$$2,3,dots,n-1$$$ such that $$$s_{i-1} e s_{i+1}$$$. Toggle $$$s_i$$$. Namely, if $$$s_i$$$ is $$$ exttt{0}$$$, set $$$s_i$$$ to $$$ exttt{1}$$$ or vice versa. Mark wants the state of the lightbulbs to be another binary string $$$t$$$. Help Mark determine the minimum number of operations to do so. Input The first line of the input contains a single integer $$$q$$$ ($$$1leq qleq 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$3leq nleq 2cdot 10^5$$$) — the number of lightbulbs. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ — the initial state of the lightbulbs. The third line of each test case contains a binary string $$$t$$$ of length $$$n$$$ — the final state of the lightbulbs. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, print a line containing the minimum number of operations Mark needs to perform to transform $$$s$$$ to $$$t$$$. If there is no such sequence of operations, print $$$-1$$$. Example Input 4 4 0100 0010 4 1010 0100 5 01001 00011 6 000101 010011 Note In the first test case, one sequence of operations that achieves the minimum number of operations is the following. Select $$$i=3$$$, changing $$$ exttt{01}color{red}{ exttt{0}} exttt{0}$$$ to $$$ exttt{01}color{red}{ exttt{1}} exttt{0}$$$. Select $$$i=2$$$, changing $$$ exttt{0}color{red}{ exttt{1}} exttt{10}$$$ to $$$ exttt{0}color{red}{ exttt{0}} exttt{10}$$$. In the second test case, there is no sequence of operations because one cannot change the first digit or the last digit of $$$s$$$. In the third test case, even though the first digits of $$$s$$$ and $$$t$$$ are the same and the last digits of $$$s$$$ and $$$t$$$ are the same, it can be shown that there is no sequence of operations that satisfies the condition. In the fourth test case, one sequence that achieves the minimum number of operations is the following: Select $$$i=3$$$, changing $$$ exttt{00}color{red}{ exttt{0}} exttt{101}$$$ to $$$ exttt{00}color{red}{ exttt{1}} exttt{101}$$$. Select $$$i=2$$$, changing $$$ exttt{0}color{red}{ exttt{0}} exttt{1101}$$$ to $$$ exttt{0}color{red}{ exttt{1}} exttt{1101}$$$. Select $$$i=4$$$, changing $$$ exttt{011}color{red}{ exttt{1}} exttt{01}$$$ to $$$ exttt{011}color{red}{ exttt{0}} exttt{01}$$$. Select $$$i=5$$$, changing $$$ exttt{0110}color{red}{ exttt{0}} exttt{1}$$$ to $$$ exttt{0110}color{red}{ exttt{1}} exttt{1}$$$. Select $$$i=3$$$, changing $$$ exttt{01}color{red}{ exttt{1}} exttt{011}$$$ to $$$ exttt{01}color{red}{ exttt{0}} exttt{011}$$$. | 1,800 | true | true | false | false | false | true | false | false | true | false | 2,043 |
542A | Ivan Anatolyevich's agency is starting to become famous in the town. They have already ordered and made _n_ TV commercial videos. Each video is made in a special way: the colors and the soundtrack are adjusted to the time of the day and the viewers' mood. That's why the _i_-th video can only be shown within the time range of [_l__i_,u2009_r__i_] (it is not necessary to use the whole segment but the broadcast time should be within this segment). Now it's time to choose a TV channel to broadcast the commercial. Overall, there are _m_ TV channels broadcasting in the city, the _j_-th one has _c__j_ viewers, and is ready to sell time [_a__j_,u2009_b__j_] to broadcast the commercial. Ivan Anatolyevich is facing a hard choice: he has to choose exactly one video _i_ and exactly one TV channel _j_ to broadcast this video and also a time range to broadcast [_x_,u2009_y_]. At that the time range should be chosen so that it is both within range [_l__i_,u2009_r__i_] and within range [_a__j_,u2009_b__j_]. Let's define the efficiency of the broadcast as value (_y_u2009-u2009_x_)·_c__j_ — the total sum of time that all the viewers of the TV channel are going to spend watching the commercial. Help Ivan Anatolyevich choose the broadcast with the maximum efficiency! Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092·105)xa0— the number of commercial videos and channels, respectively. Each of the following _n_ lines contains two integers _l__i_, _r__i_ (0u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009109)xa0— the segment of time when it is possible to show the corresponding video. Each of the following _m_ lines contains three integers _a__j_, _b__j_, _c__j_ (0u2009≤u2009_a__j_u2009≤u2009_b__j_u2009≤u2009109, 1u2009≤u2009_c__j_u2009≤u2009109), characterizing the TV channel. Output In the first line print an integer — the maximum possible efficiency of the broadcast. If there is no correct way to get a strictly positive efficiency, print a zero. If the maximum efficiency is strictly positive, in the second line also print the number of the video _i_ (1u2009≤u2009_i_u2009≤u2009_n_) and the number of the TV channel _j_ (1u2009≤u2009_j_u2009≤u2009_m_) in the most effective broadcast. If there are multiple optimal answers, you can print any of them. Examples Input 2 3 7 9 1 4 2 8 2 0 4 1 8 9 3 Note In the first sample test the most optimal solution is to show the second commercial using the first TV channel at time [2,u20094]. The efficiency of such solution is equal to (4u2009-u20092)·2u2009=u20094. In the second sample test Ivan Anatolievich's wish does not meet the options of the TV channel, the segments do not intersect, so the answer is zero. | 2,400 | false | false | false | false | true | false | false | false | true | false | 7,675 |
1738F | This is an interactive problem. Given a simple undirected graph with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$, your task is to color all the vertices such that for every color $$$c$$$, the following conditions hold: 1. The set of vertices with color $$$c$$$ is connected; 2. $$$s_c leq n_c^2$$$, where $$$n_c$$$ is the number of vertices with color $$$c$$$, and $$$s_c$$$ is the sum of degrees of vertices with color $$$c$$$. It can be shown that there always exists a way to color all the vertices such that the above conditions hold. Initially, you are only given the number $$$n$$$ of vertices and the degree of each vertex. In each query, you can choose a vertex $$$u$$$. As a response, you will be given the $$$k$$$-th edge incident to $$$u$$$, if this is the $$$k$$$-th query on vertex $$$u$$$. You are allowed to make at most $$$n$$$ queries. An undirected graph is simple if it does not contain multiple edges or self-loops. The degree of a vertex is the number of edges incident to it. A set $$$S$$$ of vertices is connected if for every two different vertices $$$u, v in S$$$, there is a path, which only passes through vertices in $$$S$$$, that connects $$$u$$$ and $$$v$$$. That is, there is a sequence of edges $$$(u_1, v_1), (u_2, v_2), dots, (u_k, v_k)$$$ with $$$k geq 1$$$ such that 1. $$$u_1 = u$$$, $$$v_k = v$$$, and $$$v_i = u_{i+1}$$$ for every $$$1 leq i < k$$$; and 2. $$$u_k in S$$$ and $$$v_k in S$$$ for every $$$1 leq i leq k$$$. Especially, a set containing only one vertex is connected. Interaction Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$) — the number of test cases. The following lines contain the description and the interactive section of each test case. For each test case, you begin the interaction by reading an integer $$$n$$$ ($$$1le n le 1000$$$) in the first line, indicating the number of vertices in the graph. The second line contains $$$n$$$ integers $$$d_1, d_2, dots, d_n$$$ ($$$0 leq d_i leq n - 1$$$), where $$$d_i$$$ is the degree of vertex $$$i$$$. To make a query on vertex $$$u$$$ ($$$1 leq u leq n$$$), you should output "? $$$u$$$" in a separate line. If this is the $$$k$$$-th query on vertex $$$u$$$, vertex $$$e_{u, k}$$$ will be given in the next separate line, where $$$left(u, e_{u, k} ight)$$$ is the $$$k$$$-th edge incident to vertex $$$u$$$. In case of $$$k > d_u$$$, define $$$e_{u, k} = -1$$$. You should make no more than $$$n$$$ "?" queries. To give the answer, you should output "! $$$c_1$$$ $$$c_2$$$ $$$dots$$$ $$$c_n$$$" in a separate line, where $$$c_i$$$ ($$$1 leq c_i leq n$$$) is the color of vertex $$$i$$$. After that, your program should continue to the next test case, or terminate if this is the last test case. It is guaranteed that the graph is a simple undirected graph. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$. In case your query format is invalid, or you have made more than $$$n$$$ "?" queries, you will receive Wrong Answer verdict. After printing a query, do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hack Format The first line of the hack contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$), indicating the number of test cases. The following lines contain the description of each test case. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 1000$$$), indicating the number of vertices in the graph. Then $$$n$$$ lines follow. The $$$i$$$-th line contains an integer $$$d_i$$$ ($$$0 leq d_i leq n - 1$$$), indicating the degree of vertex $$$i$$$, and then $$$d_i$$$ distinct integers $$$e_{i,1}, e_{i,2}, dots, e_{i,d_i}$$$ ($$$1 leq e_{i, j} leq n$$$ and $$$e_{i,j} eq i$$$), where $$$left(i, e_{i,j} ight)$$$ is the $$$j$$$-th edge incident to vertex $$$i$$$. It should be guaranteed that the graph is a simple undirected graph. It should be guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$. Note In the example, there is only one test case. In the test case, there are $$$n = 5$$$ vertices with vertices $$$1, 2, 3, 4$$$ of degree $$$2$$$ and vertex $$$5$$$ of degree $$$0$$$. It is obvious that vertex $$$5$$$ is isolated, i.e., it does not connect to any other vertices. A possible interaction is shown in the sample input and output, where $$$4$$$ "?" queries are made on vertex $$$1$$$ twice and vertex $$$3$$$ twice. According to the responses to these queries, we know that each of vertex $$$1$$$ and vertex $$$3$$$ connects to two vertices $$$2$$$ and $$$4$$$. A possible solution is shown in the sample output, where vertex $$$1$$$ and vertex $$$2$$$ are colored by $$$1$$$, vertex $$$3$$$ and vertex $$$4$$$ are colored by $$$2$$$, and vertex $$$5$$$ is colored by $$$3$$$. It can be seen that this solution satisfies the required conditions as follows. For color $$$c = 1$$$, vertex $$$1$$$ and vertex $$$2$$$ are connected. Moreover, $$$n_1 = 2$$$ and $$$s_1 = d_1 + d_2 = 2 + 2 = 4 leq n_1^2 = 2^2 = 4$$$; For color $$$c = 2$$$, vertex $$$3$$$ and vertex $$$4$$$ are connected. Moreover, $$$n_2 = 2$$$ and $$$s_2 = d_3 + d_4 = 2 + 2 = 4 leq n_2^2 = 2^2 = 4$$$; For color $$$c = 3$$$, there is only one vertex (vertex $$$5$$$) colored by $$$3$$$. Moreover, $$$n_3 = 1$$$ and $$$s_3 = d_5 = 0 leq n_3^2 = 1^2 = 1$$$. | 2,400 | false | true | false | false | false | true | false | false | false | true | 1,856 |
1358D | You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan. They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha. The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month. You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. Input The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $$$n$$$ integers $$$d_1, d_2, ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 le d_i le 10^6$$$). It is guaranteed that $$$1 le x le d_1 + d_2 + ldots + d_n$$$. Output Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. Note In the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$${1,1,2,3,1}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs. In the second test case, the numbers of the days are $$${1,2,3,1,2,3,1,2,3}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs. In the third test case, the numbers of the days are $$${1,2,3,4,1,2, 1,2,3, 1, 1,2,3}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times. | 1,900 | false | true | true | false | false | false | true | true | false | false | 3,894 |
1361B | , # Johnny and Grandmaster Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Johnny has just found the new, great tutorial: “ How to become a grandmaster? ”. The tutorial tells many strange and unexpected for Johnny things, such as you have to be patient or that very important is solving many harder and harder problems. The boy has found an online judge with tasks divided by topics they cover. He has picked pki problems from i-th category ( p is his favorite number). He wants to solve them in two weeks (the patience condition is too hard for Johnny, so for simplicity, he looks only at easy tasks, which can be solved in such a period). Now our future grandmaster has to decide which topics to cover first and which the second week. Help him assign topics in such a way, that workload is balanced. Formally, given n numbers pki , the boy wants to divide them into two disjoint sets, minimizing the absolute difference between sums of numbers in each set. Find the minimal absolute difference. Output the result modulo 10 9 + 7 . # Input Input consists of multiple test cases. The first line contains one integer t (1 ≤ t ≤ 10 5) x16 the number of test cases. Each test case is described as follows: The first line contains two integers n and p (1 ≤ n, p ≤ 10 6). The second line contains n integers ki (0 ≤ ki ≤ 10 6).The sum of n over all test cases doesn’t exceed 10 6. # Output Output one integer x16 the reminder of division the answer by 1 000 000 007 . # Example standard input standard output 45 2 2 3 4 4 3 3 1 2 10 1000 4 5 0 1 1 100 1 8 89 41146981438 747093407 # Note You have to minimize the difference, not it’s remainder. For example, if the minimum difference is equal to 2, but there is also a distribution where the difference is 10 9 + 8 , then the answer is 2, not 1.In the first test case of the example, there’re the following numbers: 4, 8, 16 , 16 , and 8. We can divide them into such two sets: 4, 8, 16 and 8, 16 . Then the difference between the sums of numbers in sets would be 4. Page 1 of 1 | 1,900 | true | true | true | false | false | false | false | false | true | false | 3,876 |
1010B | This is an interactive problem. Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet. Let's define $$$x$$$ as the distance to Mars. Unfortunately, Natasha does not know $$$x$$$. But it is known that $$$1 le x le m$$$, where Natasha knows the number $$$m$$$. Besides, $$$x$$$ and $$$m$$$ are positive integers. Natasha can ask the rocket questions. Every question is an integer $$$y$$$ ($$$1 le y le m$$$). The correct answer to the question is $$$-1$$$, if $$$x<y$$$, $$$0$$$, if $$$x=y$$$, and $$$1$$$, if $$$x>y$$$. But the rocket is brokenxa0— it does not always answer correctly. Precisely: let the correct answer to the current question be equal to $$$t$$$, then, if the rocket answers this question correctly, then it will answer $$$t$$$, otherwise it will answer $$$-t$$$. In addition, the rocket has a sequence $$$p$$$ of length $$$n$$$. Each element of the sequence is either $$$0$$$ or $$$1$$$. The rocket processes this sequence in the cyclic order, that is $$$1$$$-st element, $$$2$$$-nd, $$$3$$$-rd, $$$ldots$$$, $$$(n-1)$$$-th, $$$n$$$-th, $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$ldots$$$, $$$(n-1)$$$-th, $$$n$$$-th, $$$ldots$$$. If the current element is $$$1$$$, the rocket answers correctly, if $$$0$$$xa0— lies. Natasha doesn't know the sequence $$$p$$$, but she knows its lengthxa0— $$$n$$$. You can ask the rocket no more than $$$60$$$ questions. Help Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions. Your solution will not be accepted, if it does not receive an answer $$$0$$$ from the rocket (even if the distance to Mars is uniquely determined by the already received rocket's answers). Input The first line contains two integers $$$m$$$ and $$$n$$$ ($$$1 le m le 10^9$$$, $$$1 le n le 30$$$)xa0— the maximum distance to Mars and the number of elements in the sequence $$$p$$$. Interaction You can ask the rocket no more than $$$60$$$ questions. To ask a question, print a number $$$y$$$ ($$$1le yle m$$$) and an end-of-line character, then do the operation flush and read the answer to the question. If the program reads $$$0$$$, then the distance is correct and you must immediately terminate the program (for example, by calling exit(0)). If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream. If at some point your program reads $$$-2$$$ as an answer, it must immediately end (for example, by calling exit(0)). You will receive the "Wrong answer" verdict, and this will mean that the request is incorrect or the number of requests exceeds $$$60$$$. If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream. If your program's request is not a valid integer between $$$-2^{31}$$$ and $$$2^{31}-1$$$ (inclusive) without leading zeros, then you can get any verdict. You can get "Idleness limit exceeded" if you don't print anything or if you forget to flush the output. To flush the output buffer you can use (after printing a query 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 Use the following format for hacking: In the first line, print $$$3$$$ integers $$$m,n,x$$$ ($$$1le xle mle 10^9$$$, $$$1le nle 30$$$)xa0— the maximum distance to Mars, the number of elements in the sequence $$$p$$$ and the current distance to Mars. In the second line, enter $$$n$$$ numbers, each of which is equal to $$$0$$$ or $$$1$$$xa0— sequence $$$p$$$. The hacked solution will not have access to the number $$$x$$$ and sequence $$$p$$$. Note In the example, hacking would look like this: 5 2 3 1 0 This means that the current distance to Mars is equal to $$$3$$$, Natasha knows that it does not exceed $$$5$$$, and the rocket answers in order: correctly, incorrectly, correctly, incorrectly ... Really: on the first query ($$$1$$$) the correct answer is $$$1$$$, the rocket answered correctly: $$$1$$$; on the second query ($$$2$$$) the correct answer is $$$1$$$, the rocket answered incorrectly: $$$-1$$$; on the third query ($$$4$$$) the correct answer is $$$-1$$$, the rocket answered correctly: $$$-1$$$; on the fourth query ($$$5$$$) the correct answer is $$$-1$$$, the rocket answered incorrectly: $$$1$$$; on the fifth query ($$$3$$$) the correct and incorrect answer is $$$0$$$. | 1,800 | false | false | false | false | false | false | false | true | false | false | 5,634 |
1622C | You are given an integer array $$$a_1, a_2, dots, a_n$$$ and integer $$$k$$$. In one step you can either choose some index $$$i$$$ and decrease $$$a_i$$$ by one (make $$$a_i = a_i - 1$$$); or choose two indices $$$i$$$ and $$$j$$$ and set $$$a_i$$$ equal to $$$a_j$$$ (make $$$a_i = a_j$$$). What is the minimum number of steps you need to make the sum of array $$$sumlimits_{i=1}^{n}{a_i} le k$$$? (You are allowed to make values of array negative). 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 10^{15}$$$)xa0— the size of array $$$a$$$ and upper bound on its sum. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the array itself. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Note In the first test case, you should decrease $$$a_1$$$ $$$10$$$ times to get the sum lower or equal to $$$k = 10$$$. In the second test case, the sum of array $$$a$$$ is already less or equal to $$$69$$$, so you don't need to change it. In the third test case, you can, for example: 1. set $$$a_4 = a_3 = 1$$$; 2. decrease $$$a_4$$$ by one, and get $$$a_4 = 0$$$. As a result, you'll get array $$$[1, 2, 1, 0, 1, 2, 1]$$$ with sum less or equal to $$$8$$$ in $$$1 + 1 = 2$$$ steps. In the fourth test case, you can, for example: 1. choose $$$a_7$$$ and decrease in by one $$$3$$$ times; you'll get $$$a_7 = -2$$$; 2. choose $$$4$$$ elements $$$a_6$$$, $$$a_8$$$, $$$a_9$$$ and $$$a_{10}$$$ and them equal to $$$a_7 = -2$$$. As a result, you'll get array $$$[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$$$ with sum less or equal to $$$1$$$ in $$$3 + 4 = 7$$$ steps. | 1,600 | false | true | false | false | false | false | true | true | true | false | 2,509 |
1217D | You are given a directed graph with $$$n$$$ vertices and $$$m$$$ directed edges without self-loops or multiple edges. Let's denote the $$$k$$$-coloring of a digraph as following: you color each edge in one of $$$k$$$ colors. The $$$k$$$-coloring is good if and only if there no cycle formed by edges of same color. Find a good $$$k$$$-coloring of given digraph with minimum possible $$$k$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 5000$$$, $$$1 le m le 5000$$$) — the number of vertices and edges in the digraph, respectively. Next $$$m$$$ lines contain description of edges — one per line. Each edge is a pair of integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u e v$$$) — there is directed edge from $$$u$$$ to $$$v$$$ in the graph. It is guaranteed that each ordered pair $$$(u, v)$$$ appears in the list of edges at most once. Output In the first line print single integer $$$k$$$ — the number of used colors in a good $$$k$$$-coloring of given graph. In the second line print $$$m$$$ integers $$$c_1, c_2, dots, c_m$$$ ($$$1 le c_i le k$$$), where $$$c_i$$$ is a color of the $$$i$$$-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize $$$k$$$). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 | 2,100 | false | false | false | false | false | true | false | false | false | true | 4,587 |
954D | Little town Nsk consists of _n_ junctions connected by _m_ bidirectional roads. Each road connects two distinct junctions and no two roads connect the same pair of junctions. It is possible to get from any junction to any other junction by these roads. The distance between two junctions is equal to the minimum possible number of roads on a path between them. In order to improve the transportation system, the city council asks mayor to build one new road. The problem is that the mayor has just bought a wonderful new car and he really enjoys a ride from his home, located near junction _s_ to work located near junction _t_. Thus, he wants to build a new road in such a way that the distance between these two junctions won't decrease. You are assigned a task to compute the number of pairs of junctions that are not connected by the road, such that if the new road between these two junctions is built the distance between _s_ and _t_ won't decrease. Input The firt line of the input contains integers _n_, _m_, _s_ and _t_ (2u2009≤u2009_n_u2009≤u20091000, 1u2009≤u2009_m_u2009≤u20091000, 1u2009≤u2009_s_,u2009_t_u2009≤u2009_n_, _s_u2009≠u2009_t_)xa0— the number of junctions and the number of roads in Nsk, as well as the indices of junctions where mayors home and work are located respectively. The _i_-th of the following _m_ lines contains two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, _u__i_u2009≠u2009_v__i_), meaning that this road connects junctions _u__i_ and _v__i_ directly. It is guaranteed that there is a path between any two junctions and no two roads connect the same pair of junctions. Output Print one integerxa0— the number of pairs of junctions not connected by a direct road, such that building a road between these two junctions won't decrease the distance between junctions _s_ and _t_. Examples Input 5 6 1 5 1 2 1 3 1 4 4 5 3 5 2 5 | 1,600 | false | false | false | false | false | false | false | false | false | true | 5,906 |
1062B | JATC's math teacher always gives the class some interesting math problems so that they don't get bored. Today the problem is as follows. Given an integer $$$n$$$, you can perform the following operations zero or more times: mul $$$x$$$: multiplies $$$n$$$ by $$$x$$$ (where $$$x$$$ is an arbitrary positive integer). sqrt: replaces $$$n$$$ with $$$sqrt{n}$$$ (to apply this operation, $$$sqrt{n}$$$ must be an integer). You can perform these operations as many times as you like. What is the minimum value of $$$n$$$, that can be achieved and what is the minimum number of operations, to achieve that minimum value? Apparently, no one in the class knows the answer to this problem, maybe you can help them? Input The only line of the input contains a single integer $$$n$$$ ($$$1 le n le 10^6$$$)xa0— the initial number. Output Print two integers: the minimum integer $$$n$$$ that can be achieved using the described operations and the minimum number of operations required. Note In the first example, you can apply the operation mul $$$5$$$ to get $$$100$$$ and then sqrt to get $$$10$$$. In the second example, you can first apply sqrt to get $$$72$$$, then mul $$$18$$$ to get $$$1296$$$ and finally two more sqrt and you get $$$6$$$. Note, that even if the initial value of $$$n$$$ is less or equal $$$10^6$$$, it can still become greater than $$$10^6$$$ after applying one or more operations. | 1,500 | true | true | false | false | false | false | false | false | false | false | 5,401 |
762C | Problem - 762C - Codeforces =============== xa0 ]( "Announcement") characters from string _b_ in such a way that it becomes a subsequence of string _a_. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from _b_ and make it empty. Subsequence of string _s_ is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string _s_. Input The first line contains string _a_, and the second linexa0— string _b_. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters. Output On the first line output a subsequence of string _a_, obtained from _b_ by erasing the minimum number of consecutive characters. If the answer consists of zero characters, output «-» (a minus sign). Examples Input hi bob Output - Input abca accepted Output ac Input abacaba abcdcba Output abcba Note In the first example strings _a_ and _b_ don't share any symbols, so the longest string that you can get is empty. In the second example ac is a subsequence of _a_, and at the same time you can obtain it by erasing consecutive symbols cepted from string _b_. | 2,100 | false | false | false | false | false | false | false | true | false | false | 6,741 |
111C | Little Petya loves training spiders. Petya has a board _n_u2009×u2009_m_ in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u200940,u2009_n_·_m_u2009≤u200940) — the board sizes. Output In the first line print the maximum number of cells without spiders. Note In the first sample the only possible answer is: s In the second sample one of the possible solutions is: rdl rul s denotes command "stay idle", l, r, d, u denote commands "crawl left", "crawl right", "crawl down", "crawl up", correspondingly. | 2,100 | false | false | false | true | false | false | false | false | false | false | 9,440 |
1343D | You are given an array $$$a$$$ consisting of $$$n$$$ integers (it is guaranteed that $$$n$$$ is even, i.e. divisible by $$$2$$$). All $$$a_i$$$ does not exceed some integer $$$k$$$. Your task is to replace the minimum number of elements (replacement is the following operation: choose some index $$$i$$$ from $$$1$$$ to $$$n$$$ and replace $$$a_i$$$ with some integer in range $$$[1; k]$$$) to satisfy the following conditions: after all replacements, all $$$a_i$$$ are positive integers not greater than $$$k$$$; for all $$$i$$$ from $$$1$$$ to $$$frac{n}{2}$$$ the following equation is true: $$$a_i + a_{n - i + 1} = x$$$, where $$$x$$$ should be the same for all $$$frac{n}{2}$$$ pairs of elements. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 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$$$ ($$$2 le n le 2 cdot 10^5, 1 le k le 2 cdot 10^5$$$) — the length of $$$a$$$ and the maximum possible value of some $$$a_i$$$ correspondingly. It is guratanteed that $$$n$$$ is even (i.e. divisible by $$$2$$$). The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le k$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ (as well as the sum of $$$k$$$) over all test cases does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$, $$$sum k le 2 cdot 10^5$$$). Output For each test case, print the answer — the minimum number of elements you have to replace in $$$a$$$ to satisfy the conditions from the problem statement. Example Input 4 4 2 1 2 1 2 4 3 1 2 2 1 8 7 6 1 1 7 6 3 4 6 6 6 5 2 6 1 3 4 | 1,700 | false | true | false | false | true | false | true | false | false | false | 3,990 |
131C | Problem - 131C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics math *1400 No tag edit access → Contest materials ") ") ") ") . Output Find the required number of ways. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 5 2 5 Output 10 Input 4 3 5 Output 3 | 1,400 | true | false | false | false | false | false | false | false | false | false | 9,351 |
591B | The name of one small but proud corporation consists of _n_ lowercase English letters. The Corporation has decided to try rebrandingxa0— an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name. For this purpose the corporation has consecutively hired _m_ designers. Once a company hires the _i_-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters _x__i_ by _y__i_, and all the letters _y__i_ by _x__i_. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that _x__i_ coincides with _y__i_. The version of the name received after the work of the last designer becomes the new name of the corporation. Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive. Satisfy Arkady's curiosity and tell him the final version of the name. Input The first line of the input contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009200u2009000)xa0— the length of the initial name and the number of designers hired, respectively. The second line consists of _n_ lowercase English letters and represents the original name of the corporation. Next _m_ lines contain the descriptions of the designers' actions: the _i_-th of them contains two space-separated lowercase English letters _x__i_ and _y__i_. Output Print the new name of the corporation. Examples Input 11 6 abacabadaba a b b c a d e g f a b b Note In the second sample the name of the corporation consecutively changes as follows: | 1,200 | false | false | true | false | false | false | false | false | false | false | 7,483 |
578B | Problem - 578B - Codeforces =============== xa0 ]( "Codeforces Round #320 . The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009109). Output Output the maximum value of a bitwise OR of sequence elements after performing operations. Examples Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 Note For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | 1,700 | false | true | false | false | false | false | true | false | false | false | 7,535 |
912B | Since Grisha behaved well last year, at New Year's Eve he was visited by Ded Moroz who brought an enormous bag of gifts with him! The bag contains _n_ sweet candies from the good ol' bakery, each labeled from 1 to _n_ corresponding to its tastiness. No two candies have the same tastiness. The choice of candies has a direct effect on Grisha's happiness. One can assume that he should take the tastiest onesxa0— but no, the holiday magic turns things upside down. It is the xor-sum of tastinesses that matters, not the ordinary sum! A xor-sum of a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__m_ is defined as the bitwise XOR of all its elements: , here denotes the bitwise XOR operation; more about bitwise XOR can be found he can obtain. Input The sole string contains two integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u20091018). Output Output one numberxa0— the largest possible xor-sum. Note In the first sample case, one optimal answer is 1, 2 and 4, giving the xor-sum of 7. In the second sample case, one can, for example, take all six candies and obtain the xor-sum of 7. | 1,300 | false | false | false | false | false | true | false | false | false | false | 6,095 |
1840F | Tema is playing a very interesting computer game. During the next mission, Tema's character found himself on an unfamiliar planet. Unlike Earth, this planet is flat and can be represented as an $$$n imes m$$$ rectangle. Tema's character is located at the point with coordinates $$$(0, 0)$$$. In order to successfully complete the mission, he needs to reach the point with coordinates $$$(n, m)$$$ alive. Let the character of the computer game be located at the coordinate $$$(i, j)$$$. Every second, starting from the first, Tema can: either use vertical hyperjump technology, after which his character will end up at coordinate $$$(i + 1, j)$$$ at the end of the second; or use horizontal hyperjump technology, after which his character will end up at coordinate $$$(i, j + 1)$$$ at the end of the second; or Tema can choose not to make a hyperjump, in which case his character will not move during this second; The aliens that inhabit this planet are very dangerous and hostile. Therefore, they will shoot from their railguns $$$r$$$ times. Each shot completely penetrates one coordinate vertically or horizontally. If the character is in the line of its impact at the time of the shot (at the end of the second), he dies. Since Tema looked at the game's source code, he knows complete information about each shotxa0— the time, the penetrated coordinate, and the direction of the shot. What is the minimum time for the character to reach the desired point? If he is doomed to die and cannot reach the point with coordinates $$$(n, m)$$$, output $$$-1$$$. Input The first line of the input contains a single integer $$$T$$$ ($$$1 le T le 10^4$$$)xa0— the number of test cases. Then follow the descriptions of the test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n cdot m le 10^4$$$)xa0— the size of the planet, its height and width. The second line of each test case contains a single integer $$$r$$$ ($$$1 le r le 100$$$)xa0— the number of shots. Then follow $$$r$$$ lines, each describing one shot. A shot is described by three integers $$$t$$$, $$$d$$$, $$$coord$$$. Where $$$t$$$ is the second at which the shot will be fired ($$$1 le t le 10^9$$$). $$$d$$$ is the direction of the shot ($$$d = 1$$$ denotes a horizontal shot, $$$d = 2$$$ denotes a vertical shot). $$$coord$$$ is the size of the penetrated coordinate ($$$0 le coord le n$$$ for $$$d = 1$$$, $$$0 le coord le m$$$ for $$$d = 2$$$). The sum of the products $$$n cdot m$$$ over all test cases does not exceed $$$10^4$$$. Output For each test case, output a single numberxa0— the minimum time for the character to reach the coordinate $$$(n, m)$$$, or $$$-1$$$ if he is doomed to die. Example Input 5 1 3 4 1 2 0 2 2 1 3 2 2 4 1 1 3 3 6 2 1 0 2 1 1 2 1 2 2 2 0 2 2 1 2 2 2 2 1 3 7 1 2 2 1 1 7 2 1 2 2 5 9 1 2 3 2 0 5 1 2 4 2 2 7 1 0 4 6 7 6 1 2 12 1 3 4 1 0 17 2 3 1 2 6 16 2 6 3 2 4 Note In the first test case, the character can move as follows: $$$(0, 0) ightarrow (0, 1) ightarrow (0, 2) ightarrow (0, 3) ightarrow (0, 3) ightarrow (1, 3)$$$. In the second test case, the character will not be able to leave the rectangle that will be completely penetrated by shots at the second $$$2$$$. | 2,200 | false | false | false | true | false | false | true | false | false | true | 1,248 |
697B | Barney is standing in a bar and starring at a pretty girl. He wants to shoot her with his heart arrow but he needs to know the distance between him and the girl to make his shot accurate. Barney asked the bar tender Carl about this distance value, but Carl was so busy talking to the customers so he wrote the distance value (it's a real number) on a napkin. The problem is that he wrote it in scientific notation. The scientific notation of some real number _x_ is the notation of form _AeB_, where _A_ is a real number and _B_ is an integer and _x_u2009=u2009_A_u2009×u200910_B_ is true. In our case _A_ is between 0 and 9 and _B_ is non-negative. Barney doesn't know anything about scientific notation (as well as anything scientific at all). So he asked you to tell him the distance value in usual decimal representation with minimal number of digits after the decimal point (and no decimal point if it is an integer). See the output format for better understanding. Input The first and only line of input contains a single string of form _a_._deb_ where _a_, _d_ and _b_ are integers and _e_ is usual character 'e' (0u2009≤u2009_a_u2009≤u20099,u20090u2009≤u2009_d_u2009<u200910100,u20090u2009≤u2009_b_u2009≤u2009100)xa0— the scientific notation of the desired distance value. _a_ and _b_ contain no leading zeros and _d_ contains no trailing zeros (but may be equal to 0). Also, _b_ can not be non-zero if _a_ is zero. Output Print the only real number _x_ (the desired distance value) in the only line in its decimal notation. Thus if _x_ is an integer, print it's integer value without decimal part and decimal point and without leading zeroes. Otherwise print _x_ in a form of _p_._q_ such that _p_ is an integer that have no leading zeroes (but may be equal to zero), and _q_ is an integer that have no trailing zeroes (and may not be equal to zero). | 1,400 | true | false | true | false | false | false | true | false | false | false | 7,034 |
1794B | You are given an array of $$$n$$$ positive integers $$$a_1, a_2, ldots, a_n$$$. In one operation, you can choose any number of the array and add $$$1$$$ to it. Make at most $$$2n$$$ operations so that the array satisfies the following property: $$$a_{i+1}$$$ is not divisible by $$$a_i$$$, for each $$$i = 1, 2, ldots, n-1$$$. You do not need to minimize the number of operations. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1le nle 10^4$$$)xa0— the length of the given array. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1le a_ileq 10^9$$$)xa0— the given array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5cdot 10^4$$$. Output For each test case, print the answer on a separate line. In the only line, print $$$n$$$ integersxa0— the resulting array $$$a$$$ after applying at most $$$2n$$$ operations. We can show that an answer always exists under the given constraints. If there are multiple answers, print any of them. Note In the first test case, the array $$$[4, 5, 6, 7]$$$ can be achieved by applying $$$2$$$ operations to the first element, $$$1$$$ operation to the second element, $$$3$$$ operations to the third element, and $$$1$$$ operation to the last element. The total number of operations performed is $$$7$$$, which is less than the allowed $$$8$$$ operations in this case. In the second test case, the array $$$[3, 2, 3]$$$ can be achieved by applying two operations to the first element. Another possible resulting array could be $$$[2, 3, 5]$$$, because the total number of operations does not need to be minimum. In the third test case, not applying any operations results in an array that satisfies the statement's property. Observe that it is not mandatory to make operations. | 900 | true | true | false | false | false | true | false | false | false | false | 1,513 |
115E | You are a car race organizer and would like to arrange some races in Linear Kingdom. Linear Kingdom has _n_ consecutive roads spanning from left to right. The roads are numbered from 1 to _n_ from left to right, thus the roads follow in the order of their numbers' increasing. There will be several races that may be held on these roads. Each race will use a consecutive subset of these roads. Also, each race will pay some amount of money to you if this race is held. No races overlap in time, so some roads can be used in several races. Unfortunately, some of the roads are in a bad condition and they need repair. Each road has repair costs associated with it, you are required to pay this cost to repair the road. A race can only take place if all the roads used in the race are renovated. Your task is to repair such roads (possibly all or none) that will maximize your profit. Your profit is defined as the total money you get from the races that are held minus the total money you spent to repair the roads. Note that you may decide not to repair any road and gain zero profit. Print the maximum profit you can gain. Input The first line contains two single-space separated integers, _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092·105), denoting the number of roads and the number of races, respectively. Then _n_ lines follow, each line will contain a single non-negative integer not exceeding 109 denoting the cost to repair a road. The costs are given in order from road 1 to road _n_. Finally, _m_ lines follow. Each line is single-space-separated triplets of integers. Each triplet will be given as _lb_, _ub_, and _p_ (1u2009≤u2009_lb_u2009≤u2009_ub_u2009≤u2009_n_,u20091u2009≤u2009_p_u2009≤u2009109), which means that the race these three integers describe will use all the roads from _lb_ to _ub_, inclusive, and if it's held you get _p_. Output Print a single integer denoting the maximum possible profit you can gain. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is recommended to use cin, cout stream (also you may use %I64d specificator). Examples Input 7 4 3 2 3 2 1 2 3 1 2 5 2 3 5 3 5 3 7 7 5 Note In the first sample the optimal solution is to repair roads 1, 2, 3, and 7. Three races will take place which nets you 15. The road repair costs 11, hence your profit is 4. | 2,400 | false | false | false | true | true | false | false | false | false | false | 9,424 |
138B | Problem - 138B - 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 *1900 No tag edit access → Contest materials . At some point it turned out that if we sum the resulting numbers, then the number of zeroes with which the sum will end would be maximum among the possible variants of digit permutations in those numbers. Given number _n_, can you find the two digit permutations that have this property? Input The first line contains a positive integer _n_ — the original number. The number of digits in this number does not exceed 105. The number is written without any leading zeroes. Output Print two permutations of digits of number _n_, such that the sum of these numbers ends with the maximum number of zeroes. The permutations can have leading zeroes (if they are present, they all should be printed). The permutations do not have to be different. If there are several answers, print any of them. Examples Input 198 Output 981 819 Input 500 Output 500 500 | 1,900 | false | true | false | false | false | false | false | false | false | false | 9,324 |
677C | While walking down the street Vanya saw a label "Hide&Seek". Because he is a programmer, he used & as a bitwise AND for these two words represented as a integers in base 64 and got new word. Now Vanya thinks of some string _s_ and wants to know the number of pairs of words of length _s_ (length of _s_), such that their bitwise AND is equal to _s_. As this number can be large, output it modulo 109u2009+u20097. To represent the string as a number in numeral system with base 64 Vanya uses the following rules: digits from '0' to '9' correspond to integers from 0 to 9; letters from 'A' to 'Z' correspond to integers from 10 to 35; letters from 'a' to 'z' correspond to integers from 36 to 61; letter '-' correspond to integer 62; letter '_' correspond to integer 63. Input The only line of the input contains a single word _s_ (1u2009≤u2009_s_u2009≤u2009100u2009000), consisting of digits, lowercase and uppercase English letters, characters '-' and '_'. Output Print a single integerxa0— the number of possible pairs of words, such that their bitwise AND is equal to string _s_ modulo 109u2009+u20097. Note For a detailed definition of bitwise AND we recommend to take a look in the corresponding article in Wikipedia. In the first sample, there are 3 possible solutions: 1. _z_&_u2009=u200961&63u2009=u200961u2009=u2009_z_ 2. _&_z_u2009=u200963&61u2009=u200961u2009=u2009_z_ 3. _z_&_z_u2009=u200961&61u2009=u200961u2009=u2009_z_ | 1,500 | false | false | true | false | false | false | false | false | false | false | 7,119 |
92B | Problem - 92B - 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 *1300 No tag edit access → Contest materials , and then divide it by 2 two times in a row. As a result, we get 1. | 1,300 | false | true | false | false | false | false | false | false | false | false | 9,516 |
1894B | You are given an array $$$a_1, a_2, ldots, a_n$$$. You need to find an array $$$b_1, b_2, ldots, b_n$$$ consisting of numbers $$$1$$$, $$$2$$$, $$$3$$$ such that exactly two out of the following three conditions are satisfied: 1. There exist indices $$$1 leq i, j leq n$$$ such that $$$a_i = a_j$$$, $$$b_i = 1$$$, $$$b_j = 2$$$. 2. There exist indices $$$1 leq i, j leq n$$$ such that $$$a_i = a_j$$$, $$$b_i = 1$$$, $$$b_j = 3$$$. 3. There exist indices $$$1 leq i, j leq n$$$ such that $$$a_i = a_j$$$, $$$b_i = 2$$$, $$$b_j = 3$$$. If such an array does not exist, you should report it. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 leq t leq 500)$$$xa0— the number of test cases. Each test case is described as follows. The first line of each test case contains an integer $$$n$$$ $$$(1 leq n leq 100)$$$xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ $$$(1 leq a_i leq 100)$$$xa0— the elements of the array $$$a$$$. Output For each test case, print -1 if there is no solution. Otherwise, print $$$b_1, b_2, ldots, b_n$$$xa0— an array consisting of numbers $$$1$$$, $$$2$$$, $$$3$$$ that satisfies exactly two out of three conditions. If there are multiple possible answers, you can print any of them. Example Input 9 6 1 2 3 2 2 3 7 7 7 7 7 7 7 7 4 1 1 2 2 7 1 2 3 4 5 6 7 5 2 3 3 3 2 3 1 2 1 9 1 1 1 7 7 7 9 9 9 1 1 18 93 84 50 21 88 52 16 50 63 1 30 85 29 67 63 58 37 69 Output 1 2 3 1 1 1 -1 3 2 2 1 -1 2 1 2 1 3 -1 1 1 2 2 1 2 2 3 3 -1 3 2 1 3 3 3 3 2 2 1 1 2 3 1 3 1 1 2 Note In the first test case, $$$b = [1, 2, 3, 1, 1, 1]$$$ satisfies condition $$$1$$$ because for $$$i = 4$$$, $$$j = 2$$$: $$$a_i = a_j$$$, $$$b_i = 1$$$, and $$$b_j = 2$$$. It also satisfies condition $$$2$$$ because for $$$i = 6$$$, $$$j = 3$$$: $$$a_i = a_j$$$, $$$b_i = 1$$$, and $$$b_j = 3$$$. However, it does not satisfy condition $$$3$$$. In total, exactly two out of three conditions are satisfied. | 1,000 | false | false | false | false | false | true | false | false | false | false | 945 |
1634B | Haha, try to solve this, SelectorUnlimited! — antontrygubO_o Your friends Alice and Bob practice fortune telling. Fortune telling is performed as follows. There is a well-known array $$$a$$$ of $$$n$$$ non-negative integers indexed from $$$1$$$ to $$$n$$$. The tellee starts with some non-negative number $$$d$$$ and performs one of the two operations for each $$$i = 1, 2, ldots, n$$$, in the increasing order of $$$i$$$. The possible operations are: replace their current number $$$d$$$ with $$$d + a_i$$$ replace their current number $$$d$$$ with $$$d oplus a_i$$$ (hereinafter $$$oplus$$$ denotes the could get the number $$$y$$$ after performing the operations. It is guaranteed that on the jury tests, exactly one of your friends could have actually gotten that number. Hacks You cannot make hacks in this problem. Input On the first line of the input, you are given one number $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The following $$$2 cdot t$$$ lines contain test cases. The first line of each test case contains three numbers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 le n le 10^5$$$, $$$0 le x le 10^9$$$, $$$0 le y le 10^{15}$$$)xa0— the length of array $$$a$$$, Alice's initial number (Bob's initial number is therefore $$$x+3$$$), and the number that one of the two friends got in the end. The second line of each test case contains $$$n$$$ numbersxa0— the array $$$a$$$ ($$$0 le a_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print the name of the friend who could get the number $$$y$$$: "Alice" or "Bob". Example Input 4 1 7 9 2 2 0 2 1 3 4 0 1 1 2 3 4 2 1000000000 3000000000 1000000000 1000000000 Output Alice Alice Bob Alice Note In the first test case, Alice could get $$$9$$$ using the following operations: $$$7 + 2 = 9$$$. In the second test case, Alice could get $$$2$$$ using this operations: $$$(0 + 1) oplus 3 = 2$$$. In the third test case, Bob started with $$$x+3 = 0+3=3$$$ and could get $$$1$$$ this way: $$$(((3 + 1) + 2) oplus 3) oplus 4 = 1$$$. | 1,400 | true | false | false | false | false | false | false | false | false | false | 2,445 |
1566D2 | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Global Round 16 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 greedy implementation sortings two pointers *1600 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST D2. Seating Arrangements (hard version) time limit per test1 second memory limit per test256 megabytes It is the hard version of the problem. The only difference is that in this version $$$1 le n le 300$$$. In the cinema seats can be represented as the table with $$$n$$$ rows and $$$m$$$ columns. The rows are numbered with integers from $$$1$$$ to $$$n$$$. The seats in each row are numbered with consecutive integers from left to right: in the $$$k$$$-th row from $$$m (k - 1) + 1$$$ to $$$m k$$$ for all rows $$$1 le k le n$$$. $$$1$$$ $$$2$$$ $$$cdots$$$ $$$m - 1$$$ $$$m$$$ $$$m + 1$$$ $$$m + 2$$$ $$$cdots$$$ $$$2 m - 1$$$ $$$2 m$$$ $$$2m + 1$$$ $$$2m + 2$$$ $$$cdots$$$ $$$3 m - 1$$$ $$$3 m$$$ $$$vdots$$$ $$$vdots$$$ $$$ddots$$$ $$$vdots$$$ $$$vdots$$$ $$$m (n - 1) + 1$$$ $$$m (n - 1) + 2$$$ $$$cdots$$$ $$$n m - 1$$$ $$$n m$$$ The table with seats indices There are $$$nm$$$ people who want to go to the cinema to watch a new film. They are numbered with integers from $$$1$$$ to $$$nm$$$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $$$i$$$-th person has the level of sight $$$a_i$$$. Let's define $$$s_i$$$ as the seat index, that will be given to $$$i$$$-th person. You want to give better places for people with lower sight levels, so for any two people $$$i$$$, $$$j$$$ such that $$$a_i < a_j$$$ it should be satisfied that $$$s_i < s_j$$$. After you will give seats to all people they will start coming to their seats. In the order from $$$1$$$ to $$$nm$$$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $$$m = 5$$$, the person has the seat $$$4$$$ in the first row, the seats $$$1$$$, $$$3$$$, $$$5$$$ in the first row are already occupied, the seats $$$2$$$ and $$$4$$$ are free. The inconvenience of this person will be $$$2$$$, because he will go through occupied seats $$$1$$$ and $$$3$$$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 300$$$)xa0— the number of rows and places in each row respectively. The second line of each test case contains $$$n cdot m$$$ integers $$$a_1, a_2, ldots, a_{n cdot m}$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the sight level of $$$i$$$-th person. It's guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print a single integerxa0— the minimal total inconvenience that can be achieved. Example input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 output 1 0 4 0 0 0 1 Note In the first test case, there is a single way to give seats: the first person sits in the first place and the second personxa0— in the second. The total inconvenience is $$$1$$$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place. Codeforces (c) | 1,600 | false | true | true | false | true | false | false | false | true | false | 2,809 |
774J | Well, the series which Stepan watched for a very long time, ended. In total, the series had _n_ episodes. For each of them, Stepan remembers either that he definitely has watched it, or that he definitely hasn't watched it, or he is unsure, has he watched this episode or not. Stepan's dissatisfaction is the maximum number of consecutive series that Stepan did not watch. Your task is to determine according to Stepan's memories if his dissatisfaction could be exactly equal to _k_. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009100, 0u2009≤u2009_k_u2009≤u2009_n_) — the number of episodes in the series and the dissatisfaction which should be checked. The second line contains the sequence which consists of _n_ symbols "Y", "N" and "?". If the _i_-th symbol equals "Y", Stepan remembers that he has watched the episode number _i_. If the _i_-th symbol equals "N", Stepan remembers that he hasn't watched the epizode number _i_. If the _i_-th symbol equals "?", Stepan doesn't exactly remember if he has watched the episode number _i_ or not. Output If Stepan's dissatisfaction can be exactly equal to _k_, then print "YES" (without qoutes). Otherwise print "NO" (without qoutes). Note In the first test Stepan remembers about all the episodes whether he has watched them or not. His dissatisfaction is 2, because he hasn't watch two episodes in a row — the episode number 3 and the episode number 4. The answer is "YES", because _k_u2009=u20092. In the second test _k_u2009=u20091, Stepan's dissatisfaction is greater than or equal to 2 (because he remembers that he hasn't watch at least two episodes in a row — number 5 and number 6), even if he has watched the episodes from the first to the fourth, inclusive. | 2,000 | false | false | false | true | false | false | false | false | false | false | 6,672 |
1743C | Monocarp has been collecting rare magazines for quite a while, and now he has decided to sell them. He distributed the magazines between $$$n$$$ boxes, arranged in a row. The $$$i$$$-th box contains $$$a_i$$$ magazines. Some of the boxes are covered with lids, others are not. Suddenly it started to rain, and now Monocarp has to save as many magazines from the rain as possible. To do this, he can move the lids between boxes as follows: if the $$$i$$$-th box was covered with a lid initially, he can either move the lid from the $$$i$$$-th box to the box $$$(i-1)$$$ (if it exists), or keep the lid on the $$$i$$$-th box. You may assume that Monocarp can move the lids instantly at the same moment, and no lid can be moved more than once. If a box will be covered with a lid after Monocarp moves the lids, the magazines in it will be safe from the rain; otherwise they will soak. You have to calculate the maximum number of magazines Monocarp can save from the rain. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of the testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of boxes. The second line contains a string of $$$n$$$ characters 0 and/or 1. If the $$$i$$$-th character is 1, the $$$i$$$-th box is initially covered with a lid. If the $$$i$$$-th character is 0, the $$$i$$$-th box is initially not covered. The third line contains a sequence of integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^4$$$), where $$$a_i$$$ is the number of magazines in the $$$i$$$-th box. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print one integerxa0— the maximum number of magazines Monocarp can save from the rain. Example Input 4 5 01110 10 5 8 9 6 6 011011 20 10 9 30 20 19 4 0000 100 100 100 100 4 0111 5 4 5 1 Note In the first testcase of the example, Monocarp can move the lid from the second box to the first box, so the boxes $$$1$$$, $$$3$$$ and $$$4$$$ are covered, and $$$10 + 8 + 9 = 27$$$ magazines are saved. In the second testcase, Monocarp can move the lid from the second box to the first box, then from the third box to the second box, then from the fifth box to the fourth box, and then from the sixth box to the fifth box. The boxes $$$1$$$, $$$2$$$, $$$4$$$ and $$$5$$$ will be covered, so $$$20 + 10 + 30 + 20 = 80$$$ magazines can be saved. There are no lids in the third testcase, so it's impossible to save even a single magazine. | 1,100 | false | true | false | true | false | true | false | false | false | false | 1,822 |
979A | Katie, Kuro and Shiro are best friends. They have known each other since kindergarten. That's why they often share everything with each other and work together on some very hard problems. Today is Shiro's birthday. She really loves pizza so she wants to invite her friends to the pizza restaurant near her house to celebrate her birthday, including her best friends Katie and Kuro. She has ordered a very big round pizza, in order to serve her many friends. Exactly $$$n$$$ of Shiro's friends are here. That's why she has to divide the pizza into $$$n + 1$$$ slices (Shiro also needs to eat). She wants the slices to be exactly the same size and shape. If not, some of her friends will get mad and go home early, and the party will be over. Shiro is now hungry. She wants to cut the pizza with minimum of straight cuts. A cut is a straight segment, it might have ends inside or outside the pizza. But she is too lazy to pick up the calculator. As usual, she will ask Katie and Kuro for help. But they haven't come yet. Could you help Shiro with this problem? Input A single line contains one non-negative integer $$$n$$$ ($$$0 le n leq 10^{18}$$$)xa0— the number of Shiro's friends. The circular pizza has to be sliced into $$$n + 1$$$ pieces. Output A single integerxa0— the number of straight cuts Shiro needs. Note To cut the round pizza into quarters one has to make two cuts through the center with angle $$$90^{circ}$$$ between them. To cut the round pizza into five equal parts one has to make five cuts. | 1,000 | true | false | false | false | false | false | false | false | false | false | 5,811 |
827D | Problem - 827D - 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 dfs and similar graphs trees *2700 No tag edit access → Contest materials , where _n_ and _m_ are the number of vertices and the number of edges in the graph, respectively. Each of the next _m_ lines contains three integers _u_, _v_ and _c_ (1u2009≤u2009_v_,u2009_u_u2009≤u2009_n_, _v_u2009≠u2009_u_, 1u2009≤u2009_c_u2009≤u2009109) meaning that there is an edge between vertices _u_ and _v_ with weight _c_. Output Print the answer for each edge in the order the edges are given in the input. If an edge is contained in every minimum spanning tree with any weight, print -1 as the answer. Examples Input 4 4 1 2 2 2 3 2 3 4 2 4 1 3 Output 2 2 2 1 Input 4 3 1 2 2 2 3 2 3 4 2 Output -1 -1 -1 | 2,700 | false | false | false | false | true | false | false | false | false | true | 6,452 |
1900E | You are given a directed graph $$$G$$$ with $$$n$$$ vertices and $$$m$$$ edges between them. Initially, graph $$$H$$$ is the same as graph $$$G$$$. Then you decided to perform the following actions: If there exists a triple of vertices $$$a$$$, $$$b$$$, $$$c$$$ of $$$H$$$, such that there is an edge from $$$a$$$ to $$$b$$$ and an edge from $$$b$$$ to $$$c$$$, but there is no edge from $$$a$$$ to $$$c$$$, add an edge from $$$a$$$ to $$$c$$$. Repeat the previous step as long as there are such triples. Note that the number of edges in $$$H$$$ can be up to $$$n^2$$$ after performing the actions. You also wrote some values on vertices of graph $$$H$$$. More precisely, vertex $$$i$$$ has the value of $$$a_i$$$ written on it. Consider a simple path consisting of $$$k$$$ distinct vertices with indexes $$$v_1, v_2, ldots, v_k$$$. The length of such a path is $$$k$$$. The value of that path is defined as $$$sum_{i = 1}^k a_{v_i}$$$. A simple path is considered the longest if there is no other simple path in the graph with greater length. Among all the longest simple paths in $$$H$$$, find the one with the smallest value. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n,m le 2 cdot 10^5$$$)xa0— the number of vertices and the number of edges. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$)xa0— the numbers written on the vertices of graph $$$H$$$. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 le v_i, u_i le n$$$)xa0— meaning that there is an edge going from vertex $$$v_i$$$ to vertex $$$u_i$$$ in graph $$$G$$$. Note that edges are directed. Also note that the graph may have self-loops and multiple edges. 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 two numbersxa0— the length of the longest simple path in $$$H$$$ and the minimal possible value of such path. Example Input 3 5 6 2 2 4 1 3 1 2 1 3 2 4 3 4 4 5 5 2 7 7 999999999 999999999 999999999 999999999 1000000000 999999999 1000000000 1 2 2 3 3 4 4 1 4 5 4 6 6 7 14 22 2 3 5 7 3 4 1 4 3 4 2 2 5 1 1 2 2 3 2 4 3 1 4 4 4 5 5 6 5 6 5 12 6 7 6 8 7 5 7 7 7 9 8 4 9 11 10 9 11 10 11 10 12 13 13 14 14 12 Output 5 12 6 5999999995 11 37 Note In the first test case, the longest path in both graphs is $$$1 o 3 o 4 o 5 o 2$$$. As the path includes all vertices, the minimal possible value of the longest path is the sum of values on all vertices, which is $$$12$$$. In the second test case, the longest possible path is $$$1 o 2 o 3 o 4 o 6 o 7$$$. As there are no longest paths with vertex $$$5$$$ in them, this path has the minimal possible value of $$$5,999,999,995$$$. In the third test case, it can be proven that there is no path longer than $$$11$$$ and that the value of the longest path cannot be less than $$$37$$$. Also, notice that the given graph has both self-loops and multiple edges. | 2,100 | false | false | true | true | false | false | false | false | false | true | 911 |
14D | Problem - 14D - 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 dfs and similar dp graphs shortest paths trees two pointers *1900 No tag edit access → Contest materials ") . It is known that the profit, the «Two Paths» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company. Input The first line contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009200), where _n_ is the amount of cities in the country. The following _n_u2009-u20091 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_). Output Output the maximum possible profit. Examples Input 4 1 2 2 3 3 4 Output 1 Input 7 1 2 1 3 1 4 1 5 1 6 1 7 Output 0 Input 6 1 2 2 3 2 4 5 4 6 4 Output 4 | 1,900 | false | false | false | true | false | false | false | false | false | true | 9,922 |
1943E2 | This is the hard version of the problem. The only difference between the two versions is the constraint on $$$t$$$, $$$m$$$ and the sum of $$$m$$$. You can make hacks only if both versions of the problem are solved. Alice and Bob play yet another game on an array $$$a$$$ of size $$$n$$$. Alice starts with an empty array $$$c$$$. Both players take turns playing, with Alice starting first. On Alice's turn, she picks one element from $$$a$$$, appends that element to $$$c$$$, and then deletes it from $$$a$$$. On Bob's turn, he picks at most $$$k$$$ elements from $$$a$$$, and then deletes it from $$$a$$$. The game ends when the array $$$a$$$ is empty. Alice's score is defined to be the MEX$$$^dagger$$$ of $$$c$$$. Alice wants to maximize her score while Bob wants to minimize it. Find Alice's final score if both players play optimally. The array will be given in compressed format. Instead of giving the elements present in the array, we will be giving their frequencies. Formally, you will be given $$$m$$$, the maximum element in the array, and then $$$m + 1$$$ integers $$$f_0, f_1, ldots, f_{m}$$$, where $$$f_i$$$ represents the number of times $$$i$$$ occurs in the array $$$a$$$. $$$^dagger$$$ The $$$operatorname{MEX}$$$ (minimum excludant) of an array of integers is defined as the smallest non-negative integer which does not occur in the array. For example: The MEX of $$$[2,2,1]$$$ is $$$0$$$, because $$$0$$$ does not belong to the array. The MEX of $$$[3,1,0,1]$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the array, but $$$2$$$ does not. The MEX of $$$[0,3,1,2]$$$ is $$$4$$$, because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the array, but $$$4$$$ does not. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$m$$$ and $$$k$$$ ($$$1 le m le 2 cdot 10^5, 1 le k le 10^9$$$). The second line contains $$$m + 1$$$ integers $$$f_0, f_1, ldots, f_m$$$ ($$$1 le f_i le 10^9$$$). It is guaranteed the sum of $$$m$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, find Alice's score if both players play optimally. Example Input 5 1 4 4 5 2 1000000000 1000000000 1000000000 1000000000 3 2 2 3 100 1 1 1 2 2 3 1 1 1 1 1 Note In the first test case, the array $$$a$$$ is $$$[0, 0, 0, 0, 1, 1, 1, 1, 1]$$$. A possible game with a score of $$$2$$$ is as follows: 1. Alice chooses the element $$$0$$$. After this move, $$$a = [0, 0, 0, 1, 1, 1, 1, 1]$$$ and $$$c=[0]$$$. 2. Bob chooses to remove the $$$3$$$ elements $$$0$$$, $$$0$$$ and $$$1$$$. After this move, $$$a = [0, 1, 1, 1, 1]$$$ and $$$c=[0]$$$. 3. Alice chooses the element $$$1$$$. After this move, $$$a = [0,1,1,1]$$$ and $$$c=[0,1]$$$. 4. Bob removes the $$$4$$$ remaining elements $$$0$$$, $$$1$$$, $$$1$$$ and $$$1$$$. After this move, $$$a=[,]$$$ and $$$c=[0,1]$$$. At the end, $$$c=[0,1]$$$ which has a MEX of $$$2$$$. Note that this is an example game and does not necessarily represent the optimal strategy for both players. In the second test case, Alice can choose a $$$0$$$ in her first turn, guaranteeing that her score is at least $$$1$$$. While Bob can remove all copies element $$$1$$$ in his first turn, thus guaranteeing that Alice's score cannot exceed $$$1$$$. So Alice's score is $$$1$$$ if both players play optimally. | 3,300 | false | true | false | false | false | false | false | true | false | false | 617 |
2005E2 | This is the hard version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved. Tsovak and Narek are playing a game. They have an array $$$a$$$ and a matrix $$$b$$$ of integers with $$$n$$$ rows and $$$m$$$ columns, numbered from $$$1$$$. The cell in the $$$i$$$-th row and the $$$j$$$-th column is $$$(i, j)$$$. They are looking for the elements of $$$a$$$ in turns; Tsovak starts first. Each time a player looks for a cell in the matrix containing the current element of $$$a$$$ (Tsovak looks for the first, then Narek looks for the second, etc.). Let's say a player has chosen the cell $$$(r, c)$$$. The next player has to choose his cell in the submatrix starting at $$$(r + 1, c + 1)$$$ and ending in $$$(n, m)$$$ (the submatrix can be empty if $$$r=n$$$ or $$$c=m$$$). If a player cannot find such a cell (or the remaining submatrix is empty) or the array ends (the previous player has chosen the last element), then he loses. Your task is to determine the winner if the players play optimally. Note: since the input is large, you may need to optimize input/output for this problem. For example, in C++, it is enough to use the following lines at the start of the main() function: ``` int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);} ``` Input The first line of the input contains $$$t$$$ ($$$1 le t le 1500$$$)xa0– the number of test cases. The first line of each test case contains three integers $$$l$$$, $$$n$$$, and $$$m$$$ ($$$1 le l, n, m le 1500$$$)xa0– the size of the array and the sizes of the matrix. The second line contains $$$l$$$ integers $$$a_1, a_2, a_3, ldots a_l$$$ ($$$1 le a_i le n cdot m$$$)xa0– the elements of the array $$$a$$$. The $$$i$$$-th of the last $$$n$$$ lines contains $$$m$$$ integers $$$b_{i,1}, b_{i,2}, b_{i,3}, ldots b_{i,m}$$$ ($$$1 le b_{i,j} le n cdot m$$$)xa0– representing the $$$i$$$-th row of the matrix. It is guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$3 cdot 10^6$$$. It is guaranteed that the sum of $$$l$$$ over all test cases does not exceed $$$1500$$$. Output You should output $$$t$$$ lines, the $$$i$$$-th of them containing a character representing the answer of the $$$i$$$-th test case: "T" if Tsovak wins or "N", otherwise (without quotes). Example Input 3 2 2 3 1 2 1 3 6 4 6 2 2 2 4 1 2 1 1 3 2 4 2 5 1 2 4 2 1 2 3 4 5 6 7 8 8 8 Note In the first example, Tsovak starts by looking for $$$1$$$. There is only one occurrence of $$$1$$$ at $$$(1,1)$$$, so he chooses it. Then Narek needs to look for $$$2$$$ in the submatrix of $$$(2, 2)$$$, which consists of just the last two elements: $$$6$$$ and $$$2$$$. He chooses $$$2$$$, and then Tsovak loses since the array has ended. In the second example, Tsovak needs to choose $$$1$$$. There is a $$$1$$$ at the cell $$$(n,m)$$$, so he chooses that cell. Then, since the submatrix of $$$(n + 1, m + 1)$$$ is empty, Narek cannot find $$$2$$$, so he loses. | 2,500 | false | true | true | true | true | false | false | false | false | false | 218 |
1761F1 | This is the easy version of the problem. The only difference between the two versions is the constraint on $$$n$$$. You can make hacks only if all versions of the problem are solved. Let's call an array $$$a$$$ of odd length $$$2m+1$$$ (with $$$m ge 1$$$) bad, if element $$$a_{m+1}$$$ is equal to the median of this array. In other words, the array is bad if, after sorting it, the element at $$$m+1$$$-st position remains the same. Let's call a permutation $$$p$$$ of integers from $$$1$$$ to $$$n$$$ anti-median, if every its subarray of odd length $$$ge 3$$$ is not bad. You are already given values of some elements of the permutation. Find the number of ways to set unknown values to obtain an anti-median permutation. As this number can be very large, find it modulo $$$10^9+7$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) 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 1000)$$$ xa0— the length of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$, or $$$p_i = -1$$$) xa0— the elements of the permutation. If $$$p_i eq -1$$$, it's given, else it's unknown. It's guaranteed that if for some $$$i eq j$$$ holds $$$p_i eq -1, p_j eq -1$$$, then $$$p_i eq p_j$$$. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$10^6$$$. Note In the first test case, both $$$[1, 2]$$$ and $$$[2, 1]$$$ are anti-median. In the second test case, permutations $$$[1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2]$$$ are anti-median. The remaining two permutations, $$$[1, 2, 3]$$$, $$$[3, 2, 1]$$$, are bad arrays on their own, as their median, $$$2$$$, is in their middle. In the third test case, $$$[1, 2, 3, 4]$$$ isn't anti-median, as it contains bad subarray $$$[1, 2, 3]$$$. In the fourth test case, the only anti-median array you can get is $$$[5, 6, 3, 4, 1, 2]$$$. | 3,100 | true | false | false | true | false | false | false | false | false | false | 1,743 |
1693B | We are given a rooted tree consisting of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. The root of the tree is the vertex $$$1$$$ and the parent of the vertex $$$v$$$ is $$$p_v$$$. There is a number written on each vertex, initially all numbers are equal to $$$0$$$. Let's denote the number written on the vertex $$$v$$$ as $$$a_v$$$. For each $$$v$$$, we want $$$a_v$$$ to be between $$$l_v$$$ and $$$r_v$$$ $$$(l_v leq a_v leq r_v)$$$. In a single operation we do the following: Choose some vertex $$$v$$$. Let $$$b_1, b_2, ldots, b_k$$$ be vertices on the path from the vertex $$$1$$$ to vertex $$$v$$$ (meaning $$$b_1 = 1$$$, $$$b_k = v$$$ and $$$b_i = p_{b_{i + 1}}$$$). Choose a non-decreasing array $$$c$$$ of length $$$k$$$ of nonnegative integers: $$$0 leq c_1 leq c_2 leq ldots leq c_k$$$. For each $$$i$$$ $$$(1 leq i leq k)$$$, increase $$$a_{b_i}$$$ by $$$c_i$$$. What's the minimum number of operations needed to achieve our goal? Input The first line contains an integer $$$t$$$ $$$(1le tle 1000)$$$ 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$$$ $$$(2le nle 2 cdot 10^5)$$$ xa0— the number of the vertices in the tree. The second line of each test case contains $$$n - 1$$$ integers, $$$p_2, p_3, ldots, p_n$$$ $$$(1 leq p_i < i)$$$, where $$$p_i$$$ denotes the parent of the vertex $$$i$$$. The $$$i$$$-th of the following $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ $$$(1 le l_i le r_i le 10^9)$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case output the minimum number of operations needed. Example Input 4 2 1 1 5 2 9 3 1 1 4 5 2 4 6 10 4 1 2 1 6 9 5 6 4 5 2 4 5 1 2 3 4 5 5 4 4 3 3 2 2 1 1 Note In the first test case, we can achieve the goal with a single operation: choose $$$v = 2$$$ and $$$c = [1, 2]$$$, resulting in $$$a_1 = 1, a_2 = 2$$$. In the second test case, we can achieve the goal with two operations: first, choose $$$v = 2$$$ and $$$c = [3, 3]$$$, resulting in $$$a_1 = 3, a_2 = 3, a_3 = 0$$$. Then, choose $$$v = 3, c = [2, 7]$$$, resulting in $$$a_1 = 5, a_2 = 3, a_3 = 7$$$. | 1,700 | false | true | false | true | false | false | false | false | false | false | 2,121 |
1443C | Petya is preparing for his birthday. He decided that there would be $$$n$$$ different dishes on the dinner table, numbered from $$$1$$$ to $$$n$$$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $$$n$$$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant $$$i$$$, in this case the courier will arrive in $$$a_i$$$ minutes, Petya goes to the restaurant $$$i$$$ on his own and picks up the dish, he will spend $$$b_i$$$ minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently. For example, if Petya wants to order $$$n = 4$$$ dishes and $$$a = [3, 7, 4, 5]$$$, and $$$b = [2, 1, 2, 4]$$$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $$$3$$$ minutes, the courier of the fourth restaurant will bring the order in $$$5$$$ minutes, and Petya will pick up the remaining dishes in $$$1 + 2 = 3$$$ minutes. Thus, in $$$5$$$ minutes all the dishes will be at Petya's house. Find the minimum time after which all the dishes can be at Petya's home. Input The first line contains one positive integer $$$t$$$ ($$$1 le t le 2 cdot 10^5$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. Each test case begins with a line containing one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of dishes that Petya wants to order. The second line of each test case contains $$$n$$$ integers $$$a_1 ldots a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the time of courier delivery of the dish with the number $$$i$$$. The third line of each test case contains $$$n$$$ integers $$$b_1 ldots b_n$$$ ($$$1 le b_i le 10^9$$$)xa0— the time during which Petya will pick up the dish with the number $$$i$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. | 1,400 | false | true | false | false | false | false | false | true | true | false | 3,456 |
412B | The R1 company wants to hold a web search championship. There were _n_ computers given for the competition, each of them is connected to the Internet. The organizers believe that the data transfer speed directly affects the result. The higher the speed of the Internet is, the faster the participant will find the necessary information. Therefore, before the competition started, each computer had its maximum possible data transfer speed measured. On the _i_-th computer it was _a__i_ kilobits per second. There will be _k_ participants competing in the championship, each should get a separate computer. The organizing company does not want any of the participants to have an advantage over the others, so they want to provide the same data transfer speed to each participant's computer. Also, the organizers want to create the most comfortable conditions for the participants, so the data transfer speed on the participants' computers should be as large as possible. The network settings of the R1 company has a special option that lets you to cut the initial maximum data transfer speed of any computer to any lower speed. How should the R1 company configure the network using the described option so that at least _k_ of _n_ computers had the same data transfer speed and the data transfer speed on these computers was as large as possible? Input The first line contains two space-separated integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009100) — the number of computers and the number of participants, respectively. In the second line you have a space-separated sequence consisting of _n_ integers: _a_1,u2009_a_2,u2009...,u2009_a__n_ (16u2009≤u2009_a__i_u2009≤u200932768); number _a__i_ denotes the maximum data transfer speed on the _i_-th computer. Output Print a single integer — the maximum Internet speed value. It is guaranteed that the answer to the problem is always an integer. Examples Input 6 4 100 20 40 20 50 50 Note In the first test case the organizers can cut the first computer's speed to 30 kilobits. Then two computers (the first and the third one) will have the same speed of 30 kilobits. They should be used as the participants' computers. This answer is optimal. | 900 | false | true | false | false | false | false | false | false | true | false | 8,178 |
1886B | Monocarp tries to get home from work. He is currently at the point $$$O = (0, 0)$$$ of a two-dimensional plane; his house is at the point $$$P = (P_x, P_y)$$$. Unfortunately, it is late in the evening, so it is very dark. Monocarp is afraid of the darkness. He would like to go home along a path illuminated by something. Thankfully, there are two lanterns, located in the points $$$A = (A_x, A_y)$$$ and $$$B = (B_x, B_y)$$$. You can choose any non-negative number $$$w$$$ and set the power of both lanterns to $$$w$$$. If a lantern's power is set to $$$w$$$, it illuminates a circle of radius $$$w$$$ centered at the lantern location (including the borders of the circle). You have to choose the minimum non-negative value $$$w$$$ for the power of the lanterns in such a way that there is a path from the point $$$O$$$ to the point $$$P$$$ which is completely illuminated. You may assume that the lanterns don't interfere with Monocarp's movement. The picture for the first two test cases Input The first line of the input 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 two integers $$$P_x$$$ and $$$P_y$$$ ($$$-10^3 le P_x, P_y le 10^3$$$) — the location of Monocarp's house; the second line contains two integers $$$A_x$$$ and $$$A_y$$$ ($$$-10^3 le A_x, A_y le 10^3$$$) — the location of the first lantern; the third line contains two integers $$$B_x$$$ and $$$B_y$$$ ($$$-10^3 le B_x, B_y le 10^3$$$) — the location of the second lantern. Additional constraint on the input: in each test case, the points $$$O$$$, $$$P$$$, $$$A$$$ and $$$B$$$ are different from each other. Output For each test case, print the answer on a separate line — one real number equal to the minimum value of $$$w$$$ such that there is a completely illuminated path from the point $$$O$$$ to the point $$$P$$$. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$ — formally, if your answer is $$$a$$$, and the jury's answer is $$$b$$$, your answer will be accepted if $$$dfrac{a - b}{max(1, b)} le 10^{-6}$$$. Example Input 2 3 3 1 0 -1 6 3 3 -1 -1 4 3 Output 3.6055512755 3.2015621187 | 1,200 | true | false | false | false | false | false | false | true | false | false | 976 |
1932F | There is a fun game where you need to feed cats that come and go. The level of the game consists of $$$n$$$ steps. There are $$$m$$$ cats; the cat $$$i$$$ is present in steps from $$$l_i$$$ to $$$r_i$$$, inclusive. In each step, you can feed all the cats that are currently present or do nothing. If you feed the same cat more than once, it will overeat, and you will immediately lose the game. Your goal is to feed as many cats as possible without causing any cat to overeat. Find the maximum number of cats you can feed. Formally, you need to select several integer points from the segment from $$$1$$$ to $$$n$$$ in such a way that among given segments, none covers two or more of the selected points, and as many segments as possible cover one of the selected points. Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^6$$$, $$$1 le mle 2cdot 10^5$$$). The $$$i$$$-th of the next $$$m$$$ lines contains a pair of integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$). The sum of $$$n$$$ for all tests does not exceed $$$10^6$$$, the sum of $$$m$$$ for all tests does not exceed $$$2cdot 10^5$$$. Output For each test case, print a single integer, the maximum number of cats you can feed. Example Input 3 15 6 2 10 3 5 2 4 7 7 8 12 11 11 1000 1 1 1000 5 10 1 2 3 4 3 4 3 4 3 4 1 1 1 2 3 3 3 4 3 4 Note In the first example, one of the ways to feed five cats is to feed at steps $$$4$$$ and $$$11$$$. At step $$$4$$$, cats $$$1$$$, $$$2$$$, and $$$3$$$ will be fed. At step $$$11$$$, cats $$$5$$$ and $$$6$$$ will be fed. | 1,900 | false | false | false | true | true | false | false | false | true | false | 689 |
534B | The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals _v_1 meters per second, and in the end it is _v_2 meters per second. We know that this section of the route took exactly _t_ seconds to pass. Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by _d_ meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed _d_ in absolute value), find the maximum possible length of the path section in meters. Input The first line contains two integers _v_1 and _v_2 (1u2009≤u2009_v_1,u2009_v_2u2009≤u2009100) — the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively. The second line contains two integers _t_ (2u2009≤u2009_t_u2009≤u2009100) — the time when the car moves along the segment in seconds, _d_ (0u2009≤u2009_d_u2009≤u200910) — the maximum value of the speed change between adjacent seconds. It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals _v_1, the speed in the last second equals _v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed _d_. Output Print the maximum possible length of the path segment in meters. Note In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5u2009+u20097u2009+u20098u2009+u20096u2009=u200926 meters. In the second sample, as _d_u2009=u20090, the car covers the whole segment at constant speed _v_u2009=u200910. In _t_u2009=u200910 seconds it covers the distance of 100 meters. | 1,400 | true | true | false | true | false | false | false | false | false | false | 7,700 |
721B | Vanya is managed to enter his favourite site Codehorses. Vanya uses _n_ distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration. Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice. Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password _k_ times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that. Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds). Input The first line of the input contains two integers _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009100)xa0— the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds. The next _n_ lines contains passwords, one per linexa0— pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters. The last line of the input contains the Vanya's Codehorses password. It is guaranteed that the Vanya's Codehorses password is equal to some of his _n_ passwords. Output Print two integersxa0— time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively. Examples Input 5 2 cba abc bb1 abC ABC abc Note Consider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5 seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds. Consider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2 and only then the right one, spending 4 seconds at all. | 1,100 | true | false | true | false | false | false | false | false | true | false | 6,930 |
1700C | Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $$$n$$$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $$$a_1, a_2, dots, a_n$$$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $$$i$$$ and decrease the level of moisture of the trees $$$1, 2, dots, i$$$ by $$$1$$$. Choose a position $$$i$$$ and decrease the level of moisture of the trees $$$i, i + 1, dots, n$$$ by $$$1$$$. Increase the level of moisture of all trees by $$$1$$$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $$$0$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) xa0— the number of test cases. The description of $$$t$$$ test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 200,000$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^9 leq a_i leq 10^9$$$) — the initial levels of trees moisture. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$200,000$$$. Output For each test case output a single integer — the minimum number of actions. It can be shown that the answer exists. Example Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Note In the first test case it's enough to apply the operation of adding $$$1$$$ to the whole array $$$2$$$ times. In the second test case you can apply the operation of decreasing $$$4$$$ times on the prefix of length $$$3$$$ and get an array $$$6, 0, 3$$$. After that apply the operation of decreasing $$$6$$$ times on the prefix of length $$$1$$$ and $$$3$$$ times on the suffix of length $$$1$$$. In total, the number of actions will be $$$4 + 6 + 3 = 13$$$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $$$13$$$. | 1,700 | false | true | false | false | true | true | false | false | false | false | 2,080 |
2014F | Why, master," quoth Little John, taking the bags and weighing them in his hand, "here is the chink of gold." The folk hero Robin Hood has been troubling Sheriff of Nottingham greatly. Sheriff knows that Robin Hood is about to attack his camps and he wants to be prepared. Sheriff of Nottingham built the camps with strategy in mind and thus there are exactly $$$n$$$ camps numbered from $$$1$$$ to $$$n$$$ and $$$n-1$$$ trails, each connecting two camps. Any camp can be reached from any other camp. Each camp $$$i$$$ has initially $$$a_i$$$ gold. As it is now, all camps would be destroyed by Robin. Sheriff can strengthen a camp by subtracting exactly $$$c$$$ gold from each of its neighboring camps and use it to build better defenses for that camp. Strengthening a camp doesn't change its gold, only its neighbors' gold. A camp can have negative gold. After Robin Hood's attack, all camps that have been strengthened survive the attack, all others are destroyed. What's the maximum gold Sheriff can keep in his surviving camps after Robin Hood's attack if he strengthens his camps optimally? Camp $$$a$$$ is neighboring camp $$$b$$$ if and only if there exists a trail connecting $$$a$$$ and $$$b$$$. Only strengthened camps count towards the answer, as others are destroyed. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Each test case begins with two integers $$$n$$$, $$$c$$$ ($$$1 le n le 2cdot10^5, 1 le c le 10^9$$$)xa0— the number of camps and the gold taken from each neighboring camp for strengthening. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,dots,a_n$$$ ($$$-10^9 le a_i le 10^9$$$)xa0— the initial gold of each camp. Then follow $$$n-1$$$ lines, each with integers $$$u$$$, $$$v$$$ ($$$1 le u, v le n$$$, $$$u e v$$$)xa0— meaning that there is a trail between $$$u$$$ and $$$v$$$. The sum of $$$n$$$ over all test cases doesn't exceed $$$2cdot10^5$$$. It is guaranteed that any camp is reachable from any other camp. Output Output a single integer, the maximum gold Sheriff of Nottingham can keep in his surviving camps after Robin Hood's attack. Example Input 5 3 1 2 3 1 1 2 2 3 3 1 3 6 3 1 2 2 3 3 1 -2 -3 -1 1 2 2 3 6 1 5 -4 3 6 7 3 4 1 5 1 3 5 3 6 1 2 8 1 3 5 2 7 8 5 -3 -4 7 3 1 8 4 3 3 5 7 6 8 7 2 1 Note In the first test case, it is optimal to strengthen the second base. The final gold at each base is $$$[1,3,0]$$$. In the second test case, it is optimal to strengthen all bases. The final gold at each base is $$$[2,4,2]$$$. In the third test case, it is optimal to not strengthen any base. | 2,000 | false | true | false | true | false | false | false | false | false | false | 166 |
1060H | You are given integers $$$d$$$ and $$$p$$$, $$$p$$$ is prime. Also you have a mysterious device. It has memory cells, each contains an integer between $$$0$$$ and $$$p-1$$$. Also two instructions are supported, addition and raising to the $$$d$$$-th power. $$$ extbf{Both are modulo}$$$ $$$p$$$. The memory cells are numbered $$$1, 2, dots, 5000$$$. Initially cells $$$1$$$ and $$$2$$$ contain integers $$$x$$$ and $$$y$$$, respectively ($$$0 leqslant x, y leq p - 1$$$). All other cells contain $$$ extbf{1}$$$s. You can not directly access values in cells, and you $$$ extbf{don't know}$$$ values of $$$x$$$ and $$$y$$$ (but you know they are written in first two cells). You mission, should you choose to accept it, is to write a program using the available instructions to obtain the product $$$xy$$$ modulo $$$p$$$ in one of the cells. You program should work for all possible $$$x$$$ and $$$y$$$. Addition instruction evaluates sum of values in two cells and writes it to third cell. This instruction is encoded by a string "+ e1 e2 to", which writes sum of values in cells e1 and e2 into cell to. Any values of e1, e2, to can coincide. Second instruction writes the $$$d$$$-th power of a value in some cell to the target cell. This instruction is encoded by a string "^ e to". Values e and to can coincide, in this case value in the cell will be overwritten. Last instruction is special, this is the return instruction, and it is encoded by a string "f target". This means you obtained values $$$xy bmod p$$$ in the cell target. No instructions should be called after this instruction. Provide a program that obtains $$$xy bmod p$$$ and uses no more than $$$5000$$$ instructions (including the return instruction). It is guaranteed that, under given constrains, a solution exists. Note This problem has no sample tests. A sample output is shown below. Note that this output is not supposed to be a solution to any testcase, and is there purely to illustrate the output format. $$$ exttt{+ 1 1 3} exttt{^ 3 3} exttt{+ 3 2 2} exttt{+ 3 2 3} exttt{^ 3 1} exttt{f 1}$$$ Here's a step-by-step runtime illustration: $$$begin{array}{cccc} hline exttt{} & ext{cell 1} & ext{cell 2} & ext{cell 3} hline ext{initially} & x & y & 1 hline exttt{+ 1 1 3} & x & y & 2x hline exttt{^ 3 3} & x & y & (2x)^d hline exttt{+ 3 2 2} & x & y + (2x)^d & (2x)^d hline exttt{+ 3 2 3} & x & y + (2x)^d & y + 2cdot(2x)^d hline exttt{^ 3 1} & (y + 2cdot(2x)^d)^d & y + (2x)^d & y + 2cdot(2x)^d hline end{array}$$$ Suppose that $$$d = 2$$$ and $$$p = 3$$$. Since for $$$x = 0$$$ and $$$y = 1$$$ the returned result is $$$1 eq 0 cdot 1 bmod 3$$$, this program would be judged as incorrect. | 3,300 | false | false | false | false | false | true | false | false | false | false | 5,409 |
611A | Today is Wednesday, the third day of the week. What's more interesting is that tomorrow is the last day of the year 2015. Limak is a little polar bear. He enjoyed this year a lot. Now, he is so eager to the coming year 2016. Limak wants to prove how responsible a bear he is. He is going to regularly save candies for the entire year 2016! He considers various saving plans. He can save one candy either on some fixed day of the week or on some fixed day of the month. Limak chose one particular plan. He isn't sure how many candies he will save in the 2016 with his plan. Please, calculate it and tell him. Input The only line of the input is in one of the following two formats: "_x_ of week" where _x_ (1u2009≤u2009_x_u2009≤u20097) denotes the day of the week. The 1-st day is Monday and the 7-th one is Sunday. "_x_ of month" where _x_ (1u2009≤u2009_x_u2009≤u200931) denotes the day of the month. Output Print one integerxa0— the number of candies Limak will save in the year 2016. Note Polar bears use the Gregorian calendar. It is the most common calendar and you likely use it too. You can read about it on Wikipedia if you want to – [ The week starts with Monday. In the first sample Limak wants to save one candy on each Thursday (the 4-th day of the week). There are 52 Thursdays in the 2016. Thus, he will save 52 candies in total. In the second sample Limak wants to save one candy on the 30-th day of each month. There is the 30-th day in exactly 11 months in the 2016xa0— all months but February. It means that Limak will save 11 candies in total. | 900 | false | false | true | false | false | false | false | false | false | false | 7,401 |
18C | Problem - 18C - 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 implementation *1200 No tag edit access → Contest materials ") Tutorial") . In each square he wrote an integer number, possibly negative. He became interested in how many ways exist to cut this stripe into two pieces so that the sum of numbers from one piece is equal to the sum of numbers from the other piece, and each piece contains positive integer amount of squares. Would you help Bob solve this problem? Input The first input line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — amount of squares in the stripe. The second line contains _n_ space-separated numbers — they are the numbers written in the squares of the stripe. These numbers are integer and do not exceed 10000 in absolute value. Output Output the amount of ways to cut the stripe into two non-empty pieces so that the sum of numbers from one piece is equal to the sum of numbers from the other piece. Don't forget that it's allowed to cut the stripe along the squares' borders only. Examples Input 9 1 5 -6 7 9 -16 0 -2 2 Output 3 Input 3 1 1 1 Output 0 Input 2 0 0 Output 1 | 1,200 | false | false | true | false | true | false | false | false | false | false | 9,903 |
730K | Berland consists of _n_ cities and _m_ bidirectional roads connecting pairs of cities. There is no road connecting a city to itself, and between any pair of cities there is no more than one road. It is possible to reach any city from any other moving along roads. Currently Mr. President is in the city _s_ and his destination is the city _t_. He plans to move along roads from _s_ to _t_ (_s_u2009≠u2009_t_). That's why Ministry of Fools and Roads has difficult days. The minister is afraid that Mr. President could get into a traffic jam or get lost. Who knows what else can happen! To be sure that everything goes as planned, the minister decided to temporarily make all roads one-way. So each road will be oriented in one of two possible directions. The following conditions must be satisfied: There should be no cycles along roads after orientation. The city _s_ should be the only such city that all its roads are oriented out (i.e. there are no ingoing roads to the city _s_ and the city _s_ is the only such city). The city _t_ should be the only such city that all its roads are oriented in (i.e. there are no outgoing roads from the city _t_ and the city _t_ is the only such city). Help the minister solve his problem. Write a program to find any such orientation of all roads or report that no solution exists. Input Each test in this problem contains one or more test cases to solve. The first line of the input contains positive number _T_ — the number of cases to solve. Each case starts with a line containing four integers _n_, _m_, _s_ and _t_ (2u2009≤u2009_n_u2009≤u20094·105, 1u2009≤u2009_m_u2009≤u2009106, 1u2009≤u2009_s_,u2009_t_u2009≤u2009_n_, _s_u2009≠u2009_t_) — the number of cities, the number of roads and indices of departure and destination cities. The cities are numbered from 1 to _n_. The following _m_ lines contain roads, one road per line. Each road is given as two integer numbers _x__j_, _y__j_ (1u2009≤u2009_x__j_,u2009_y__j_u2009≤u2009_n_, _x__j_u2009≠u2009_y__j_), which means that the _j_-th road connects cities _x__j_ and _y__j_. There is at most one road between any pair of cities. It is possible to reach any city from any other moving along roads. The sum of values _n_ over all cases in a test doesn't exceed 4·105. The sum of values _m_ over all cases in a test doesn't exceed 106. Output For each case print "Yes" if the answer exists. In the following _m_ lines print roads in the required directions. You can print roads in arbitrary order. If there are multiple answers, print any of them. Print the only line "No" if there is no answer for a case. Example Input 2 4 4 1 2 1 2 2 3 3 4 4 1 3 2 1 3 3 1 2 3 | 3,200 | false | false | false | false | false | false | false | false | false | true | 6,878 |
1120F | Little W and Little P decided to send letters to each other regarding the most important events during a day. There are $$$n$$$ events during a day: at time moment $$$t_i$$$ something happens to the person $$$p_i$$$ ($$$p_i$$$ is either W or P, denoting Little W and Little P, respectively), so he needs to immediately send a letter to the other person. They can send a letter using one of the two ways: Ask Friendly O to deliver the letter directly. Friendly O takes $$$d$$$ acorns for each letter. Leave the letter at Wise R's den. Wise R values free space, so he takes $$$c cdot T$$$ acorns for storing a letter for a time segment of length $$$T$$$. The recipient can take a letter from Wise R either when he leaves his own letter at Wise R's den, or at time moment $$$t_{n + 1}$$$, when everybody comes to Wise R for a tea. It is not possible to take a letter from Wise R's den at other time moments. The friends can store as many letters at Wise R's den as they want, paying for each one separately. Help the friends determine the minimum possible total cost of sending all letters. Input The first line contains three integers $$$n, c, d$$$ ($$$1 leq n leq 10^5$$$, $$$1 leq c leq 10^2$$$, $$$1 leq d leq 10^8$$$)xa0— the number of letters, the cost of storing a letter for one time unit at Wise R's den and the cost of delivering a letter via Friendly O. The next $$$n$$$ describe the events. The $$$i$$$-th of them contains an integer $$$t_i$$$ and a character $$$p_i$$$ ($$$0 leq t_i leq 10^6$$$, $$$p_i$$$ is either W or P)xa0— the time the $$$i$$$-th event happens and the person the event happens to. The last line contains a single integer $$$t_{n + 1}$$$ ($$$0 leq t_{n+1} leq 10^6$$$)xa0— the time when everybody comes to Wise R for a tea and takes all remaining letters. It is guaranteed that $$$t_i < t_{i + 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. Output Print a single integerxa0— the minimum possible cost of delivery of all letters. Examples Input 5 1 4 0 P 1 W 3 P 5 P 8 P 10 Input 10 10 94 17 W 20 W 28 W 48 W 51 P 52 W 56 W 62 P 75 P 78 P 87 Note One of optimal solutions in the first example: At time moment 0 Little P leaves the letter at Wise R's den. At time moment 1 Little W leaves his letter at Wise R's den and takes Little P's letter. This letter is at the den from time moment 0 to time moment 1, it costs $$$1$$$ acorn. At time moment 3 Little P sends his letter via Friendly O, it costs $$$4$$$ acorns. At time moment 5 Little P leaves his letter at the den, receiving Little W's letter which storage costs 4 acorns. At time moment 8 Little P leaves one more letter at the den. At time moment 10 Little W comes to the den for a tea and receives the two letters, paying 5 and 2 acorns. The total cost of delivery is thus $$$1 + 4 + 4 + 5 + 2 = 16$$$ acorns. | 3,100 | false | true | false | true | true | false | false | false | false | false | 5,081 |
1894A | Let's consider a game in which two players, A and B, participate. This game is characterized by two positive integers, $$$X$$$ and $$$Y$$$. The game consists of sets, and each set consists of plays. In each play, exactly one of the players, either A or B, wins. A set ends exactly when one of the players reaches $$$X$$$ wins in the plays of that set. This player is declared the winner of the set. The players play sets until one of them reaches $$$Y$$$ wins in the sets. After that, the game ends, and this player is declared the winner of the entire game. You have just watched a game but didn't notice who was declared the winner. You remember that during the game, $$$n$$$ plays were played, and you know which player won each play. However, you do not know the values of $$$X$$$ and $$$Y$$$. Based on the available information, determine who won the entire gamexa0— A or B. If there is not enough information to determine the winner, you should also report it. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 leq t leq 10^4)$$$ - 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 20)$$$ - the number of plays played during the game. The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of characters $$$ exttt{A}$$$ and $$$ exttt{B}$$$. If $$$s_i = exttt{A}$$$, it means that player A won the $$$i$$$-th play. If $$$s_i = exttt{B}$$$, it means that player B won the $$$i$$$-th play. It is guaranteed that the given sequence of plays corresponds to at least one valid game scenario, for some values of $$$X$$$ and $$$Y$$$. Output For each test case, output: $$$ exttt{A}$$$xa0— if player A is guaranteed to be the winner of the game. $$$ exttt{B}$$$xa0— if player B is guaranteed to be the winner of the game. $$$ exttt{?}$$$xa0— if it is impossible to determine the winner of the game. Example Input 7 5 ABBAA 3 BBB 7 BBAAABA 20 AAAAAAAABBBAABBBBBAB 1 A 13 AAAABABBABBAB 7 BBBAAAA Note In the first test case, the game could have been played with parameters $$$X = 3$$$, $$$Y = 1$$$. The game consisted of $$$1$$$ set, in which player A won, as they won the first $$$3$$$ plays. In this scenario, player A is the winner. The game could also have been played with parameters $$$X = 1$$$, $$$Y = 3$$$. It can be shown that there are no such $$$X$$$ and $$$Y$$$ values for which player B would be the winner. In the second test case, player B won all the plays. It can be easily shown that in this case, player B is guaranteed to be the winner of the game. In the fourth test case, the game could have been played with parameters $$$X = 3$$$, $$$Y = 3$$$: In the first set, $$$3$$$ plays were played: AAA. Player A is declared the winner of the set. In the second set, $$$3$$$ plays were played: AAA. Player A is declared the winner of the set. In the third set, $$$5$$$ plays were played: AABBB. Player B is declared the winner of the set. In the fourth set, $$$5$$$ plays were played: AABBB. Player B is declared the winner of the set. In the fifth set, $$$4$$$ plays were played: BBAB. Player B is declared the winner of the set. In total, player B was the first player to win $$$3$$$ sets. They are declared the winner of the game. | 800 | false | false | true | false | false | false | false | false | false | false | 946 |
1792E | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Educational Codeforces Round 142 (Rated for 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 brute force dfs and similar dp number theory *2400 No tag edit access → Contest materials Announcement Tutorial PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST E. Divisors and Table time limit per test2.5 seconds memory limit per test256 megabytes You are given an $$$n imes n$$$ multiplication table and a positive integer $$$m = m_1 cdot m_2$$$. A $$$n imes n$$$ multiplication table is a table with $$$n$$$ rows and $$$n$$$ columns numbered from $$$1$$$ to $$$n$$$, where $$$a_{i, j} = i cdot j$$$. For each divisor $$$d$$$ of $$$m$$$, check: does $$$d$$$ occur in the table at least once, and if it does, what is the minimum row that contains $$$d$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10$$$)xa0— the number of test cases. The first and only line of each test case contains three integers $$$n$$$, $$$m_1$$$ and $$$m_2$$$ ($$$1 le n le 10^9$$$; $$$1 le m_1, m_2 le 10^9$$$)xa0— the size of the multiplication table and the integer $$$m$$$ represented as $$$m_1 cdot m_2$$$. Output For each test case, let $$$d_1, d_2, dots, d_k$$$ be all divisors of $$$m$$$ sorted in the increasing order. And let $$$a_1, a_2, dots, a_k$$$ be an array of answers, where $$$a_i$$$ is equal to the minimum row index where divisor $$$d_i$$$ occurs, or $$$0$$$, if there is no such row. Since array $$$a$$$ may be large, first, print an integer $$$s$$$xa0— the number of divisors of $$$m$$$ that are present in the $$$n imes n$$$ table. Next, print a single value $$$X = a_1 oplus a_2 oplus dots oplus a_k$$$, where $$$oplus$$$ denotes the bitwise XOR operation. Example input 3 3 72 1 10 10 15 6 1 210 output 6 2 10 0 8 5 Note In the first test case, $$$m = 72 cdot 1 = 72$$$ and has $$$12$$$ divisors $$$[1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72]$$$. The $$$3 imes 3$$$ multiplication table looks like that: 1 2 3 1 1 2 3 2 2 4 6 3 3 6 9 For each divisor of $$$m$$$ that is present in the table, the position with minimum row index is marked. So the array of answers $$$a$$$ is equal to $$$[1, 1, 1, 2, 2, 0, 3, 0, 0, 0, 0, 0]$$$. There are only $$$6$$$ non-zero values, and xor of $$$a$$$ is equal to $$$2$$$. In the second test case, $$$m = 10 cdot 15 = 150$$$ and has $$$12$$$ divisors $$$[1, 2, 3, 5, 6, 10, 15, 25, 30, 50, 75, 150]$$$. All divisors except $$$75$$$ and $$$150$$$ are present in the $$$10 imes 10$$$ table. Array $$$a$$$ $$$=$$$ $$$[1, 1, 1, 1, 1, 1, 3, 5, 3, 5, 0, 0]$$$. There are $$$10$$$ non-zero values, and xor of $$$a$$$ is equal to $$$0$$$. In the third test case, $$$m = 1 cdot 210 = 210$$$ and has $$$16$$$ divisors $$$[1, 2, 3, 5, 6, 7, 10, 14, 15, 21, 30, 35, 42, 70, 105, 210]$$$. The $$$6 imes 6$$$ table with marked divisors is shown below: 1 2 3 4 5 6 1 1 2 3 4 5 6 2 2 4 6 8 10 12 3 3 6 9 12 15 18 4 4 8 12 16 20 24 5 5 10 15 20 25 30 6 6 12 18 24 30 36 Array $$$a$$$ $$$=$$$ $$$[1, 1, 1, 1, 1, 0, 2, 0, 3, 0, 5, 0, 0, 0, 0, 0]$$$. There are $$$8$$$ non-zero values, and xor of $$$a$$$ is equal to $$$5$$$. Codeforces (c) | 2,400 | false | false | false | true | false | false | true | false | false | false | 1,523 |
1142D | Traveling around the world you noticed that many shop owners raise prices to inadequate values if the see you are a foreigner. You define inadequate numbers as follows: all integers from $$$1$$$ to $$$9$$$ are inadequate; for an integer $$$x ge 10$$$ to be inadequate, it is required that the integer $$$lfloor x / 10 floor$$$ is inadequate, but that's not the only condition. Let's sort all the inadequate integers. Let $$$lfloor x / 10 floor$$$ have number $$$k$$$ in this order. Then, the integer $$$x$$$ is inadequate only if the last digit of $$$x$$$ is strictly less than the reminder of division of $$$k$$$ by $$$11$$$. Here $$$lfloor x / 10 floor$$$ denotes $$$x/10$$$ rounded down. Thus, if $$$x$$$ is the $$$m$$$-th in increasing order inadequate number, and $$$m$$$ gives the remainder $$$c$$$ when divided by $$$11$$$, then integers $$$10 cdot x + 0, 10 cdot x + 1 ldots, 10 cdot x + (c - 1)$$$ are inadequate, while integers $$$10 cdot x + c, 10 cdot x + (c + 1), ldots, 10 cdot x + 9$$$ are not inadequate. The first several inadequate integers are $$$1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 30, 31, 32 ldots$$$. After that, since $$$4$$$ is the fourth inadequate integer, $$$40, 41, 42, 43$$$ are inadequate, while $$$44, 45, 46, ldots, 49$$$ are not inadequate; since $$$10$$$ is the $$$10$$$-th inadequate number, integers $$$100, 101, 102, ldots, 109$$$ are all inadequate. And since $$$20$$$ is the $$$11$$$-th inadequate number, none of $$$200, 201, 202, ldots, 209$$$ is inadequate. You wrote down all the prices you have seen in a trip. Unfortunately, all integers got concatenated in one large digit string $$$s$$$ and you lost the bounds between the neighboring integers. You are now interested in the number of substrings of the resulting string that form an inadequate number. If a substring appears more than once at different positions, all its appearances are counted separately. Note In the first example the inadequate numbers in the string are $$$1, 2, 4, 21, 40, 402$$$. In the second example the inadequate numbers in the string are $$$1$$$ and $$$10$$$, and $$$1$$$ appears twice (on the first and on the second positions). | 2,800 | false | false | false | true | false | false | false | false | false | false | 5,010 |
1039D | Problem - 1039D - 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 trees *2800 No tag edit access → Contest materials ") and each path consists of exactly $$$k$$$ vertices. You are given a tree with $$$n$$$ vertices. For each $$$k$$$ from $$$1$$$ to $$$n$$$ inclusive find what is the maximum possible size of a $$$k$$$-valid set of simple paths. Input The first line of the input contains a single integer $$$n$$$ ($$$2 le n le 100,000$$$)xa0— the number of vertices in the tree. Then following $$$n - 1$$$ lines describe the tree, each of them contains two integers $$$v$$$, $$$u$$$ ($$$1 le v, u le n$$$)xa0— endpoints of the corresponding edge. It is guaranteed, that the given graph is a tree. Output Output $$$n$$$ numbers, the $$$i$$$-th of which is the maximum possible number of paths in an $$$i$$$-valid set of paths. Examples Input 7 1 2 2 3 3 4 4 5 5 6 6 7 Output 7 3 2 1 1 1 1 Input 6 1 2 2 3 2 4 1 5 5 6 Output 6 2 2 1 1 0 Note One way to achieve the optimal number of paths for the second sample is illustrated in the following picture: | 2,800 | false | false | false | true | true | false | false | false | false | false | 5,499 |
862D | Mahmoud and Ehab are in the fourth stage now. Dr. Evil has a hidden binary string of length _n_. He guarantees that there is at least one '0' symbol and at least one '1' symbol in it. Now he wants Mahmoud and Ehab to find a position of any '0' symbol and any '1' symbol. In order to do this, Mahmoud and Ehab can ask Dr. Evil up to 15 questions. They tell Dr. Evil some binary string of length _n_, and Dr. Evil tells the Hamming distance between these two strings. Hamming distance between 2 binary strings of the same length is the number of positions in which they have different symbols. You can find the definition of Hamming distance in the notes section below. Help Mahmoud and Ehab find these two positions. You will get Wrong Answer verdict if Your queries doesn't satisfy interaction protocol described below. You ask strictly more than 15 questions and your program terminated after exceeding queries limit. Please note, that you can do up to 15 ask queries and one answer query. Your final answer is not correct. You will get Idleness Limit Exceeded if you don't print anything or if you forget to flush the output, including for the final answer (more info about flushing output below). If you exceed the maximum number of queries, You should terminate with 0, In this case you'll get Wrong Answer, If you don't terminate you may receive any verdict because you'll be reading from a closed stream . Input The first line of input will contain a single integer _n_ (2u2009≤u2009_n_u2009≤u20091000)xa0— the length of the hidden binary string. Output To print the final answer, print "! pos0 pos1" (without quotes), where _pos_0 and _pos_1 are positions of some '0' and some '1' in the string (the string is 1-indexed). Don't forget to flush the output after printing the answer! Interaction To ask a question use the format "? s" (without quotes), where _s_ is a query string. Don't forget to flush the output after printing a query! After each query you can read a single integer from standard inputxa0— the Hamming distance between the hidden string and the query string. To flush the output you can use:- 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 just print one binary string with length up to 1000, containing at least one '0' and at least one '1'. Example Output ? 000 ? 001 ? 010 ? 011 ? 100 ? 101 ! 2 1 Note Hamming distance definition: [ the first test case the hidden binary string is 101, The first query is 000, so the Hamming distance is 2. In the second query the hidden string is still 101 and query is 001, so the Hamming distance is 1. After some queries you find that symbol at position 2 is '0' and symbol at position 1 is '1', so you print "! 2 1". | 2,000 | false | false | false | false | false | false | false | true | false | false | 6,297 |
193A | You've gotten an _n_u2009×u2009_m_ sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as _A_. Set _A_ is connected. Your task is to find the minimum number of squares that we can delete from set _A_ to make it not connected. A set of painted squares is called connected, if for every two squares _a_ and _b_ from this set there is a sequence of squares from the set, beginning in _a_ and ending in _b_, such that in this sequence any square, except for the last one, shares a common side with the square that follows next in the sequence. An empty set and a set consisting of exactly one square are connected by definition. Input The first input line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u200950) — the sizes of the sheet of paper. Each of the next _n_ lines contains _m_ characters — the description of the sheet of paper: the _j_-th character of the _i_-th line equals either "#", if the corresponding square is painted (belongs to set _A_), or equals "." if the corresponding square is not painted (does not belong to set _A_). It is guaranteed that the set of all painted squares _A_ is connected and isn't empty. Output On the first line print the minimum number of squares that need to be deleted to make set _A_ not connected. If it is impossible, print -1. Examples Input 5 4 #### #..# #..# #..# #### Input 5 5 ##### #...# ##### #...# ##### Note In the first sample you can delete any two squares that do not share a side. After that the set of painted squares is not connected anymore. The note to the second sample is shown on the figure below. To the left there is a picture of the initial set of squares. To the right there is a set with deleted squares. The deleted squares are marked with crosses. | 1,700 | false | false | false | false | false | true | false | false | false | true | 9,070 |
758D | Problem - 758D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms dp greedy math strings *2000 No tag edit access → Contest materials ") Editorial") . Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base _n_ he will get the number _k_. Input The first line contains the integer _n_ (2u2009≤u2009_n_u2009≤u2009109). The second line contains the integer _k_ (0u2009≤u2009_k_u2009<u20091060), it is guaranteed that the number _k_ contains no more than 60 symbols. All digits in the second line are strictly less than _n_. Alexander guarantees that the answer exists and does not exceed 1018. The number _k_ doesn't contain leading zeros. Output Print the number _x_ (0u2009≤u2009_x_u2009≤u20091018)xa0— the answer to the problem. Examples Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 Note In the first example 12 could be obtained by converting two numbers to the system with base 13: 12u2009=u200912·130 or 15u2009=u20091·131u2009+u20092·130. | 2,000 | true | true | false | true | false | true | false | false | false | false | 6,754 |
671E | Kekoland is a country with _n_ beautiful cities numbered from left to right and connected by _n_u2009-u20091 roads. The _i_-th road connects cities _i_ and _i_u2009+u20091 and length of this road is _w__i_ kilometers. When you drive in Kekoland, each time you arrive in city _i_ by car you immediately receive _g__i_ liters of gas. There is no other way to get gas in Kekoland. You were hired by the Kekoland president Keko to organize the most beautiful race Kekoland has ever seen. Let race be between cities _l_ and _r_ (_l_u2009≤u2009_r_). Race will consist of two stages. On the first stage cars will go from city _l_ to city _r_. After completing first stage, next day second stage will be held, now racers will go from _r_ to _l_ with their cars. Of course, as it is a race, racers drive directly from start city to finish city. It means that at the first stage they will go only right, and at the second stage they go only left. Beauty of the race between _l_ and _r_ is equal to _r_u2009-u2009_l_u2009+u20091 since racers will see _r_u2009-u2009_l_u2009+u20091 beautiful cities of Kekoland. Cars have infinite tank so racers will take all the gas given to them. At the beginning of each stage racers start the race with empty tank (0 liters of gasoline). They will immediately take their gasoline in start cities (_l_ for the first stage and _r_ for the second stage) right after the race starts. It may not be possible to organize a race between _l_ and _r_ if cars will run out of gas before they reach finish. You have _k_ presents. Each time you give a present to city _i_ its value _g__i_ increases by 1. You may distribute presents among cities in any way (also give many presents to one city, each time increasing _g__i_ by 1). What is the most beautiful race you can organize? Each car consumes 1 liter of gas per one kilometer. Input The first line of the input contains two integers _n_ and _k_ (2u2009≤u2009_n_u2009≤u2009100u2009000, 0u2009≤u2009_k_u2009≤u2009109)xa0— the number of cities in Kekoland and the number of presents you have, respectively. Next line contains _n_u2009-u20091 integers. The _i_-th of them is _w__i_ (1u2009≤u2009_w__i_u2009≤u2009109)xa0— the length of the road between city _i_ and _i_u2009+u20091. Next line contains _n_ integers. The _i_-th of them is _g__i_ (0u2009≤u2009_g__i_u2009≤u2009109)xa0— the amount of gas you receive every time you enter city _i_. Note In first sample if you give one present to each city then it will be possible to make a race between city 1 and city 4. In second sample you should add 1 to _g_5 and 4 to _g_6, then it will be possible to make a race between cities 2 and 8. | 3,300 | false | true | false | false | true | false | false | false | false | false | 7,136 |
731D | Archeologists have found a secret pass in the dungeon of one of the pyramids of Cycleland. To enter the treasury they have to open an unusual lock on the door. The lock consists of _n_ words, each consisting of some hieroglyphs. The wall near the lock has a round switch. Each rotation of this switch changes the hieroglyphs according to some rules. The instruction nearby says that the door will open only if words written on the lock would be sorted in lexicographical order (the definition of lexicographical comparison in given in notes section). The rule that changes hieroglyphs is the following. One clockwise rotation of the round switch replaces each hieroglyph with the next hieroglyph in alphabet, i.e. hieroglyph _x_ (1u2009≤u2009_x_u2009≤u2009_c_u2009-u20091) is replaced with hieroglyph (_x_u2009+u20091), and hieroglyph _c_ is replaced with hieroglyph 1. Help archeologist determine, how many clockwise rotations they should perform in order to open the door, or determine that this is impossible, i.e. no cyclic shift of the alphabet will make the sequence of words sorted lexicographically. Input The first line of the input contains two integers _n_ and _c_ (2u2009≤u2009_n_u2009≤u2009500u2009000, 1u2009≤u2009_c_u2009≤u2009106)xa0— the number of words, written on the lock, and the number of different hieroglyphs. Each of the following _n_ lines contains the description of one word. The _i_-th of these lines starts with integer _l__i_ (1u2009≤u2009_l__i_u2009≤u2009500u2009000), that denotes the length of the _i_-th word, followed by _l__i_ integers _w__i_,u20091, _w__i_,u20092, ..., _w__i_,u2009_l__i_ (1u2009≤u2009_w__i_,u2009_j_u2009≤u2009_c_)xa0— the indices of hieroglyphs that make up the _i_-th word. Hieroglyph with index 1 is the smallest in the alphabet and with index _c_xa0— the biggest. It's guaranteed, that the total length of all words doesn't exceed 106. Output If it is possible to open the door by rotating the round switch, print integer _x_ (0u2009≤u2009_x_u2009≤u2009_c_u2009-u20091) that defines the required number of clockwise rotations. If there are several valid _x_, print any of them. If it is impossible to open the door by this method, print u2009-u20091. Examples Input 4 3 2 3 2 1 1 3 2 3 1 4 2 3 1 2 Note Word _a_1,u2009_a_2,u2009...,u2009_a__m_ of length _m_ is lexicographically not greater than word _b_1,u2009_b_2,u2009...,u2009_b__k_ of length _k_, if one of two conditions hold: at first position _i_, such that _a__i_u2009≠u2009_b__i_, the character _a__i_ goes earlier in the alphabet than character _b__i_, i.e. _a_ has smaller character in the first position where they differ; if there is no such position _i_ and _m_u2009≤u2009_k_, i.e. the first word is a prefix of the second or two words are equal. The sequence of words is said to be sorted in lexicographical order if each word (except the last one) is lexicographically not greater than the next word. In the first sample, after the round switch is rotated 1 position clockwise the words look as follows: 1 3 2 3 1 2 3 1 2 3 In the second sample, words are already sorted in lexicographical order. In the last sample, one can check that no shift of the alphabet will work. | 2,200 | false | true | false | false | true | false | true | false | true | false | 6,873 |
117D | Let _a_ be an array consisting of _n_ numbers. The array's elements are numbered from 1 to _n_, _even_ is an array consisting of the numerals whose numbers are even in _a_ (_even__i_u2009=u2009_a_2_i_, 1u2009≤u20092_i_u2009≤u2009_n_), _odd_ is an array consisting of the numberals whose numbers are odd in _а_ (_odd__i_u2009=u2009_a_2_i_u2009-u20091, 1u2009≤u20092_i_u2009-u20091u2009≤u2009_n_). Then let's define the transformation of array _F_(_a_) in the following manner: if _n_u2009>u20091, _F_(_a_)u2009=u2009_F_(_odd_)u2009+u2009_F_(_even_), where operation "u2009+u2009" stands for the arrays' concatenation (joining together) if _n_u2009=u20091, _F_(_a_)u2009=u2009_a_ Let _a_ be an array consisting of _n_ numbers 1,u20092,u20093,u2009...,u2009_n_. Then _b_ is the result of applying the transformation to the array _a_ (so _b_u2009=u2009_F_(_a_)). You are given _m_ queries (_l_,u2009_r_,u2009_u_,u2009_v_). Your task is to find for each query the sum of numbers _b__i_, such that _l_u2009≤u2009_i_u2009≤u2009_r_ and _u_u2009≤u2009_b__i_u2009≤u2009_v_. You should print the query results modulo _mod_. Input The first line contains three integers _n_, _m_, _mod_ (1u2009≤u2009_n_u2009≤u20091018,u20091u2009≤u2009_m_u2009≤u2009105,u20091u2009≤u2009_mod_u2009≤u2009109). Next _m_ lines describe the queries. Each query is defined by four integers _l_, _r_, _u_, _v_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_, 1u2009≤u2009_u_u2009≤u2009_v_u2009≤u20091018). Please do not use the %lld specificator to read or write 64-bit integers in C++. Use %I64d specificator. Output Print _m_ lines each containing an integer — remainder modulo _mod_ of the query result. Examples Input 4 5 10000 2 3 4 5 2 4 1 3 1 2 2 4 2 3 3 5 1 3 3 4 Input 2 5 10000 1 2 2 2 1 1 4 5 1 1 2 5 1 1 1 3 1 2 5 5 Note Let's consider the first example. First let's construct an array _b_u2009=u2009_F_(_a_)u2009=u2009_F_([1,u20092,u20093,u20094]). Step 1. _F_([1,u20092,u20093,u20094])u2009=u2009_F_([1,u20093])u2009+u2009_F_([2,u20094]) Step 2. _F_([1,u20093])u2009=u2009_F_([1])u2009+u2009_F_([3])u2009=u2009[1]u2009+u2009[3]u2009=u2009[1,u20093] Step 3. _F_([2,u20094])u2009=u2009_F_([2])u2009+u2009_F_([4])u2009=u2009[2]u2009+u2009[4]u2009=u2009[2,u20094] Step 4. _b_u2009=u2009_F_([1,u20092,u20093,u20094])u2009=u2009_F_([1,u20093])u2009+u2009_F_([2,u20094])u2009=u2009[1,u20093]u2009+u2009[2,u20094]u2009=u2009[1,u20093,u20092,u20094] Thus _b_u2009=u2009[1,u20093,u20092,u20094]. Let's consider the first query _l_u2009=u20092,u2009_r_u2009=u20093,u2009_u_u2009=u20094,u2009_v_u2009=u20095. The second and third positions in the array _b_ do not have numbers in the range [4,u20095], so the sum obviously equals zero. Let's consider the second query _l_u2009=u20092,u2009_r_u2009=u20094,u2009_u_u2009=u20091,u2009_v_u2009=u20093. The second and third positions have two numbers that belong to the range [1,u20093], their sum equals 5. | 2,500 | true | false | false | false | false | false | false | false | false | false | 9,418 |
1523C | William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $$$a_1 ,., a_2 ,., a_3 ,., ,cdots, ,.,a_k$$$ and can be one of two types: 1. Add an item $$$a_1 ,., a_2 ,., a_3 ,., cdots ,., a_k ,., 1$$$ (starting a list of a deeper level), or 2. Add an item $$$a_1 ,., a_2 ,., a_3 ,., cdots ,., (a_k + 1)$$$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^3$$$), which is the number of lines in the list. Each of the next $$$n$$$ lines contains a single integer $$$a_i$$$ ($$$1 le a_i le n$$$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $$$n$$$ across all test cases does not exceed $$$10^3$$$. Output For each test case output $$$n$$$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. | 1,600 | false | true | true | false | true | false | true | false | false | false | 3,048 |
1719C | Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $$$n$$$ athletes participate in the tournament, numbered from $$$1$$$ to $$$n$$$. Burenka determined the strength of the $$$i$$$-th athlete as an integer $$$a_i$$$, where $$$1 leq a_i leq n$$$. All the strength values are different, that is, the array $$$a$$$ is a permutation of length $$$n$$$. We know that in a fight, if $$$a_i > a_j$$$, then the $$$i$$$-th participant always wins the $$$j$$$-th. The tournament goes like this: initially, all $$$n$$$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $$$q$$$ questions. In each question, Burenka asks how many victories the $$$i$$$-th participant gets in the first $$$k$$$ rounds of the competition for some given numbers $$$i$$$ and $$$k$$$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$2 leq n leq 10^5$$$, $$$1 leq q leq 10^5$$$) — the number of tournament participants and the number of questions. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq n$$$) — the array $$$a$$$, which is a permutation. The next $$$q$$$ lines of a test case contain questions. Each line contains two integers $$$i$$$ and $$$k$$$ ($$$1 leq i leq n$$$, $$$1 leq k leq 10^9$$$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$10^5$$$. Output For each Burenka's question, print a single line containing one integer — the answer to the question. Example Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Note In the first test case, the first numbered athlete has the strength of $$$3$$$, in the first round he will defeat the athlete with the number $$$2$$$ and the strength of $$$1$$$, and in the second round, the athlete with the number $$$3$$$ and the strength of $$$2$$$. In the second test case, we list the strengths of the athletes fighting in the first $$$5$$$ fights: $$$1$$$ and $$$3$$$, $$$3$$$ and $$$4$$$, $$$4$$$ and $$$2$$$, $$$4$$$ and $$$1$$$, $$$4$$$ and $$$3$$$. The participant with the number $$$4$$$ in the first $$$5$$$ rounds won $$$0$$$ times (his strength is $$$2$$$). The participant with the number $$$3$$$ has a strength of $$$4$$$ and won $$$1$$$ time in the first two fights by fighting $$$1$$$ time. | 1,400 | false | false | true | false | true | false | false | true | false | false | 1,966 |
1560F1 | Problem - 1560F1 - 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 constructive algorithms dfs and similar greedy *1900 No tag edit access → Contest materials ") time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output It is a simplified version of problem F2. The difference between them is the constraints (F1: $$$k le 2$$$, F2: $$$k le 10$$$). You are given an integer $$$n$$$. Find the minimum integer $$$x$$$ such that $$$x ge n$$$ and the number $$$x$$$ is $$$k$$$-beautiful. A number is called $$$k$$$-beautiful if its decimal representation having no leading zeroes contains no more than $$$k$$$ different digits. E.g. if $$$k = 2$$$, the numbers $$$3434443$$$, $$$55550$$$, $$$777$$$ and $$$21$$$ are $$$k$$$-beautiful whereas the numbers $$$120$$$, $$$445435$$$ and $$$998244353$$$ are not. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^9$$$, $$$1 le k le 2$$$). Output For each test case output on a separate line $$$x$$$ — the minimum $$$k$$$-beautiful integer such that $$$x ge n$$$. Example Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 | 1,900 | false | true | false | false | false | true | true | true | false | false | 2,825 |
1416A | You are given an array $$$a$$$ consisting of $$$n$$$ integers numbered from $$$1$$$ to $$$n$$$. Let's define the $$$k$$$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $$$k$$$ (recall that a subsegment of $$$a$$$ of length $$$k$$$ is a contiguous part of $$$a$$$ containing exactly $$$k$$$ elements). If there is no integer occuring in all subsegments of length $$$k$$$ for some value of $$$k$$$, then the $$$k$$$-amazing number is $$$-1$$$. For each $$$k$$$ from $$$1$$$ to $$$n$$$ calculate the $$$k$$$-amazing number of the array $$$a$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$) — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case print $$$n$$$ integers, where the $$$i$$$-th integer is equal to the $$$i$$$-amazing number of the array. Example Input 3 5 1 2 3 4 5 5 4 4 4 4 2 6 1 3 1 5 3 1 Output -1 -1 3 2 1 -1 4 4 4 2 -1 -1 1 1 1 1 | 1,500 | false | false | true | false | true | false | false | true | false | false | 3,602 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.