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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
377D | Pavel is going to make a game of his dream. However, he knows that he can't make it on his own so he founded a development company and hired _n_ workers of staff. Now he wants to pick _n_ workers from the staff who will be directly responsible for developing a game. Each worker has a certain skill level _v__i_. Besides, each worker doesn't want to work with the one whose skill is very different. In other words, the _i_-th worker won't work with those whose skill is less than _l__i_, and with those whose skill is more than _r__i_. Pavel understands that the game of his dream isn't too hard to develop, so the worker with any skill will be equally useful. That's why he wants to pick a team of the maximum possible size. Help him pick such team. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105)xa0— the number of workers Pavel hired. Each of the following _n_ lines contains three space-separated integers _l__i_, _v__i_, _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_v__i_u2009≤u2009_r__i_u2009≤u20093·105)xa0— the minimum skill value of the workers that the _i_-th worker can work with, the _i_-th worker's skill and the maximum skill value of the workers that the _i_-th worker can work with. Output In the first line print a single integer _m_xa0— the number of workers Pavel must pick for developing the game. In the next line print _m_ space-separated integersxa0— the numbers of the workers in any order. If there are multiple optimal solutions, print any of them. Examples Input 4 2 8 9 1 4 7 3 6 8 5 8 10 Input 6 3 5 16 1 6 11 4 8 12 7 9 16 2 10 14 8 13 15 | 2,400 | false | false | false | false | true | false | false | false | false | false | 8,321 |
1621C | This is an interactive problem. The jury has a permutation $$$p$$$ of length $$$n$$$ and wants you to guess it. For this, the jury created another permutation $$$q$$$ of length $$$n$$$. Initially, $$$q$$$ is an identity permutation ($$$q_i = i$$$ for all $$$i$$$). You can ask queries to get $$$q_i$$$ for any $$$i$$$ you want. After each query, the jury will change $$$q$$$ in the following way: At first, the jury will create a new permutation $$$q'$$$ of length $$$n$$$ such that $$$q'_i = q_{p_i}$$$ for all $$$i$$$. Then the jury will replace permutation $$$q$$$ with pemutation $$$q'$$$. You can make no more than $$$2n$$$ queries in order to quess $$$p$$$. Input The first line of input contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. Interaction Interaction in each test case starts after reading the single integer $$$n$$$ ($$$1 leq n leq 10^4$$$)xa0— the length of permutations $$$p$$$ and $$$q$$$. To get the value of $$$q_i$$$, output the query in the format $$$?$$$ $$$i$$$ ($$$1 leq i leq n$$$). After that you will receive the value of $$$q_i$$$. You can make at most $$$2n$$$ queries. After the incorrect query you will receive $$$0$$$ and you should exit immediately to get Wrong answer verdict. When you will be ready to determine $$$p$$$, output $$$p$$$ in format $$$!$$$ $$$p_1$$$ $$$p_2$$$ $$$ldots$$$ $$$p_n$$$. After this you should go to the next test case or exit if it was the last test case. Printing the permutation is not counted as one of $$$2n$$$ queries. 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. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^4$$$. The interactor is not adaptive in this problem. Hacks: To hack, use the following format: The first line contains the single integer $$$t$$$xa0— the number of test cases. The first line of each test case contains the single integer $$$n$$$xa0— the length of the permutations $$$p$$$ and $$$q$$$. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$xa0— the hidden permutation for this test case. Example Output ? 3 ? 2 ? 4 ! 4 2 1 3 ? 2 ? 3 ? 2 ! 1 3 4 2 Note In the first test case the hidden permutation $$$p = [4, 2, 1, 3]$$$. Before the first query $$$q = [1, 2, 3, 4]$$$ so answer for the query will be $$$q_3 = 3$$$. Before the second query $$$q = [4, 2, 1, 3]$$$ so answer for the query will be $$$q_2 = 2$$$. Before the third query $$$q = [3, 2, 4, 1]$$$ so answer for the query will be $$$q_4 = 1$$$. In the second test case the hidden permutation $$$p = [1, 3, 4, 2]$$$. Empty strings are given only for better readability. There will be no empty lines in the testing system. | 1,700 | true | false | false | false | false | false | false | false | false | false | 2,518 |
703C | And while Mishka is enjoying her trip... Chris is a little brown bear. No one knows, where and when he met Mishka, but for a long time they are together (excluding her current trip). However, best friends are important too. John is Chris' best friend. Once walking with his friend, John gave Chris the following problem: At the infinite horizontal road of width _w_, bounded by lines _y_u2009=u20090 and _y_u2009=u2009_w_, there is a bus moving, presented as a convex polygon of _n_ vertices. The bus moves continuously with a constant speed of _v_ in a straight _Ox_ line in direction of decreasing _x_ coordinates, thus in time only _x_ coordinates of its points are changing. Formally, after time _t_ each of _x_ coordinates of its points will be decreased by _vt_. There is a pedestrian in the point (0,u20090), who can move only by a vertical pedestrian crossing, presented as a segment connecting points (0,u20090) and (0,u2009_w_) with any speed not exceeding _u_. Thus the pedestrian can move only in a straight line _Oy_ in any direction with any speed not exceeding _u_ and not leaving the road borders. The pedestrian can instantly change his speed, thus, for example, he can stop instantly. Please look at the sample note picture for better understanding. We consider the pedestrian is hit by the bus, if at any moment the point he is located in lies strictly inside the bus polygon (this means that if the point lies on the polygon vertex or on its edge, the pedestrian is not hit by the bus). You are given the bus position at the moment 0. Please help Chris determine minimum amount of time the pedestrian needs to cross the road and reach the point (0,u2009_w_) and not to be hit by the bus. Input The first line of the input contains four integers _n_, _w_, _v_, _u_ (3u2009≤u2009_n_u2009≤u200910u2009000, 1u2009≤u2009_w_u2009≤u2009109, 1u2009≤u2009_v_,u2009u2009_u_u2009≤u20091000)xa0— the number of the bus polygon vertices, road width, bus speed and pedestrian speed respectively. The next _n_ lines describes polygon vertices in counter-clockwise order. _i_-th of them contains pair of integers _x__i_ and _y__i_ (u2009-u2009109u2009≤u2009_x__i_u2009≤u2009109, 0u2009≤u2009_y__i_u2009≤u2009_w_)xa0— coordinates of _i_-th polygon point. It is guaranteed that the polygon is non-degenerate. | 2,100 | false | false | true | false | false | false | false | false | false | false | 7,009 |
1736C1 | This is the easy version of this problem. In this version, we do not have queries. Note that we have multiple test cases in this version. You can make hacks only if both versions of the problem are solved. An array $$$b$$$ of length $$$m$$$ is good if for all $$$i$$$ the $$$i$$$-th element is greater than or equal to $$$i$$$. In other words, $$$b$$$ is good if and only if $$$b_i geq i$$$ for all $$$i$$$ ($$$1 leq i leq m$$$). You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find the number of pairs of indices $$$(l, r)$$$, where $$$1 le l le r le n$$$, such that the array $$$[a_l, a_{l+1}, ldots, a_r]$$$ is good. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 2 cdot 10^5$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$), the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ space-separated integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 leq a_i leq n$$$), representing the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print the number of suitable pairs of indices. Example Input 3 3 1 2 3 3 1 1 1 4 2 1 4 3 Note In the first test case, all subarrays of $$$a$$$ are good, so all pairs are suitable. In the second test case, the pairs $$$(1, 1)$$$, $$$(2, 2)$$$, and $$$(3, 3)$$$ are suitable. For example, when $$$(l, r) = (1, 2)$$$, the array $$$b=[1,1]$$$ is not good because $$$b_2 < 2$$$. | 1,300 | false | false | false | false | true | false | false | true | false | false | 1,872 |
111A | Problem - 111A - 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 *1400 No tag edit access → Contest materials . Please do not use the %lld specificator to read or write 64-bit integers in С++. It is recommended to use cin, cout streams or the %I64d specificator. Output Print _n_ positive integers that satisfy the conditions, one integer per line. If such numbers do not exist, print a single number "-1". If there are several solutions, print any of them. Examples Input 5 15 15 Output 4 4 1 1 2 Input 2 3 2 Output -1 Input 1 99 11 Output 11 | 1,400 | false | true | false | false | false | false | false | false | false | false | 9,442 |
1080D | Recently, Olya received a magical square with the size of $$$2^n imes 2^n$$$. It seems to her sister that one square is boring. Therefore, she asked Olya to perform exactly $$$k$$$ splitting operations. A Splitting operation is an operation during which Olya takes a square with side $$$a$$$ and cuts it into 4 equal squares with side $$$dfrac{a}{2}$$$. If the side of the square is equal to $$$1$$$, then it is impossible to apply a splitting operation to it (see examples for better understanding). Olya is happy to fulfill her sister's request, but she also wants the condition of Olya's happiness to be satisfied after all operations. The condition of Olya's happiness will be satisfied if the following statement is fulfilled: Let the length of the side of the lower left square be equal to $$$a$$$, then the length of the side of the right upper square should also be equal to $$$a$$$. There should also be a path between them that consists only of squares with the side of length $$$a$$$. All consecutive squares on a path should have a common side. Obviously, as long as we have one square, these conditions are met. So Olya is ready to fulfill her sister's request only under the condition that she is satisfied too. Tell her: is it possible to perform exactly $$$k$$$ splitting operations in a certain order so that the condition of Olya's happiness is satisfied? If it is possible, tell also the size of the side of squares of which the path from the lower left square to the upper right one will consist. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^3$$$)xa0— the number of tests. Each of the following $$$t$$$ lines contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 le n_i le 10^9, 1 le k_i le 10^{18}$$$)xa0— the description of the $$$i$$$-th test, which means that initially Olya's square has size of $$$2^{n_i} imes 2^{n_i}$$$ and Olya's sister asks her to do exactly $$$k_i$$$ splitting operations. Output Print $$$t$$$ lines, where in the $$$i$$$-th line you should output "YES" if it is possible to perform $$$k_i$$$ splitting operations in the $$$i$$$-th test in such a way that the condition of Olya's happiness is satisfied or print "NO" otherwise. If you printed "YES", then also print the $$$log_2$$$ of the length of the side of the squares through space, along which you can build a path from the lower left square to the upper right one. You can output each letter in any case (lower or upper). If there are multiple answers, print any. Note In each of the illustrations, the pictures are shown in order in which Olya applied the operations. The recently-created squares are highlighted with red. In the first test, Olya can apply splitting operations in the following order: Olya applies one operation on the only existing square. The condition of Olya's happiness will be met, since there is a path of squares of the same size from the lower left square to the upper right one: The length of the sides of the squares on the path is $$$1$$$. $$$log_2(1) = 0$$$. In the second test, Olya can apply splitting operations in the following order: Olya applies the first operation on the only existing square. She applies the second one on the right bottom square. The condition of Olya's happiness will be met, since there is a path of squares of the same size from the lower left square to the upper right one: The length of the sides of the squares on the path is $$$2$$$. $$$log_2(2) = 1$$$. In the third test, it takes $$$5$$$ operations for Olya to make the square look like this: Since it requires her to perform $$$7$$$ splitting operations, and it is impossible to perform them on squares with side equal to $$$1$$$, then Olya cannot do anything more and the answer is "NO". | 2,000 | true | false | true | false | false | true | false | false | false | false | 5,326 |
1951H | There is an array $$$a$$$ of size $$$2^k$$$ for some positive integer $$$k$$$, which is initially a permutation of values from $$$1$$$ to $$$2^k$$$. Alice and Bob play the following game on the array $$$a$$$. First, a value $$$t$$$ between $$$1$$$ and $$$k$$$ is shown to both Alice and Bob. Then, for exactly $$$t$$$ turns, the following happens: Alice either does nothing, or chooses two distinct elements of the array $$$a$$$ and swaps them. Bob chooses either the left half or the right half of the array $$$a$$$ and erases it. The score of the game is defined as the maximum value in $$$a$$$ after all $$$t$$$ turns have been played. Alice wants to maximize this score, while Bob wants to minimize it. You need to output $$$k$$$ numbers: the score of the game if both Alice and Bob play optimally for $$$t$$$ from $$$1$$$ to $$$k$$$. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$k$$$ ($$$1 le k le 20$$$)xa0— the parameter of the size of $$$a$$$. The second line of each test case contains $$$2^k$$$ integers $$$a_1, a_2, ldots, a_{2^k}$$$ ($$$1 le a_i le 2^k$$$, $$$a_i$$$'s are pairwise distinct)xa0— the given array $$$a$$$. It is guaranteed that the sum of $$$2^k$$$ over all test cases does not exceed $$$2^{20}$$$. Output For each test case, print $$$k$$$ numbers, where the $$$i$$$-th number is the score of the game if both Alice and Bob play optimally for $$$t = i$$$. Example Input 5 1 1 2 2 4 3 2 1 3 5 1 6 4 7 2 8 3 4 10 15 6 12 1 3 4 9 13 5 7 16 14 11 2 8 5 32 2 5 23 19 17 31 7 29 3 4 16 13 9 30 24 14 1 8 20 6 15 26 18 10 27 22 12 25 21 28 11 Output 1 3 1 7 5 1 15 13 9 1 31 28 25 17 1 Note In the third test case, for $$$t = 2$$$, the game could have proceeded as follows: Initially, $$$a = [5, 1, 6, 4, 7, 2, 8, 3]$$$. Alice swaps $$$a_6$$$ and $$$a_8$$$, $$$a$$$ becomes $$$[5, 1, 6, 4, 7, 3, 8, 2]$$$. Bob erases the right half of the array, $$$a$$$ becomes $$$[5, 1, 6, 4]$$$. Alice does nothing, $$$a$$$ remains as $$$[5, 1, 6, 4]$$$. Bob erases the right half of the array, $$$a$$$ becomes $$$[5, 1]$$$. The game ends with a score of $$$5$$$. | 3,200 | false | true | false | true | false | false | false | true | false | false | 567 |
513G3 | You are given a permutation of _n_ numbers _p_1,u2009_p_2,u2009...,u2009_p__n_. We perform _k_ operations of the following type: choose uniformly at random two indices _l_ and _r_ (_l_u2009≤u2009_r_) and reverse the order of the elements _p__l_,u2009_p__l_u2009+u20091,u2009...,u2009_p__r_. Your task is to find the expected value of the number of inversions in the resulting permutation. Input The first line of input contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_k_u2009≤u2009109). The next line contains _n_ integers _p_1,u2009_p_2,u2009...,u2009_p__n_ — the given permutation. All _p__i_ are different and in range from 1 to _n_. The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem G1 (3 points), the constraints 1u2009≤u2009_n_u2009≤u20096, 1u2009≤u2009_k_u2009≤u20094 will hold. In subproblem G2 (5 points), the constraints 1u2009≤u2009_n_u2009≤u200930, 1u2009≤u2009_k_u2009≤u2009200 will hold. In subproblem G3 (16 points), the constraints 1u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_k_u2009≤u2009109 will hold. Output Output the answer with absolute or relative error no more than 1_e_u2009-u20099. Note Consider the first sample test. We will randomly pick an interval of the permutation (1,u20092,u20093) (which has no inversions) and reverse the order of its elements. With probability , the interval will consist of a single element and the permutation will not be altered. With probability we will inverse the first two elements' order and obtain the permutation (2,u20091,u20093) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1,u20093,u20092) with one inversion. Finally, with probability the randomly picked interval will contain all elements, leading to the permutation (3,u20092,u20091) with 3 inversions. Hence, the expected number of inversions is equal to . | 3,100 | false | false | false | true | false | false | false | false | false | false | 7,781 |
407E | Problem - 407E - 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 *3100 No tag edit access → Contest materials . The second line contains _n_ space-separated integers: _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009109u2009≤u2009_a__i_u2009≤u2009109)xa0— the actual sequence. Output Print two space-separated integers _l_,u2009_r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_) show that sequence _a__l_,u2009_a__l_u2009+u20091,u2009...,u2009_a__r_ is the longest subsegment that is a good _k_-_d_ sequence. If there are multiple optimal answers, print the one with the minimum value of _l_. Examples Input 6 1 2 4 3 2 8 6 2 Output 3 5 Note In the first test sample the answer is the subsegment consisting of numbers 2, 8, 6xa0— after adding number 4 and sorting it becomes sequence 2, 4, 6, 8xa0— the arithmetic progression with difference 2. | 3,100 | false | false | false | false | true | false | false | false | false | false | 8,194 |
960B | You are given two arrays _A_ and _B_, each of size _n_. The error, _E_, between these two arrays is defined . You have to perform exactly _k_1 operations on array _A_ and exactly _k_2 operations on array _B_. In one operation, you have to choose one element of the array and increase or decrease it by 1. Output the minimum possible value of error after _k_1 operations on array _A_ and _k_2 operations on array _B_ have been performed. Input The first line contains three space-separated integers _n_ (1u2009≤u2009_n_u2009≤u2009103), _k_1 and _k_2 (0u2009≤u2009_k_1u2009+u2009_k_2u2009≤u2009103, _k_1 and _k_2 are non-negative) — size of arrays and number of operations to perform on _A_ and _B_ respectively. Second line contains _n_ space separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009106u2009≤u2009_a__i_u2009≤u2009106) — array _A_. Third line contains _n_ space separated integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (u2009-u2009106u2009≤u2009_b__i_u2009≤u2009106)— array _B_. Output Output a single integer — the minimum possible value of after doing exactly _k_1 operations on array _A_ and exactly _k_2 operations on array _B_. Note In the first sample case, we cannot perform any operations on _A_ or _B_. Therefore the minimum possible error _E_u2009=u2009(1u2009-u20092)2u2009+u2009(2u2009-u20093)2u2009=u20092. In the second sample case, we are required to perform exactly one operation on _A_. In order to minimize error, we increment the first element of _A_ by 1. Now, _A_u2009=u2009[2,u20092]. The error is now _E_u2009=u2009(2u2009-u20092)2u2009+u2009(2u2009-u20092)2u2009=u20090. This is the minimum possible error obtainable. In the third sample case, we can increase the first element of _A_ to 8, using the all of the 5 moves available to us. Also, the first element of _B_ can be reduced to 8 using the 6 of the 7 available moves. Now _A_u2009=u2009[8,u20094] and _B_u2009=u2009[8,u20094]. The error is now _E_u2009=u2009(8u2009-u20098)2u2009+u2009(4u2009-u20094)2u2009=u20090, but we are still left with 1 move for array _B_. Increasing the second element of _B_ to 5 using the left move, we get _B_u2009=u2009[8,u20095] and _E_u2009=u2009(8u2009-u20098)2u2009+u2009(4u2009-u20095)2u2009=u20091. | 1,500 | false | true | false | false | true | false | false | false | true | false | 5,870 |
242A | Petya and Vasya are tossing a coin. Their friend Valera is appointed as a judge. The game is very simple. First Vasya tosses a coin _x_ times, then Petya tosses a coin _y_ times. If the tossing player gets head, he scores one point. If he gets tail, nobody gets any points. The winner is the player with most points by the end of the game. If boys have the same number of points, the game finishes with a draw. At some point, Valera lost his count, and so he can not say exactly what the score is at the end of the game. But there are things he remembers for sure. He remembers that the entire game Vasya got heads at least _a_ times, and Petya got heads at least _b_ times. Moreover, he knows that the winner of the game was Vasya. Valera wants to use this information to know every possible outcome of the game, which do not contradict his memories. Input The single line contains four integers _x_,u2009_y_,u2009_a_,u2009_b_ (1u2009≤u2009_a_u2009≤u2009_x_u2009≤u2009100,u20091u2009≤u2009_b_u2009≤u2009_y_u2009≤u2009100). The numbers on the line are separated by a space. Output In the first line print integer _n_ — the number of possible outcomes of the game. Then on _n_ lines print the outcomes. On the _i_-th line print a space-separated pair of integers _c__i_, _d__i_ — the number of heads Vasya and Petya got in the _i_-th outcome of the game, correspondingly. Print pairs of integers (_c__i_,u2009_d__i_) in the strictly increasing order. Let us remind you that the pair of numbers (_p_1,u2009_q_1) is less than the pair of numbers (_p_2,u2009_q_2), if _p_1u2009<u2009_p_2, or _p_1u2009=u2009_p_2 and also _q_1u2009<u2009_q_2. | 1,100 | false | false | true | false | false | false | true | false | false | false | 8,868 |
228B | You've got two rectangular tables with sizes _n__a_u2009×u2009_m__a_ and _n__b_u2009×u2009_m__b_ cells. The tables consist of zeroes and ones. We will consider the rows and columns of both tables indexed starting from 1. Then we will define the element of the first table, located at the intersection of the _i_-th row and the _j_-th column, as _a__i_,u2009_j_; we will define the element of the second table, located at the intersection of the _i_-th row and the _j_-th column, as _b__i_,u2009_j_. We will call the pair of integers (_x_,u2009_y_) a shift of the second table relative to the first one. We'll call the overlap factor of the shift (_x_,u2009_y_) value: where the variables _i_,u2009_j_ take only such values, in which the expression _a__i_,u2009_j_·_b__i_u2009+u2009_x_,u2009_j_u2009+u2009_y_ makes sense. More formally, inequalities 1u2009≤u2009_i_u2009≤u2009_n__a_,u20091u2009≤u2009_j_u2009≤u2009_m__a_,u20091u2009≤u2009_i_u2009+u2009_x_u2009≤u2009_n__b_,u20091u2009≤u2009_j_u2009+u2009_y_u2009≤u2009_m__b_ must hold. If there are no values of variables _i_,u2009_j_, that satisfy the given inequalities, the value of the sum is considered equal to 0. Your task is to find the shift with the maximum overlap factor among all possible shifts. Input The first line contains two space-separated integers _n__a_,u2009_m__a_ (1u2009≤u2009_n__a_,u2009_m__a_u2009≤u200950) — the number of rows and columns in the first table. Then _n__a_ lines contain _m__a_ characters each — the elements of the first table. Each character is either a "0", or a "1". The next line contains two space-separated integers _n__b_,u2009_m__b_ (1u2009≤u2009_n__b_,u2009_m__b_u2009≤u200950) — the number of rows and columns in the second table. Then follow the elements of the second table in the format, similar to the first table. It is guaranteed that the first table has at least one number "1". It is guaranteed that the second table has at least one number "1". Output Print two space-separated integers _x_,u2009_y_ (_x_,u2009_y_u2009≤u2009109) — a shift with maximum overlap factor. If there are multiple solutions, print any of them. | 1,400 | false | false | true | false | false | false | true | false | false | false | 8,927 |
1917B | You are given a string $$$s$$$ of length $$$n$$$. Let's define two operations you can apply on the string: remove the first character of the string; remove the second character of the string. Your task is to find the number of distinct non-empty strings that can be generated by applying the given operations on the initial string any number of times (possibly zero), in any order. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains $$$n$$$ ($$$1 leq n leq 10^5$$$) — the length of the string. The second line of each test case contains the string $$$s$$$. It is guaranteed that the string only contains lowercase letters of the English alphabet. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integer: the number of distinct non-empty strings you can get. Example Input 5 5 aaaaa 1 z 5 ababa 14 bcdaaaabcdaaaa 20 abcdefghijklmnopqrst Note In the first test case, we can get the following strings: $$$a$$$, $$$aa$$$, $$$aaa$$$, $$$aaaa$$$, $$$aaaaa$$$. In the third test case, for example, the word $$$ba$$$ can be reached in the following way: remove the first character of the current string $$$ababa$$$, getting $$$baba$$$; remove the second character of the current string $$$baba$$$, getting $$$bba$$$; remove the second character of the current string $$$bba$$$, getting $$$ba$$$. | 1,100 | false | false | false | true | true | false | true | false | false | false | 793 |
1409C | We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array: The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers. The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x < y$$$. If you sort the array in increasing order (such that $$$a_1 < a_2 < ldots < a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below. Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$max(a_1, a_2, dots, a_n)$$$. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 le n le 50$$$; $$$1 le x < y le 50$$$) — the length of the array and two elements that are present in the array, respectively. Output For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter). It can be proven that such an array always exists under the given constraints. Example Input 5 2 1 49 5 20 50 6 20 50 5 3 8 9 13 22 Output 1 49 20 40 30 50 10 26 32 20 38 44 50 8 23 18 13 3 1 10 13 4 19 22 25 16 7 | 1,200 | true | false | false | false | false | false | true | false | false | false | 3,625 |
1626A | You are given a string $$$s$$$, consisting of lowercase Latin letters. Every letter appears in it no more than twice. Your task is to rearrange the letters in the string in such a way that for each pair of letters that appear exactly twice, the distance between the letters in the pair is the same. You are not allowed to add or remove letters. It can be shown that the answer always exists. If there are multiple answers, print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^3$$$)xa0— the number of testcases. Each testcase consists of a non-empty string $$$s$$$, consisting of lowercase Latin letters. Every letter appears in the string no more than twice. The length of the string doesn't exceed $$$52$$$. Output For each testcase, print a single string. Every letter should appear in it the same number of times as it appears in string $$$s$$$. For each pair of letters that appear exactly twice, the distance between the letters in the pair should be the same. If there are multiple answers, print any of them. Note In the first testcase of the example, the only letter that appears exactly twice is letter 'l'. You can rearrange the letters arbitrarily, since there are no distances to compare. In the second testcase of the example, the letters that appear exactly twice are 'a', 'b' and 'c'. Initially, letters 'a' are distance $$$6$$$ apart, letters 'b' are distance $$$4$$$ apart and letters 'c' are distance $$$2$$$ apart. They are not the same, so we have to rearrange the letters. After rearrangement, letters 'a' are distance $$$2$$$ apart, letters 'b' are distance $$$2$$$ apart and letters 'c' are distance $$$2$$$ apart. They are all the same, so the answer is valid. In the third testcase of the example, there are no letters that appear exactly twice. Thus, any rearrangement is valid. Including not changing the string at all. | 800 | false | false | false | false | false | true | false | false | true | false | 2,487 |
331E1 | Everybody knows that we have been living in the Matrix for a long time. And in the new seventh Matrix the world is ruled by beavers. So let's take beaver Neo. Neo has so-called "deja vu" outbursts when he gets visions of events in some places he's been at or is going to be at. Let's examine the phenomenon in more detail. We can say that Neo's city is represented by a directed graph, consisting of _n_ shops and _m_ streets that connect the shops. No two streets connect the same pair of shops (besides, there can't be one street from A to B and one street from B to A). No street connects a shop with itself. As Neo passes some streets, he gets visions. No matter how many times he passes street _k_, every time he will get the same visions in the same order. A vision is a sequence of shops. We know that Neo is going to get really shocked if he passes the way from some shop _a_ to some shop _b_, possible coinciding with _a_, such that the list of visited shops in the real life and in the visions coincide. Suggest beaver Neo such path of non-zero length. Or maybe you can even count the number of such paths modulo 1000000007 (109u2009+u20097)?.. Input The first line contains integers _n_ and _m_ — the number of shops and the number of streets, correspondingly, 1u2009≤u2009_n_u2009≤u200950, . Next _m_ lines contain the descriptions of the streets in the following format: _x__i_ _y__i_ _k__i_ _v_1 _v_2 ... _v__k_, where _x__i_ and _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_,u2009_x__i_u2009≠u2009_y__i_) are indices of shops connected by a street, _k__i_ (0u2009≤u2009_k__i_u2009≤u2009_n_) is the number of visions on the way from _x__i_ to _y__i_; _v_1, _v_2, ..., _v__k_ (1u2009≤u2009_v__i_u2009≤u2009_n_) describe the visions: the numbers of the shops Neo saw. Note that the order of the visions matters. It is guaranteed that the total number of visions on all streets doesn't exceed 105. to get 50 points, you need to find any (not necessarily simple) path of length at most 2·_n_, that meets the attributes described above (subproblem E1); to get 50 more points, you need to count for each length from 1 to 2·_n_ the number of paths that have the attribute described above (subproblem E2). Output Subproblem E1. In the first line print an integer _k_ (1u2009≤u2009_k_u2009≤u20092·_n_) — the numbers of shops on Neo's path. In the next line print _k_ integers — the number of shops in the order Neo passes them. If the graph doesn't have such paths or the length of the shortest path includes more than 2·_n_ shops, print on a single line 0. Subproblem E2. Print 2·_n_ lines. The _i_-th line must contain a single integer — the number of required paths of length _i_ modulo 1000000007 (109u2009+u20097). Examples Input 6 6 1 2 2 1 2 2 3 1 3 3 4 2 4 5 4 5 0 5 3 1 3 6 1 1 6 Input 6 6 1 2 2 1 2 2 3 1 3 3 4 2 4 5 4 5 0 5 3 1 3 6 1 1 6 Note The input in both samples are the same. The first sample contains the answer to the first subproblem, the second sample contains the answer to the second subproblem. | 2,900 | false | false | true | false | false | true | false | false | false | true | 8,512 |
571D | Oscolcovo city has a campus consisting of _n_ student dormitories, _n_ universities and _n_ military offices. Initially, the _i_-th dormitory belongs to the _i_-th university and is assigned to the _i_-th military office. Life goes on and the campus is continuously going through some changes. The changes can be of four types: 1. University _a__j_ merges with university _b__j_. After that all the dormitories that belonged to university _b__j_ are assigned to to university _a__j_, and university _b__j_ disappears. 2. Military office _c__j_ merges with military office _d__j_. After that all the dormitories that were assigned to military office _d__j_, are assigned to military office _c__j_, and military office _d__j_ disappears. 3. Students of university _x__j_ move in dormitories. Lets _k__x__j_ is the number of dormitories that belong to this university at the time when the students move in. Then the number of students in each dormitory of university _x__j_ increases by _k__x__j_ (note that the more dormitories belong to the university, the more students move in each dormitory of the university). 4. Military office number _y__j_ conducts raids on all the dormitories assigned to it and takes all students from there. Thus, at each moment of time each dormitory is assigned to exactly one university and one military office. Initially, all the dormitory are empty. Your task is to process the changes that take place in the campus and answer the queries, how many people currently live in dormitory _q__j_. Input The first line contains two integers, _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20095·105) — the number of dormitories and the number of queries, respectively. Next _m_ lines contain the queries, each of them is given in one of the following formats: «U _a__j_ _b__j_» — merging universities; «M _c__j_ _d__j_» — merging military offices; «A _x__j_» — students of university _x__j_ moving in the dormitories; «Z _y__j_» — a raid in military office _y__j_; «Q _q__j_» — a query asking the number of people in dormitory _q__j_. All the numbers in the queries are positive integers and do not exceed _n_. It is guaranteed that at the moment of the query the universities and military offices, that are present in the query, exist. Output In the _i_-th line print the answer to the _i_-th query asking the number of people in the dormitory. Examples Input 2 7 A 1 Q 1 U 1 2 A 1 Z 1 Q 1 Q 2 Input 5 12 U 1 2 M 4 5 A 1 Q 1 A 3 A 4 Q 3 Q 4 Z 4 Q 4 A 5 Q 5 Note Consider the first sample test: In the first query university 1 owns only dormitory 1, so after the query dormitory 1 will have 1 student. After the third query university 1 owns dormitories 1 and 2. The fourth query increases by 2 the number of students living in dormitories 1 and 2 that belong to university number 1. After that 3 students live in the first dormitory and 2 students live in the second dormitory. At the fifth query the number of students living in dormitory 1, assigned to the military office 1, becomes zero. | 3,100 | false | false | false | false | true | false | false | true | false | false | 7,563 |
1582C | Grandma Capa has decided to knit a scarf and asked Grandpa Sher to make a pattern for it, a pattern is a string consisting of lowercase English letters. Grandpa Sher wrote a string $$$s$$$ of length $$$n$$$. Grandma Capa wants to knit a beautiful scarf, and in her opinion, a beautiful scarf can only be knit from a string that is a palindrome. She wants to change the pattern written by Grandpa Sher, but to avoid offending him, she will choose one lowercase English letter and erase some (at her choice, possibly none or all) occurrences of that letter in string $$$s$$$. She also wants to minimize the number of erased symbols from the pattern. Please help her and find the minimum number of symbols she can erase to make string $$$s$$$ a palindrome, or tell her that it's impossible. Notice that she can only erase symbols equal to the one letter she chose. A string is a palindrome if it is the same from the left to the right and from the right to the left. For example, the strings 'kek', 'abacaba', 'r' and 'papicipap' are palindromes, while the strings 'abb' and 'iq' are not. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The next $$$2 cdot t$$$ lines contain the description of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) — the length of the string. The second line of each test case contains the string $$$s$$$ consisting of $$$n$$$ lowercase English letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case print the minimum number of erased symbols required to make the string a palindrome, if it is possible, and $$$-1$$$, if it is impossible. Example Input 5 8 abcaacab 6 xyzxyz 4 abba 8 rprarlap 10 khyyhhyhky Note In the first test case, you can choose a letter 'a' and erase its first and last occurrences, you will get a string 'bcaacb', which is a palindrome. You can also choose a letter 'b' and erase all its occurrences, you will get a string 'acaaca', which is a palindrome as well. In the second test case, it can be shown that it is impossible to choose a letter and erase some of its occurrences to get a palindrome. In the third test case, you don't have to erase any symbols because the string is already a palindrome. | 1,200 | false | true | false | false | true | false | true | false | false | false | 2,723 |
735E | Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph. Ostap's tree now has _n_ vertices. He wants to paint some vertices of the tree black such that from any vertex _u_ there is at least one black vertex _v_ at distance no more than _k_. Distance between two vertices of the tree is the minimum possible number of edges of the path between them. As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109u2009+u20097. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one. Input The first line of the input contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009100, 0u2009≤u2009_k_u2009≤u2009_min_(20,u2009_n_u2009-u20091))xa0— the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for _k_. Each of the next _n_u2009-u20091 lines contain two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_)xa0— indices of vertices, connected by the _i_-th edge. It's guaranteed that given graph is a tree. Note In the first sample, Ostap has to paint both vertices black. In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both. In the third sample, the valid ways to paint vertices are: {1,u20093}, {1,u20094}, {2,u20093}, {2,u20094}, {1,u20092,u20093}, {1,u20092,u20094}, {1,u20093,u20094}, {2,u20093,u20094}, {1,u20092,u20093,u20094}. | 2,500 | false | false | false | true | false | false | false | false | false | false | 6,848 |
233A | Problem - 233A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation math *800 No tag edit access → Contest materials (_n_ is the permutation size) the following equations hold _p__p__i_u2009=u2009_i_ and _p__i_u2009≠u2009_i_. Nickolas asks you to print any perfect permutation of size _n_ for the given _n_. Input A single line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the permutation size. Output If a perfect permutation of size _n_ doesn't exist, print a single integer -1. Otherwise print _n_ distinct integers from 1 to _n_, _p_1,u2009_p_2,u2009...,u2009_p__n_ — permutation _p_, that is perfect. Separate printed numbers by whitespaces. Examples Input 1 Output -1 Input 2 Output 2 1 Input 4 Output 2 1 4 3 | 800 | true | false | true | false | false | false | false | false | false | false | 8,906 |
933C | Problem - 933C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags geometry graphs *2700 No tag edit access → Contest materials , denoting the number of circles. The following _n_ lines each contains three space-separated integers _x_, _y_ and _r_ (u2009-u200910u2009≤u2009_x_,u2009_y_u2009≤u200910, 1u2009≤u2009_r_u2009≤u200910), describing a circle whose center is (_x_,u2009_y_) and the radius is _r_. No two circles have the same _x_, _y_ and _r_ at the same time. Output Print a single integerxa0— the number of regions on the plane. Examples Input 3 0 0 1 2 0 1 4 0 1 Output 4 Input 3 0 0 2 3 0 2 6 0 2 Output 6 Input 3 0 0 2 2 0 2 1 1 2 Output 8 Note For the first example, For the second example, For the third example, | 2,700 | false | false | false | false | false | false | false | false | false | true | 5,969 |
329D | Important: All possible tests are in the pretest, so you shouldn't hack on this problem. So, if you passed pretests, you will also pass the system test. You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak monsters, you arrived at a square room consisting of tiles forming an _n_u2009×u2009_n_ grid, surrounded entirely by walls. At the end of the room lies a door locked with evil magical forces. The following inscriptions are written on the door: The sound of clashing rocks will awaken the door! Being a very senior adventurer, you immediately realize what this means. In the room next door lies an infinite number of magical rocks. There are four types of rocks: '^': this rock moves upwards; '<': this rock moves leftwards; '>': this rock moves rightwards; 'v': this rock moves downwards. To open the door, you first need to place the rocks on some of the tiles (one tile can be occupied by at most one rock). Then, you select a single rock that you have placed and activate it. The activated rock will then move in its direction until it hits another rock or hits the walls of the room (the rock will not move if something already blocks it in its chosen direction). The rock then deactivates. If it hits the walls, or if there have been already 107 events of rock becoming activated, the movements end. Otherwise, the rock that was hit becomes activated and this procedure is repeated. If a rock moves at least one cell before hitting either the wall or another rock, the hit produces a sound. The door will open once the number of produced sounds is at least _x_. It is okay for the rocks to continue moving after producing _x_ sounds. The following picture illustrates the four possible scenarios of moving rocks. Moves at least one cell, then hits another rock. A sound is produced, the hit rock becomes activated. Moves at least one cell, then hits the wall (i.e., the side of the room). A sound is produced, the movements end. Does not move because a rock is already standing in the path. The blocking rock becomes activated, but no sounds are produced. Does not move because the wall is in the way. No sounds are produced and the movements end. Assume there's an infinite number of rocks of each type in the neighboring room. You know what to do: place the rocks and open the door! Input The first line will consists of two integers _n_ and _x_, denoting the size of the room and the number of sounds required to open the door. There will be exactly three test cases for this problem: _n_u2009=u20095,u2009_x_u2009=u20095; _n_u2009=u20093,u2009_x_u2009=u20092; _n_u2009=u2009100,u2009_x_u2009=u2009105. All of these testcases are in pretest. Output Output _n_ lines. Each line consists of _n_ characters — the _j_-th character of the _i_-th line represents the content of the tile at the _i_-th row and the _j_-th column, and should be one of these: '^', '<', '>', or 'v': a rock as described in the problem statement. '.': an empty tile. Then, output two integers _r_ and _c_ (1u2009≤u2009_r_,u2009_c_u2009≤u2009_n_) on the next line — this means that the rock you activate first is located at the _r_-th row from above and _c_-th column from the left. There must be a rock in this cell. If there are multiple solutions, you may output any of them. Examples Output >...v v.<.. ..^.. >.... ..^.< 1 1 Note Here's a simulation of the first example, accompanied with the number of sounds produced so far. 0 sound 1 sound 2 sounds 3 sounds 4 sounds still 4 sounds In the picture above, the activated rock switches between the '^' rock and the '<' rock. However, no sound is produced since the '^' rock didn't move even a single tile. So, still 4 sound. 5 sounds At this point, 5 sound are already produced, so this solution is already correct. However, for the sake of example, we will continue simulating what happens. 6 sounds 7 sounds still 7 sounds 8 sounds And the movement stops. In total, it produces 8 sounds. Notice that the last move produced sound. Here's a simulation of the second example: 0 sound 1 sound 2 sounds Now, the activated stone will switch continuously from one to another without producing a sound until it reaches the 107 limit, after which the movement will cease. In total, it produced exactly 2 sounds, so the solution is correct. | 2,500 | false | false | false | false | false | true | false | false | false | false | 8,526 |
690B3 | Zombies have found out about the Zombie Contamination level checker and managed to damage it! Now detecting the shape of their main compound will be a real challenge for Heidi. As before, a lair can be represented as a strictly convex polygon on a lattice. Each vertex of the polygon occupies a point on the lattice. However, the damaged Zombie Contamination level checker can only tell, for each cell, whether the level of Zombie Contamination for that cell is in the set {1,u20092,u20093}. In other words, Heidi knows all the cells of the lattice for which the Contamination level is not 0 and not 4. Given this information, Heidi still wants to know the exact shape of the lair to rain destruction on the zombies. Help her! Input The input contains multiple test cases. The first line of each test case contains two space-separated integers _N_ and _M_, where _N_ is the size of the lattice grid (5u2009≤u2009_N_u2009≤u2009100000) and _M_ is the number of lattice points for which the Zombie Contamination level is 1, 2, or 3 (8u2009≤u2009_M_u2009≤u2009200000). The second line of each test case contains _M_ pairs of integers _x_1,u2009_y_1,u2009...,u2009_x__M_,u2009_y__M_ – coordinates of the cells with Zombie Contamination level not equal to 0 nor 4. It is guaranteed that 1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_N_. All pairs _x__i_,u2009_y__i_ are different. Cells are enumerated based on the coordinates of their upper right corner. This means that the bottommost leftmost cell that touches the origin has coordinates (1,u20091), and the uppermost leftmost cell is identified as (1,u2009_N_). The last line of the file contains two zeroes. This line should not be treated as a test case. The sum of the _M_ values for all tests in one file will not exceed 200000. Output For each test case, the following output is expected: The first line of the output should contain one integer _V_, the number of vertices of the polygon that is the secret lair. The next _V_ lines each should contain two integers, denoting the vertices of the polygon in the clockwise order, starting from the lexicographically smallest vertex. Example Input 8 19 2 3 2 4 2 5 3 3 3 5 4 3 4 5 4 6 5 2 5 3 5 6 6 2 6 3 6 4 6 5 6 6 6 7 7 6 7 7 5 8 2 2 2 3 2 4 3 2 3 4 4 2 4 3 4 4 0 0 Output 4 2 3 2 4 6 6 5 2 4 2 2 2 3 3 3 3 2 Note It is guaranteed that the solution always exists and is unique. It is guaranteed that in the correct solution the coordinates of the polygon vertices are between 1 and _N_u2009-u20091. A vertex (_x_1,u2009_y_1) is lexicographically smaller than vertex (_x_2,u2009_y_2) if _x_1u2009<u2009_x_2 or . | 2,600 | false | false | false | false | true | false | false | false | false | false | 7,059 |
1089F | Problem - 1089F - 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 math *1900 No tag edit access → Contest materials ") for some $$$k$$$ such that: $$$$$$ begin{cases} ext{$b_i$ divides $n$, $1 < b_i < n$ for $i = 1 ldots k$} ext{$1 le a_i < b_i$ for $i = 1 ldots k$} ext{$sumlimits_{i=1}^k frac{a_i}{b_i} = 1 - frac{1}{n}$} end{cases} $$$$$$ Input The input consists of a single integer $$$n$$$ ($$$2 le n le 10^9$$$). Output In the first line print "YES" if there exists such a sequence of fractions or "NO" otherwise. If there exists such a sequence, next lines should contain a description of the sequence in the following format. The second line should contain integer $$$k$$$ ($$$1 le k le 100,000$$$)xa0— the number of elements in the sequence. It is guaranteed that if such a sequence exists, then there exists a sequence of length at most $$$100,000$$$. Next $$$k$$$ lines should contain fractions of the sequence with two integers $$$a_i$$$ and $$$b_i$$$ on each line. Examples Input 2 Output NO Input 6 Output YES 2 1 2 1 3 Note In the second example there is a sequence $$$frac{1}{2}, frac{1}{3}$$$ such that $$$frac{1}{2} + frac{1}{3} = 1 - frac{1}{6}$$$. | 1,900 | true | false | false | false | false | false | false | false | false | false | 5,280 |
101D | Gerald is positioned in an old castle which consists of _n_ halls connected with _n_u2009-u20091 corridors. It is exactly one way to go from any hall to any other one. Thus, the graph is a tree. Initially, at the moment of time 0, Gerald is positioned in hall 1. Besides, some other hall of the castle contains the treasure Gerald is looking for. The treasure's position is not known; it can equiprobably be in any of other _n_u2009-u20091 halls. Gerald can only find out where the treasure is when he enters the hall with the treasure. That very moment Gerald sees the treasure and the moment is regarded is the moment of achieving his goal. The corridors have different lengths. At that, the corridors are considered long and the halls are considered small and well lit. Thus, it is possible not to take the time Gerald spends in the halls into consideration. The castle is very old, that's why a corridor collapses at the moment when somebody visits it two times, no matter in which direction. Gerald can move around the castle using the corridors; he will go until he finds the treasure. Naturally, Gerald wants to find it as quickly as possible. In other words, he wants to act in a manner that would make the average time of finding the treasure as small as possible. Each corridor can be used no more than two times. That's why Gerald chooses the strategy in such a way, so he can visit every hall for sure. More formally, if the treasure is located in the second hall, then Gerald will find it the moment he enters the second hall for the first time — let it be moment _t_2. If the treasure is in the third hall, then Gerald will find it the moment he enters the third hall for the first time. Let it be the moment of time _t_3. And so on. Thus, the average time of finding the treasure will be equal to . Input The first line contains the only integer _n_ (2u2009≤u2009_n_u2009≤u2009105) — the number of halls in the castle. Next _n_u2009-u20091 lines each contain three integers. The _i_-th line contains numbers _a__i_, _b__i_ and _t__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_, 1u2009≤u2009_t__i_u2009≤u20091000) — the numbers of halls connected with the _i_-th corridor and the time needed to go along the corridor. Initially Gerald is in the hall number 1. It is guaranteed that one can get from any hall to any other one using corridors. Output Print the only real number: the sought expectation of time needed to find the treasure. The answer should differ from the right one in no less than 10u2009-u20096. Examples Input 5 1 2 1 1 3 1 1 4 1 1 5 1 Note In the first test the castle only has two halls which means that the treasure is located in the second hall. Gerald will only need one minute to go to the second hall from the first one. In the second test Gerald can only go from the first hall to the third one. He can get from the third room to the first one or to the second one, but he has already visited the first hall and can get nowhere from there. Thus, he needs to go to the second hall. He should go to hall 4 from there, because all other halls have already been visited. If the treasure is located in the third hall, Gerald will find it in a minute, if the treasure is located in the second hall, Gerald finds it in two minutes, if the treasure is in the fourth hall, Gerald will find it in three minutes. The average time makes 2 minutes. In the third test Gerald needs to visit 4 halls: the second, third, fourth and fifth ones. All of them are only reachable from the first hall. Thus, he needs to go to those 4 halls one by one and return. Gerald will enter the first of those halls in a minute, in the second one — in three minutes, in the third one - in 5 minutes, in the fourth one - in 7 minutes. The average time is 4 minutes. | 2,300 | false | true | false | true | false | false | false | false | true | false | 9,476 |
1824C | LuoTianyi gives you a tree with values in its vertices, and the root of the tree is vertex $$$1$$$. In one operation, you can change the value in one vertex to any non-negative integer. Now you need to find the minimum number of operations you need to perform to make each path from the root to leaf$$$^{dagger}$$$ has a — the number of vertices in the tree. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$), the $$$i$$$-th number represents the value in the $$$i$$$-th vertex. Next $$$n−1$$$ lines describe the edges of the tree. The $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i,v_i le n, u_i eq v_i$$$) — the vertices connected by an edge of the tree. It's guaranteed that the given edges form a tree. Note The tree in the first example: If we change the value in the vertex $$$2$$$ to $$$3$$$, the value in the vertex $$$5$$$ to $$$4$$$, and the value in the vertex $$$6$$$ to $$$6$$$, then the tree will be ok. The bitwise XOR from the root to the leaf $$$2$$$ will be $$$3 oplus 3=0$$$. The bitwise XOR from the root to the leaf $$$5$$$ will be $$$4 oplus 7 oplus 3=0$$$. The bitwise XOR from the root to the leaf $$$6$$$ will be $$$6 oplus 5 oplus 3=0$$$. The tree in the second example: If we change the value in the vertex $$$2$$$ to $$$4$$$, the value in the vertex $$$3$$$ to $$$27$$$, and the value in the vertex $$$6$$$ to $$$20$$$, then the tree will be ok. The bitwise XOR from the root to the leaf $$$6$$$ will be $$$20 oplus 19 oplus 7=0$$$. The bitwise XOR from the root to the leaf $$$8$$$ will be $$$11 oplus 27 oplus 4 oplus 19 oplus 7=0$$$. The bitwise XOR from the root to the leaf $$$4$$$ will be $$$16 oplus 4 oplus 19 oplus 7=0$$$. The bitwise XOR from the root to the leaf $$$7$$$ will be $$$16 oplus 4 oplus 19 oplus 7=0$$$. In the third example, the only leaf is the vertex $$$4$$$ and the bitwise XOR on the path to it is $$$1 oplus 2 oplus 1 oplus 2 = 0$$$, so we don't need to change values. In the fourth example, we can change the value in the vertex $$$1$$$ to $$$5$$$, and the value in the vertex $$$4$$$ to $$$0$$$. Here $$$oplus$$$ denotes the bitwise XOR operation. | 2,500 | false | true | false | true | true | false | false | false | false | false | 1,334 |
1676E | Timur has $$$n$$$ candies. The $$$i$$$-th candy has a quantity of sugar equal to $$$a_i$$$. So, by eating the $$$i$$$-th candy, Timur consumes a quantity of sugar equal to $$$a_i$$$. Timur will ask you $$$q$$$ queries regarding his candies. For the $$$j$$$-th query you have to answer what is the minimum number of candies he needs to eat in order to reach a quantity of sugar greater than or equal to $$$x_j$$$ or print -1 if it's not possible to obtain such a quantity. In other words, you should print the minimum possible $$$k$$$ such that after eating $$$k$$$ candies, Timur consumes a quantity of sugar of at least $$$x_j$$$ or say that no possible $$$k$$$ exists. Note that he can't eat the same candy twice and queries are independent of each other (Timur can use the same candy in different queries). Input The first line of input contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) xa0— the number of test cases. The description of test cases follows. The first line contains $$$2$$$ integers $$$n$$$ and $$$q$$$ ($$$1 leq n, q leq 1.5cdot10^5$$$)xa0— the number of candies Timur has and the number of queries you have to print an answer for respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^4$$$)xa0— the quantity of sugar in each of the candies respectively. Then $$$q$$$ lines follow. Each of the next $$$q$$$ lines contains a single integer $$$x_j$$$ ($$$1 leq x_j leq 2 cdot 10^9$$$)xa0– the quantity Timur wants to reach for the given query. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$1.5 cdot 10^5$$$. Output For each test case output $$$q$$$ lines. For the $$$j$$$-th line output the number of candies Timur needs to eat in order to reach a quantity of sugar greater than or equal to $$$x_j$$$ or print -1 if it's not possible to obtain such a quantity. Example Input 3 8 7 4 3 3 1 1 4 5 9 1 10 50 14 15 22 30 4 1 1 2 3 4 3 1 2 5 4 6 Output 1 2 -1 2 3 4 8 1 1 -1 Note For the first test case: For the first query, Timur can eat any candy, and he will reach the desired quantity. For the second query, Timur can reach a quantity of at least $$$10$$$ by eating the $$$7$$$-th and the $$$8$$$-th candies, thus consuming a quantity of sugar equal to $$$14$$$. For the third query, there is no possible answer. For the fourth query, Timur can reach a quantity of at least $$$14$$$ by eating the $$$7$$$-th and the $$$8$$$-th candies, thus consuming a quantity of sugar equal to $$$14$$$. For the second test case: For the only query of the second test case, we can choose the third candy from which Timur receives exactly $$$3$$$ sugar. It's also possible to obtain the same answer by choosing the fourth candy. | 1,100 | false | true | false | false | false | false | false | true | true | false | 2,211 |
626E | Problem - 626E - Codeforces =============== xa0 ]( "23515") integers. Find the non-empty subset (with repetition) with the maximum simple skewness. The mean of a collection is the average of its elements. The median of a collection is its middle element when all of its elements are sorted, or the average of its two middle elements if it has even size. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200 000)xa0— the number of elements in the list. The second line contains _n_ integers _x__i_ (0u2009≤u2009_x__i_u2009≤u20091u2009000u2009000)xa0— the _i_th element of the list. Output In the first line, print a single integer _k_xa0— the size of the subset. In the second line, print _k_ integersxa0— the elements of the subset in any order. If there are multiple optimal subsets, print any. Examples Input 4 1 2 3 12 Output 3 1 2 12 Input 4 1 1 2 2 Output 3 1 1 2 Input 2 1 2 Output 2 1 2 Note In the first case, the optimal subset is , which has mean 5, median 2, and simple skewness of 5u2009-u20092u2009=u20093. In the second case, the optimal subset is . Note that repetition is allowed. In the last case, any subset has the same median and mean, so all have simple skewness of 0. | 2,400 | true | false | false | false | false | false | false | true | false | false | 7,324 |
336E | Vasily the bear has two favorite integers _n_ and _k_ and a pencil. Besides, he's got _k_ jars with different water color paints. All jars are numbered in some manner from 1 to _k_, inclusive. The jar number _i_ contains the paint of the _i_-th color. Initially the bear took a pencil and drew four segments on the coordinate plane. All of them end at point (0,u20090). They begin at: (0,u20092_n_), (0,u2009u2009-u20092_n_), (2_n_,u20090), (u2009-u20092_n_,u20090). Then for each _i_u2009=u20091,u20092,u2009...,u2009_n_, the bear drew two squares. The first square has the following vertex coordinates: (2_i_,u20090), (u2009-u20092_i_,u20090), (0,u2009u2009-u20092_i_), (0,u20092_i_). The second square has the following vertex coordinates: (u2009-u20092_i_u2009-u20091,u2009u2009-u20092_i_u2009-u20091), (u2009-u20092_i_u2009-u20091,u20092_i_u2009-u20091), (2_i_u2009-u20091,u2009u2009-u20092_i_u2009-u20091), (2_i_u2009-u20091,u20092_i_u2009-u20091). After that, the bear drew another square: (1,u20090), (u2009-u20091,u20090), (0,u2009u2009-u20091), (0,u20091). All points mentioned above form the set of points _A_. The sample of the final picture at _n_u2009=u20090 The sample of the final picture at _n_u2009=u20092 The bear decided to paint the resulting picture in _k_ moves. The _i_-th move consists of the following stages: 1. The bear chooses 3 distinct points in set _А_ so that any pair of the chosen points has a segment on the picture between them. The chosen points and segments mark the area that mustn't contain any previously painted points. 2. The bear paints the area bounded by the chosen points and segments the _i_-th color. Note that after the _k_-th move some parts of the picture can stay unpainted. The bear asked you to calculate, how many distinct ways there are to paint his picture. A way to paint the picture is a sequence of three-element sets of points he chose on each step. Two sequences are considered distinct if there is such number _i_ (1u2009≤u2009_i_u2009≤u2009_k_), that the _i_-th members of these sequences do not coincide as sets. As the sought number can be rather large, you only need to calculate the remainder after dividing it by number 1000000007 (109u2009+u20097). | 2,700 | false | false | true | true | false | false | false | false | false | false | 8,488 |
189A | Problem - 189A - 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 *1300 No tag edit access → Contest materials — the length of the original ribbon and the acceptable lengths of the ribbon pieces after the cutting, correspondingly. The numbers _a_, _b_ and _c_ can coincide. Output Print a single number — the maximum possible number of ribbon pieces. It is guaranteed that at least one correct ribbon cutting exists. Examples Input 5 5 3 2 Output 2 Input 7 5 5 2 Output 2 Note In the first example Polycarpus can cut the ribbon in such way: the first piece has length 2, the second piece has length 3. In the second example Polycarpus can cut the ribbon in such way: the first piece has length 5, the second piece has length 2. | 1,300 | false | false | false | true | false | false | true | false | false | false | 9,084 |
912C | A lot of students spend their winter holidays productively. Vlad has advanced very well in doing so! For three days already, fueled by salads and tangerinesxa0— the leftovers from New Year celebrationxa0— he has been calibrating his rating in his favorite MOBA game, playing as a hero named Perun. Perun has an ultimate ability called "Thunderwrath". At the instant of its activation, each enemy on the map (_n_ of them in total) loses health points as a single-time effect. It also has a restriction: it can only activated when the moment of time is an integer. The initial bounty for killing an enemy is . Additionally, it increases by each second. Formally, if at some second _t_ the ability is activated and the _i_-th enemy is killed as a result (i.e. his health drops to zero or lower), Vlad earns units of gold. Every enemy can receive damage, as well as be healed. There are multiple ways of doing so, but Vlad is not interested in details. For each of _n_ enemies he knows: There also _m_ health updates Vlad knows about: Obviously, Vlad wants to maximize his profit. If it's necessary, he could even wait for years to activate his ability at the right second. Help him determine the exact second (note that it must be an integer) from 0 (inclusively) to u2009+u2009∞ so that a single activation of the ability would yield Vlad the maximum possible amount of gold, and print this amount. Input In the first line, two integers are given (separated by spaces)xa0— _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009105, 0u2009≤u2009_m_u2009≤u2009105). In the second line, there are three integers: , and (, ). Each of the following _n_ lines has three integersxa0— , , (, ). The next _m_ lines contain three integers eachxa0— , , (, , ). It is guaranteed that there is no more than one hearth change per second for each enemy: more formally, for each _a_,u2009_b_ so that 1u2009≤u2009_a_,u2009_b_u2009≤u2009_m_,u2009_a_u2009≠u2009_b_ holds that if , then . Output Output the single integerxa0— the maximum amount of gold Vlad can obtain if he applies "Thunderwrath" exactly once, or -1 if this amount can be infinitely large. Examples Input 3 2 1000 10 50 70 5 5 90 70 1 110 20 2 20 2 10 30 3 10 Input 1 1 500 50 1000 750 750 20 10 1 300 Note On the pictures you can see health points of each enemy versus time in sample cases. Periods when Vlad can kill one enemy are marked with yellow color. Periods when Vlad can kill two enemies are marked with purple color. In the first sample case, Vlad can activate the ability at the 50-th second: the enemies 2 and 3 will die since they would have 40 and 50 health points correspondingly. Vlad will earn 2·(1000u2009+u200950·10)u2009=u20093000 gold. In the second sample case, the maximum amount of health for the enemy 1 is less than the damage dealt by the ability. Hence, the enemy could be killed anytime. As the bounty increases by 50 over the time, the maximum possible amount of gold is infinite. | 2,500 | false | true | false | false | false | false | true | false | true | false | 6,094 |
424E | Cold winter evenings in Tomsk are very boring — nobody wants be on the streets at such a time. Residents of Tomsk while away the time sitting in warm apartments, inventing a lot of different games. One of such games is 'Colored Jenga'. This game requires wooden blocks of three colors: red, green and blue. A tower of _n_ levels is made from them. Each level consists of three wooden blocks. The blocks in each level can be of arbitrary colors, but they are always located close and parallel to each other. An example of such a tower is shown in the figure. The game is played by exactly one person. Every minute a player throws a special dice which has six sides. Two sides of the dice are green, two are blue, one is red and one is black. The dice shows each side equiprobably. If the dice shows red, green or blue, the player must take any block of this color out of the tower at this minute so that the tower doesn't fall. If this is not possible, the player waits until the end of the minute, without touching the tower. He also has to wait until the end of the minute without touching the tower if the dice shows the black side. It is not allowed to take blocks from the top level of the tower (whether it is completed or not). Once a player got a block out, he must put it on the top of the tower so as to form a new level or finish the upper level consisting of previously placed blocks. The newly constructed levels should have all the same properties as the initial levels. If the upper level is not completed, starting the new level is prohibited. For the tower not to fall, in each of the levels except for the top, there should be at least one block. Moreover, if at some of these levels there is exactly one block left and this block is not the middle block, the tower falls. The game ends at the moment when there is no block in the tower that you can take out so that the tower doesn't fall. Here is a wonderful game invented by the residents of the city of Tomsk. I wonder for how many minutes can the game last if the player acts optimally well? If a player acts optimally well, then at any moment he tries to choose the block he takes out so as to minimize the expected number of the game duration. Your task is to write a program that determines the expected number of the desired amount of minutes. Input The first line of the input contains the only integer _n_ (2u2009≤u2009_n_u2009≤u20096) — the number of levels in the tower. Then _n_ lines follow, describing the levels of the tower from the bottom to the top (the first line is the top of the tower). Each level is described by three characters, the first and the third of them set the border blocks of the level and the second one is the middle block. The character that describes the block has one of the following values 'R' (a red block), 'G' (a green block) and 'B' (a blue block). | 2,500 | false | false | false | true | false | false | false | false | false | false | 8,139 |
1185D | A sequence $$$a_1, a_2, dots, a_k$$$ is called an arithmetic progression if for each $$$i$$$ from $$$1$$$ to $$$k$$$ elements satisfy the condition $$$a_i = a_1 + c cdot (i - 1)$$$ for some fixed $$$c$$$. For example, these five sequences are arithmetic progressions: $$$[5, 7, 9, 11]$$$, $$$[101]$$$, $$$[101, 100, 99]$$$, $$$[13, 97]$$$ and $$$[5, 5, 5, 5, 5]$$$. And these four sequences aren't arithmetic progressions: $$$[3, 1, 2]$$$, $$$[1, 2, 4, 8]$$$, $$$[1, -1, 1, -1]$$$ and $$$[1, 2, 3, 3, 3]$$$. You are given a sequence of integers $$$b_1, b_2, dots, b_n$$$. Find any index $$$j$$$ ($$$1 le j le n$$$), such that if you delete $$$b_j$$$ from the sequence, you can reorder the remaining $$$n-1$$$ elements, so that you will get an arithmetic progression. If there is no such index, output the number -1. Input The first line of the input contains one integer $$$n$$$ ($$$2 le n le 2cdot10^5$$$) — length of the sequence $$$b$$$. The second line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$-10^9 le b_i le 10^9$$$) — elements of the sequence $$$b$$$. Output Print such index $$$j$$$ ($$$1 le j le n$$$), so that if you delete the $$$j$$$-th element from the sequence, you can reorder the remaining elements, so that you will get an arithmetic progression. If there are multiple solutions, you are allowed to print any of them. If there is no such index, print -1. Note Note to the first example. If you delete the $$$4$$$-th element, you can get the arithmetic progression $$$[2, 4, 6, 8]$$$. Note to the second example. The original sequence is already arithmetic progression, so you can delete $$$1$$$-st or last element and you will get an arithmetical progression again. | 1,700 | true | false | true | false | false | false | false | false | false | false | 4,776 |
1393D | Carousel Boutique is busy again! Rarity has decided to visit the pony ball and she surely needs a new dress, because going out in the same dress several times is a sign of bad manners. First of all, she needs a dress pattern, which she is going to cut out from the rectangular piece of the multicolored fabric. The piece of the multicolored fabric consists of $$$n imes m$$$ separate square scraps. Since Rarity likes dresses in style, a dress pattern must only include scraps sharing the same color. A dress pattern must be the square, and since Rarity is fond of rhombuses, the sides of a pattern must form a $$$45^{circ}$$$ angle with sides of a piece of fabric (that way it will be resembling the traditional picture of a rhombus). Examples of proper dress patterns: Examples of improper dress patterns: The first one consists of multi-colored scraps, the second one goes beyond the bounds of the piece of fabric, the third one is not a square with sides forming a $$$45^{circ}$$$ angle with sides of the piece of fabric. Rarity wonders how many ways to cut out a dress pattern that satisfies all the conditions that do exist. Please help her and satisfy her curiosity so she can continue working on her new masterpiece! Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2000$$$). Each of the next $$$n$$$ lines contains $$$m$$$ characters: lowercase English letters, the $$$j$$$-th of which corresponds to scrap in the current line and in the $$$j$$$-th column. Scraps having the same letter share the same color, scraps having different letters have different colors. Output Print a single integer: the number of ways to cut out a dress pattern to satisfy all of Rarity's conditions. Examples Input 5 5 zbacg baaac aaaaa eaaad weadd Note In the first example, all the dress patterns of size $$$1$$$ and one of size $$$2$$$ are satisfactory. In the second example, only the dress patterns of size $$$1$$$ are satisfactory. | 2,100 | false | false | true | true | false | false | false | false | false | false | 3,704 |
103A | The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness". The test consists of _n_ questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question _n_. Question _i_ contains _a__i_ answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the _n_ questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves. Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case? Input The first line contains a positive integer _n_ (1u2009≤u2009_n_u2009≤u2009100). It is the number of questions in the test. The second line contains space-separated _n_ positive integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109), the number of answer variants to question _i_. Output Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Note Note to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished. | 1,100 | true | true | true | false | false | false | false | false | false | false | 9,472 |
1327B | The King of Berland Polycarp LXXXIV has $$$n$$$ daughters. To establish his power to the neighbouring kingdoms he wants to marry his daughters to the princes of these kingdoms. As a lucky coincidence there are $$$n$$$ other kingdoms as well. So Polycarp LXXXIV has enumerated his daughters from $$$1$$$ to $$$n$$$ and the kingdoms from $$$1$$$ to $$$n$$$. For each daughter he has compiled a list of kingdoms princes of which she wanted to marry. Polycarp LXXXIV is very busy, so he finds a couple for his daughters greedily one after another. For the first daughter he takes the kingdom with the lowest number from her list and marries the daughter to their prince. For the second daughter he takes the kingdom with the lowest number from her list, prince of which hasn't been taken already. If there are no free princes in the list then the daughter marries nobody and Polycarp LXXXIV proceeds to the next daughter. The process ends after the $$$n$$$-th daughter. For example, let there be $$$4$$$ daughters and kingdoms, the lists daughters have are $$$[2, 3]$$$, $$$[1, 2]$$$, $$$[3, 4]$$$, $$$[3]$$$, respectively. In that case daughter $$$1$$$ marries the prince of kingdom $$$2$$$, daughter $$$2$$$ marries the prince of kingdom $$$1$$$, daughter $$$3$$$ marries the prince of kingdom $$$3$$$, leaving daughter $$$4$$$ nobody to marry to. Actually, before starting the marriage process Polycarp LXXXIV has the time to convince one of his daughters that some prince is also worth marrying to. Effectively, that means that he can add exactly one kingdom to exactly one of his daughter's list. Note that this kingdom should not be present in the daughter's list. Polycarp LXXXIV wants to increase the number of married couples. Unfortunately, what he doesn't have the time for is determining what entry to add. If there is no way to increase the total number of married couples then output that the marriages are already optimal. Otherwise, find such an entry that the total number of married couples increases if Polycarp LXXXIV adds it. If there are multiple ways to add an entry so that the total number of married couples increases then print any of them. For your and our convenience you are asked to answer $$$t$$$ independent test cases. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) — the number of daughters and the number of kingdoms. Each of the next $$$n$$$ lines contains the description of each daughter's list. The first integer $$$k$$$ ($$$0 le k le n$$$) is the number of entries in the $$$i$$$-th daughter's list. After that $$$k$$$ distinct integers follow $$$g_i[1], g_i[2], dots, g_i[k]$$$ ($$$1 le g_i[j] le n$$$) — the indices of the kingdoms in the list in the increasing order ($$$g_i[1] < g_i[2] < dots < g_i[k]$$$). It's guaranteed that the total number of daughters over all test cases does not exceed $$$10^5$$$. It's also guaranteed that the total number of kingdoms in lists over all test cases does not exceed $$$10^5$$$. Output For each test case print the answer to it. Print "IMPROVE" in the first line if Polycarp LXXXIV can add some kingdom to some of his daughter's list so that the total number of married couples increases. The second line then should contain two integers — the index of the daughter and the index of the kingdom Polycarp LXXXIV should add to that daughter's list. If there are multiple ways to add an entry so that the total number of married couples increases then print any of them. Otherwise the only line should contain one word "OPTIMAL". Note The first test case is depicted in the statement. Adding the fourth kingdom to the list of the fourth daughter makes her marry the prince of the fourth kingdom. In the second test case any new entry will increase the number of marriages from $$$0$$$ to $$$1$$$. In the third and the fourth test cases there is no way to add an entry. In the fifth test case there is no way to change the marriages by adding any entry. | 1,200 | false | true | false | false | false | false | true | false | false | true | 4,077 |
898F | A correct expression of the form a+b=c was written; _a_, _b_ and _c_ are non-negative integers without leading zeros. In this expression, the plus and equally signs were lost. The task is to restore the expression. In other words, one character '+' and one character '=' should be inserted into given sequence of digits so that: character'+' is placed on the left of character '=', characters '+' and '=' split the sequence into three non-empty subsequences consisting of digits (let's call the left part a, the middle partxa0— b and the right partxa0— c), all the three parts a, b and c do not contain leading zeros, it is true that a+b=c. It is guaranteed that in given tests answer always exists. Input The first line contains a non-empty string consisting of digits. The length of the string does not exceed 106. Output Output the restored expression. If there are several solutions, you can print any of them. Note that the answer at first should contain two terms (divided with symbol '+'), and then the result of their addition, before which symbol'=' should be. Do not separate numbers and operation signs with spaces. Strictly follow the output format given in the examples. If you remove symbol '+' and symbol '=' from answer string you should get a string, same as string from the input data. Examples Input 123123123456456456579579579 Output 123123123+456456456=579579579 | 2,300 | true | false | false | false | false | false | true | false | false | false | 6,153 |
509F | Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students: You are given a tree _T_ with _n_ vertices, specified by its adjacency matrix _a_[1... _n_,u20091... _n_]. What is the output of the following pseudocode? used[1 ... n] = {0, ..., 0};procedure dfs(v): print v; used[v] = 1; for i = 1, 2, ..., n: if (a[v][i] == 1 and used[i] == 0): dfs(i); dfs(1); In order to simplify the test results checking procedure, Dmitry Olegovich decided to create a tree _T_ such that the result is his favorite sequence _b_. On the other hand, Dmitry Olegovich doesn't want to provide students with same trees as input, otherwise they might cheat. That's why Dmitry Olegovich is trying to find out the number of different trees _T_ such that the result of running the above pseudocode with _T_ as input is exactly the sequence _b_. Can you help him? Two trees with _n_ vertices are called different if their adjacency matrices _a_1 and _a_2 are different, i. e. there exists a pair (_i_,u2009_j_), such that 1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_ and _a_1[_i_][_j_]u2009≠u2009_a_2[_i_][_j_]. Input The first line contains the positive integer _n_ (1u2009≤u2009_n_u2009≤u2009500) — the length of sequence _b_. The second line contains _n_ positive integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009≤u2009_b__i_u2009≤u2009_n_). It is guaranteed that _b_ is a permutation, or in other words, each of the numbers 1,u20092,u2009...,u2009_n_ appears exactly once in the sequence _b_. Also it is guaranteed that _b_1u2009=u20091. | 2,300 | false | false | false | true | false | false | false | false | false | false | 7,801 |
717F | I’m strolling on sunshine, yeah-ah! And doesn’t it feel good! Well, it certainly feels good for our Heroes of Making Magic, who are casually walking on a one-directional road, fighting imps. Imps are weak and feeble creatures and they are not good at much. However, Heroes enjoy fighting them. For fun, if nothing else. Our Hero, Ignatius, simply adores imps. He is observing a line of imps, represented as a zero-indexed array of integers _a_ of length _n_, where _a__i_ denotes the number of imps at the _i_-th position. Sometimes, imps can appear out of nowhere. When heroes fight imps, they select a segment of the line, start at one end of the segment, and finish on the other end, without ever exiting the segment. They can move exactly one cell left or right from their current position and when they do so, they defeat one imp on the cell that they moved to, so, the number of imps on that cell decreases by one. This also applies when heroes appear at one end of the segment, at the beginning of their walk. Their goal is to defeat all imps on the segment, without ever moving to an empty cell in it (without imps), since they would get bored. Since Ignatius loves imps, he doesn’t really want to fight them, so no imps are harmed during the events of this task. However, he would like you to tell him whether it would be possible for him to clear a certain segment of imps in the above mentioned way if he wanted to. You are given _q_ queries, which have two types: 1 _a_ _b_ _k_xa0— denotes that _k_ imps appear at each cell from the interval [_a_,u2009_b_] 2 _a_ _b_ - asks whether Ignatius could defeat all imps on the interval [_a_,u2009_b_] in the way described above Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000), the length of the array _a_. The following line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u20095u2009000), the initial number of imps in each cell. The third line contains a single integer _q_ (1u2009≤u2009_q_u2009≤u2009300u2009000), the number of queries. The remaining _q_ lines contain one query each. Each query is provided by integers _a_, _b_ and, possibly, _k_ (0u2009≤u2009_a_u2009≤u2009_b_u2009<u2009_n_, 0u2009≤u2009_k_u2009≤u20095u2009000). Note For the first query, one can easily check that it is indeed impossible to get from the first to the last cell while clearing everything. After we add 1 to the second position, we can clear the segment, for example by moving in the following way: . | 2,600 | false | false | false | false | true | false | false | false | false | false | 6,947 |
1166C | The legend of the foundation of Vectorland talks of two integers $$$x$$$ and $$$y$$$. Centuries ago, the array king placed two markers at points $$$x$$$ and $$$y$$$ on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland. Many years later, the vector king placed markers at points $$$x - y$$$ and $$$x + y$$$ and conquered all the land in between (including the endpoints), which he declared to be Vectorland. He did so in such a way that the land of Arrayland was completely inside (including the endpoints) the land of Vectorland. Here $$$z$$$ denotes the absolute value of $$$z$$$. Now, Jose is stuck on a question of his history exam: "What are the values of $$$x$$$ and $$$y$$$?" Jose doesn't know the answer, but he believes he has narrowed the possible answers down to $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$. Now, he wants to know the number of unordered pairs formed by two different elements from these $$$n$$$ integers such that the legend could be true if $$$x$$$ and $$$y$$$ were equal to these two values. Note that it is possible that Jose is wrong, and that no pairs could possibly make the legend true. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) xa0— the number of choices. The second line contains $$$n$$$ pairwise distinct integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$)xa0— the choices Jose is considering. Output Print a single integer numberxa0— the number of unordered pairs $$${x, y}$$$ formed by different numbers from Jose's choices that could make the legend true. Note Consider the first sample. For the pair $$${2, 5}$$$, the situation looks as follows, with the Arrayland markers at $$$2 = 2$$$ and $$$5 = 5$$$, while the Vectorland markers are located at $$$2 - 5 = 3$$$ and $$$2 + 5 = 7$$$: The legend is not true in this case, because the interval $$$[2, 3]$$$ is not conquered by Vectorland. For the pair $$${5, -3}$$$ the situation looks as follows, with Arrayland consisting of the interval $$$[3, 5]$$$ and Vectorland consisting of the interval $$$[2, 8]$$$: As Vectorland completely contains Arrayland, the legend is true. It can also be shown that the legend is true for the pair $$${2, -3}$$$, for a total of two pairs. In the second sample, the only pair is $$${3, 6}$$$, and the situation looks as follows: Note that even though Arrayland and Vectorland share $$$3$$$ as endpoint, we still consider Arrayland to be completely inside of Vectorland. | 1,500 | false | false | false | false | false | false | false | true | true | false | 4,887 |
1764F | Doremy has an edge-weighted tree with $$$n$$$ vertices whose weights are integers between $$$1$$$ and $$$10^9$$$. She does $$$frac{n(n+1)}{2}$$$ experiments on it. In each experiment, Doremy chooses vertices $$$i$$$ and $$$j$$$ such that $$$j leq i$$$ and connects them directly with an edge with weight $$$1$$$. Then, there is exactly one cycle (or self-loop when $$$i=j$$$) in the graph. Doremy defines $$$f(i,j)$$$ as the sum of lengths of shortest paths from every vertex to the cycle. Formally, let $$$mathrm{dis}_{i,j}(x,y)$$$ be the length of the shortest path between vertex $$$x$$$ and $$$y$$$ when the edge $$$(i,j)$$$ of weight $$$1$$$ is added, and $$$S_{i,j}$$$ be the set of vertices that are on the cycle when edge $$$(i,j)$$$ is added. Then, $$$$$$ f(i,j)=sum_{x=1}^{n}left(min_{yin S_{i,j}}mathrm{dis}_{i,j}(x,y) ight). $$$$$$ Doremy writes down all values of $$$f(i,j)$$$ such that $$$1 leq j leq i leq n$$$, then goes to sleep. However, after waking up, she finds that the tree has gone missing. Fortunately, the values of $$$f(i,j)$$$ are still in her notebook, and she knows which $$$i$$$ and $$$j$$$ they belong to. Given the values of $$$f(i,j)$$$, can you help her restore the tree? It is guaranteed that at least one suitable tree exists. Input The first line of input contains a single integer $$$n$$$ ($$$2 le n le 2000$$$)xa0— the number of vertices in the tree. The following $$$n$$$ lines contain a lower-triangular matrix with $$$i$$$ integers on the $$$i$$$-th line; the $$$j$$$-th integer on the $$$i$$$-th line is $$$f(i,j)$$$ ($$$0 le f(i,j) le 2cdot 10^{15}$$$). It is guaranteed that there exists a tree whose weights are integers between $$$1$$$ and $$$10^9$$$ such that the values of $$$f(i,j)$$$ of the tree match those given in the input. Output Print $$$n-1$$$ lines describing the tree. In the $$$i$$$-th line of the output, output three integers $$$u_i$$$, $$$v_i$$$, $$$w_i$$$ ($$$1 le u_i,v_i le n$$$, $$$1 le w_i le 10^9$$$), representing an edge $$$(u_i,v_i)$$$ whose weight is $$$w_i$$$. If there are multiple answers, you may output any. All edges must form a tree and all values of $$$f(i,j)$$$ must match those given in the input. Examples Input 9 8081910646 8081902766 8081903751 8081902555 8081903540 8081905228 3090681001 3090681986 3090681775 7083659398 7083657913 7083658898 7083658687 2092437133 15069617722 1748216295 1748217280 1748217069 5741194692 749972427 10439821163 2377558289 2377559274 2377559063 6370536686 1379314421 5028071980 8866466178 1746983932 1746984917 1746984706 5739962329 748740064 10438588800 5026839617 10448447704 2341942133 2341943118 2341942907 6334920530 1343698265 4992455824 8830850022 4991223461 9115779270 Output 1 2 985 2 3 211 2 4 998244353 2 5 998244853 4 6 671232353 6 8 1232363 4 7 356561356 7 9 35616156 Note In the first test case, the picture below, from left to right, from top to bottom, shows the graph when pairs $$$(1,1)$$$, $$$(1,2)$$$, $$$(1,3)$$$, $$$(2,2)$$$, $$$(2,3)$$$, $$$(3,3)$$$ are connected with an edge, respectively. The nodes colored yellow are on the cycle. | 2,500 | false | false | false | false | false | true | true | false | true | false | 1,722 |
120A | A sky scraper with 1000 floors has been built in the city of N. It has modern superfast elevators to help to travel from one floor to another. Each elevator has two doors, the front one and the back one. If one goes in through the front door, he goes out through the back one and vice versa. The elevator has two rails numbered with numbers 1 and 2. Rail 1 is located to the left of the entrance to the front door (or correspondingly, to the right of the entrance to the back door). Rail 2 is located opposite it, to the right of the entrance to the front door and to the left of the entrance to the back door. We know that each person in the city of N holds at a rail with the strongest hand. One day a VIP person visited the city and of course, he took a look at the skyscraper and took a ride in the elevator. We know the door through which he entered and the rail he was holding at. Now we need to determine as soon as possible whether he is left-handed or right-handed. Input The first line indicates the door through which the very important person entered the elevator. It contains "front" if the person enters the elevator through the front door and "back" if he entered the elevator through the back door. The second line contains integer _a_ (1u2009≤u2009_a_u2009≤u20092) which denotes the number of the rail at which the person was holding. Output Print character "R" if the VIP is right-handed or "L" if he is left-handed. | 1,000 | true | false | true | false | false | false | true | false | false | false | 9,406 |
1025B | During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers $$$(a_1, b_1)$$$, $$$(a_2, b_2)$$$, ..., $$$(a_n, b_n)$$$ their WCD is arbitrary integer greater than $$$1$$$, such that it divides at least one element in each pair. WCD may not exist for some lists. For example, if the list looks like $$$[(12, 15), (25, 18), (10, 24)]$$$, then their WCD can be equal to $$$2$$$, $$$3$$$, $$$5$$$ or $$$6$$$ (each of these numbers is strictly greater than $$$1$$$ and divides at least one number in each pair). You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 150,000$$$)xa0— the number of pairs. Each of the next $$$n$$$ lines contains two integer values $$$a_i$$$, $$$b_i$$$ ($$$2 le a_i, b_i le 2 cdot 10^9$$$). Output Print a single integerxa0— the WCD of the set of pairs. If there are multiple possible answers, output any; if there is no answer, print $$$-1$$$. Note In the first example the answer is $$$6$$$ since it divides $$$18$$$ from the first pair, $$$24$$$ from the second and $$$12$$$ from the third ones. Note that other valid answers will also be accepted. In the second example there are no integers greater than $$$1$$$ satisfying the conditions. In the third example one of the possible answers is $$$5$$$. Note that, for example, $$$15$$$ is also allowed, but it's not necessary to maximize the output. | 1,600 | false | true | false | false | false | false | true | false | false | false | 5,582 |
938D | Musicians of a popular band "Flayer" have announced that they are going to "make their exit" with a world tour. Of course, they will visit Berland as well. There are _n_ cities in Berland. People can travel between cities using two-directional train routes; there are exactly _m_ routes, _i_-th route can be used to go from city _v__i_ to city _u__i_ (and from _u__i_ to _v__i_), and it costs _w__i_ coins to use this route. Each city will be visited by "Flayer", and the cost of the concert ticket in _i_-th city is _a__i_ coins. You have friends in every city of Berland, and they, knowing about your programming skills, asked you to calculate the minimum possible number of coins they have to pay to visit the concert. For every city _i_ you have to compute the minimum number of coins a person from city _i_ has to spend to travel to some city _j_ (or possibly stay in city _i_), attend a concert there, and return to city _i_ (if _j_u2009≠u2009_i_). Formally, for every you have to calculate , where _d_(_i_,u2009_j_) is the minimum number of coins you have to spend to travel from city _i_ to city _j_. If there is no way to reach city _j_ from city _i_, then we consider _d_(_i_,u2009_j_) to be infinitely large. Input The first line contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u20092·105, 1u2009≤u2009_m_u2009≤u20092·105). Then _m_ lines follow, _i_-th contains three integers _v__i_, _u__i_ and _w__i_ (1u2009≤u2009_v__i_,u2009_u__i_u2009≤u2009_n_,u2009_v__i_u2009≠u2009_u__i_, 1u2009≤u2009_w__i_u2009≤u20091012) denoting _i_-th train route. There are no multiple train routes connecting the same pair of cities, that is, for each (_v_,u2009_u_) neither extra (_v_,u2009_u_) nor (_u_,u2009_v_) present in input. The next line contains _n_ integers _a_1,u2009_a_2,u2009... _a__k_ (1u2009≤u2009_a__i_u2009≤u20091012) — price to attend the concert in _i_-th city. Output Print _n_ integers. _i_-th of them must be equal to the minimum number of coins a person from city _i_ has to spend to travel to some city _j_ (or possibly stay in city _i_), attend a concert there, and return to city _i_ (if _j_u2009≠u2009_i_). | 2,000 | false | false | false | false | true | false | false | false | false | true | 5,948 |
282C | The Bitlandians are quite weird people. They do everything differently. They have a different alphabet so they have a different definition for a string. A Bitlandish string is a string made only of characters "0" and "1". BitHaval (the mayor of Bitland) loves to play with Bitlandish strings. He takes some Bitlandish string _a_, and applies several (possibly zero) operations to it. In one operation the mayor may take any two adjacent characters of a string, define one of them as _x_ and the other one as _y_. Then he calculates two values _p_ and _q_: _p_u2009=u2009_x_xa0_xor_xa0_y_, _q_u2009=u2009_x_xa0_or_xa0_y_. Then he replaces one of the two taken characters by _p_ and the other one by _q_. The _xor_ operation means the bitwise excluding OR operation. The _or_ operation is the bitwise OR operation. So for example one operation can transform string 11 to string 10 or to string 01. String 1 cannot be transformed into any other string. You've got two Bitlandish strings _a_ and _b_. Your task is to check if it is possible for BitHaval to transform string _a_ to string _b_ in several (possibly zero) described operations. Input The first line contains Bitlandish string _a_, the second line contains Bitlandish string _b_. The strings can have different lengths. It is guaranteed that the given strings only consist of characters "0" and "1". The strings are not empty, their length doesn't exceed 106. Output Print "YES" if _a_ can be transformed into _b_, otherwise print "NO". Please do not print the quotes. | 1,500 | true | false | true | false | false | true | false | false | false | false | 8,706 |
1851C | Vlad remembered that he had a series of $$$n$$$ tiles and a number $$$k$$$. The tiles were numbered from left to right, and the $$$i$$$-th tile had colour $$$c_i$$$. If you stand on the first tile and start jumping any number of tiles right, you can get a path of length $$$p$$$. The length of the path is the number of tiles you stood on. Vlad wants to see if it is possible to get a path of length $$$p$$$ such that: it ends at tile with index $$$n$$$; $$$p$$$ is divisible by $$$k$$$ the path is divided into blocks of length exactly $$$k$$$ each; tiles in each block have the same colour, the colors in adjacent blocks are not necessarily different. For example, let $$$n = 14$$$, $$$k = 3$$$. The colours of the tiles are contained in the array $$$c$$$ = [$$$color{red}{1}, color{violet}{2}, color{red}{1}, color{red}{1}, color{gray}{7}, color{orange}{5}, color{green}{3}, color{green}{3}, color{red}{1}, color{green}{3}, color{blue}{4}, color{blue}{4}, color{violet}{2}, color{blue}{4}$$$]. Then we can construct a path of length $$$6$$$ consisting of $$$2$$$ blocks: $$$color{red}{c_1} ightarrow color{red}{c_3} ightarrow color{red}{c_4} ightarrow color{blue}{c_{11}} ightarrow color{blue}{c_{12}} ightarrow color{blue}{c_{14}}$$$ All tiles from the $$$1$$$-st block will have colour $$$color{red}{ extbf{1}}$$$, from the $$$2$$$-nd block will have colour $$$color{blue}{ extbf{4}}$$$. It is also possible to construct a path of length $$$9$$$ in this example, in which all tiles from the $$$1$$$-st block will have colour $$$color{red}{ extbf{1}}$$$, from the $$$2$$$-nd block will have colour $$$color{green}{ extbf{3}}$$$, and from the $$$3$$$-rd block will have colour $$$color{blue}{ extbf{4}}$$$. Input The first line of input data contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2 cdot 10^5$$$)—the number of tiles in the series and the length of the block. The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, c_3, dots, c_n$$$ ($$$1 le c_i le n$$$) — the colours of the tiles. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output on a separate line: YES if you can get a path that satisfies these conditions; NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response). | 1,000 | false | true | false | false | false | false | false | false | false | false | 1,172 |
370B | Lately, a national version of a bingo game has become very popular in Berland. There are _n_ players playing the game, each player has a card with numbers. The numbers on each card are distinct, but distinct cards can have equal numbers. The card of the _i_-th player contains _m__i_ numbers. During the game the host takes numbered balls one by one from a bag. He reads the number aloud in a high and clear voice and then puts the ball away. All participants cross out the number if it occurs on their cards. The person who crosses out all numbers from his card first, wins. If multiple people cross out all numbers from their cards at the same time, there are no winners in the game. At the beginning of the game the bag contains 100 balls numbered 1 through 100, the numbers of all balls are distinct. You are given the cards for each player. Write a program that determines whether a player can win the game at the most favorable for him scenario or not. Input The first line of the input contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of the players. Then follow _n_ lines, each line describes a player's card. The line that describes a card starts from integer _m__i_ (1u2009≤u2009_m__i_u2009≤u2009100) that shows how many numbers the _i_-th player's card has. Then follows a sequence of integers _a__i_,u20091,u2009_a__i_,u20092,u2009...,u2009_a__i_,u2009_m__i_ (1u2009≤u2009_a__i_,u2009_k_u2009≤u2009100) — the numbers on the _i_-th player's card. The numbers in the lines are separated by single spaces. It is guaranteed that all the numbers on each card are distinct. Output Print _n_ lines, the _i_-th line must contain word "YES" (without the quotes), if the _i_-th player can win, and "NO" (without the quotes) otherwise. | 1,300 | false | false | true | false | false | false | false | false | false | false | 8,352 |
106C | Lavrenty, a baker, is going to make several buns with stuffings and sell them. Lavrenty has _n_ grams of dough as well as _m_ different stuffing types. The stuffing types are numerated from 1 to _m_. Lavrenty knows that he has _a__i_ grams left of the _i_-th stuffing. It takes exactly _b__i_ grams of stuffing _i_ and _c__i_ grams of dough to cook a bun with the _i_-th stuffing. Such bun can be sold for _d__i_ tugriks. Also he can make buns without stuffings. Each of such buns requires _c_0 grams of dough and it can be sold for _d_0 tugriks. So Lavrenty can cook any number of buns with different stuffings or without it unless he runs out of dough and the stuffings. Lavrenty throws away all excess material left after baking. Find the maximum number of tugriks Lavrenty can earn. Input The first line contains 4 integers _n_, _m_, _c_0 and _d_0 (1u2009≤u2009_n_u2009≤u20091000, 1u2009≤u2009_m_u2009≤u200910, 1u2009≤u2009_c_0,u2009_d_0u2009≤u2009100). Each of the following _m_ lines contains 4 integers. The _i_-th line contains numbers _a__i_, _b__i_, _c__i_ and _d__i_ (1u2009≤u2009_a__i_,u2009_b__i_,u2009_c__i_,u2009_d__i_u2009≤u2009100). Output Print the only number — the maximum number of tugriks Lavrenty can earn. Examples Input 10 2 2 1 7 3 2 100 12 3 1 10 Input 100 1 25 50 15 5 20 10 Note To get the maximum number of tugriks in the first sample, you need to cook 2 buns with stuffing 1, 4 buns with stuffing 2 and a bun without any stuffing. In the second sample Lavrenty should cook 4 buns without stuffings. | 1,700 | false | false | false | true | false | false | false | false | false | false | 9,459 |
1608B | You are given three integers $$$n, a, b$$$. Determine if there exists a permutation $$$p_1, p_2, ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that: There are exactly $$$a$$$ integers $$$i$$$ with $$$2 le i le n-1$$$ such that $$$p_{i-1} < p_i > p_{i+1}$$$ (in other words, there are exactly $$$a$$$ local maximums). There are exactly $$$b$$$ integers $$$i$$$ with $$$2 le i le n-1$$$ such that $$$p_{i-1} > p_i < p_{i+1}$$$ (in other words, there are exactly $$$b$$$ local minimums). If such permutations exist, find any such permutation. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$2 leq n leq 10^5$$$, $$$0 leq a,b leq n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Output For each test case, if there is no permutation with the requested properties, output $$$-1$$$. Otherwise, print the permutation that you are found. If there are several such permutations, you may print any of them. Example Output 1 3 2 4 4 2 3 1 5 6 -1 Note In the first test case, one example of such permutations is $$$[1, 3, 2, 4]$$$. In it $$$p_1 < p_2 > p_3$$$, and $$$2$$$ is the only such index, and $$$p_2> p_3 < p_4$$$, and $$$3$$$ the only such index. One can show that there is no such permutation for the third test case. | 1,200 | false | true | false | false | false | true | false | false | false | false | 2,614 |
847K | In the evening Polycarp decided to analyze his today's travel expenses on public transport. The bus system in the capital of Berland is arranged in such a way that each bus runs along the route between two stops. Each bus has no intermediate stops. So each of the buses continuously runs along the route from one stop to the other and back. There is at most one bus running between a pair of stops. Polycarp made _n_ trips on buses. About each trip the stop where he started the trip and the the stop where he finished are known. The trips follow in the chronological order in Polycarp's notes. It is known that one trip on any bus costs _a_ burles. In case when passenger makes a transshipment the cost of trip decreases to _b_ burles (_b_u2009<u2009_a_). A passenger makes a transshipment if the stop on which he boards the bus coincides with the stop where he left the previous bus. Obviously, the first trip can not be made with transshipment. For example, if Polycarp made three consecutive trips: "BerBank" "University", "University" "BerMall", "University" "BerBank", then he payed _a_u2009+u2009_b_u2009+u2009_a_u2009=u20092_a_u2009+u2009_b_ burles. From the BerBank he arrived to the University, where he made transshipment to the other bus and departed to the BerMall. Then he walked to the University and returned to the BerBank by bus. Also Polycarp can buy no more than _k_ travel cards. Each travel card costs _f_ burles. The travel card for a single bus route makes free of charge any trip by this route (in both directions). Once purchased, a travel card can be used any number of times in any direction. What is the smallest amount of money Polycarp could have spent today if he can buy no more than _k_ travel cards? Input The first line contains five integers _n_,u2009_a_,u2009_b_,u2009_k_,u2009_f_ (1u2009≤u2009_n_u2009≤u2009300, 1u2009≤u2009_b_u2009<u2009_a_u2009≤u2009100, 0u2009≤u2009_k_u2009≤u2009300, 1u2009≤u2009_f_u2009≤u20091000) where: _n_ — the number of Polycarp trips, _a_ — the cost of a regualar single trip, _b_ — the cost of a trip after a transshipment, _k_ — the maximum number of travel cards Polycarp can buy, _f_ — the cost of a single travel card. The following _n_ lines describe the trips in the chronological order. Each line contains exactly two different words separated by a single space — the name of the start stop and the name of the finish stop of the trip. All names consist of uppercase and lowercase English letters and have lengths between 1 to 20 letters inclusive. Uppercase and lowercase letters should be considered different. Note In the first example Polycarp can buy travel card for the route "BerBank University" and spend 8 burles. Note that his second trip "University" "BerMall" was made after transshipment, so for this trip Polycarp payed 3 burles. So the minimum total sum equals to 8u2009+u20093u2009=u200911 burles. In the second example it doesn't make sense to buy travel cards. Note that each of Polycarp trip (except the first) was made with transshipment. So the minimum total sum equals to 2u2009+u20091u2009+u20091u2009+u20091u2009=u20095 burles. | 1,800 | false | true | true | false | false | false | false | false | true | false | 6,361 |
1733B | There is a badminton championship in which $$$n$$$ players take part. The players are numbered from $$$1$$$ to $$$n$$$. The championship proceeds as follows: player $$$1$$$ and player $$$2$$$ play a game, then the winner and player $$$3$$$ play a game, and then the winner and player $$$4$$$ play a game, and so on. So, $$$n-1$$$ games are played, and the winner of the last game becomes the champion. There are no draws in the games. You want to find out the result of championship. Currently, you only know the following information: Each player has either won $$$x$$$ games or $$$y$$$ games in the championship. Given $$$n$$$, $$$x$$$, and $$$y$$$, find out if there is a result that matches this information. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0— the number of test cases. The only line of each test case contains three integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$2 le n le 10^5$$$, $$$0 le x, y < n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output Print the answer for each test case, one per line. If there is no result that matches the given information about $$$n$$$, $$$x$$$, $$$y$$$, print $$$-1$$$. Otherwise, print $$$n-1$$$ space separated integers, where the $$$i$$$-th integer is the player number of the winner of the $$$i$$$-th game. If there are multiple valid results, print any. Example Input 5 5 2 0 8 1 2 3 0 0 2 0 1 6 3 0 Note In the first test case, player $$$1$$$ and player $$$4$$$ won $$$x$$$ times, player $$$2$$$ and player $$$3$$$ won $$$y$$$ times. In the second, third, and fifth test cases, no valid result exists. | 900 | true | false | false | false | false | true | false | false | false | false | 1,891 |
490C | Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won. Having carefully studied the interaction protocol, Polycarpus came to the conclusion that the secret key can be obtained if he properly cuts the public key of the application into two parts. The public key is a long integer which may consist of even a million digits! Polycarpus needs to find such a way to cut the public key into two nonempty parts, that the first (left) part is divisible by _a_ as a separate number, and the second (right) part is divisible by _b_ as a separate number. Both parts should be positive integers that have no leading zeros. Polycarpus knows values _a_ and _b_. Help Polycarpus and find any suitable method to cut the public key. Input The first line of the input contains the public key of the messenger — an integer without leading zeroes, its length is in range from 1 to 106 digits. The second line contains a pair of space-separated positive integers _a_, _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009108). Output In the first line print "YES" (without the quotes), if the method satisfying conditions above exists. In this case, next print two lines — the left and right parts after the cut. These two parts, being concatenated, must be exactly identical to the public key. The left part must be divisible by _a_, and the right part must be divisible by _b_. The two parts must be positive integers having no leading zeros. If there are several answers, print any of them. If there is no answer, print in a single line "NO" (without the quotes). Examples Input 284254589153928171911281811000 1009 1000 Output YES 2842545891539 28171911281811000 | 1,700 | true | false | false | false | false | false | true | false | false | false | 7,875 |
1043F | Problem - 1043F - Codeforces =============== xa0 ]( "Codeforces Round #519 by Botan Investments — анонс") xa0— the number of integers in the sequence. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 300,000$$$). Output If there is no subset of the given sequence with gcd equal to $$$1$$$, output -1. Otherwise, output exactly one integerxa0— the size of the smallest subset with gcd equal to $$$1$$$. Examples Input 3 10 6 15 Output 3 Input 3 2 4 6 Output -1 Input 7 30 60 21 42 70 15 30 Output 3 Note In the first example, selecting a subset of all numbers gives a gcd of $$$1$$$ and for all smaller subsets the gcd is greater than $$$1$$$. In the second example, for all subsets of numbers the gcd is at least $$$2$$$. | 2,500 | true | false | false | true | false | false | false | false | false | false | 5,478 |
1042A | There are $$$n$$$ benches in the Berland Central park. It is known that $$$a_i$$$ people are currently sitting on the $$$i$$$-th bench. Another $$$m$$$ people are coming to the park and each of them is going to have a seat on some bench out of $$$n$$$ available. Let $$$k$$$ be the maximum number of people sitting on one bench after additional $$$m$$$ people came to the park. Calculate the minimum possible $$$k$$$ and the maximum possible $$$k$$$. Nobody leaves the taken seat during the whole process. Input The first line contains a single integer $$$n$$$ $$$(1 le n le 100)$$$ — the number of benches in the park. The second line contains a single integer $$$m$$$ $$$(1 le m le 10,000)$$$ — the number of people additionally coming to the park. Each of the next $$$n$$$ lines contains a single integer $$$a_i$$$ $$$(1 le a_i le 100)$$$ — the initial number of people on the $$$i$$$-th bench. Output Print the minimum possible $$$k$$$ and the maximum possible $$$k$$$, where $$$k$$$ is the maximum number of people sitting on one bench after additional $$$m$$$ people came to the park. Note In the first example, each of four benches is occupied by a single person. The minimum $$$k$$$ is $$$3$$$. For example, it is possible to achieve if two newcomers occupy the first bench, one occupies the second bench, one occupies the third bench, and two remaining — the fourth bench. The maximum $$$k$$$ is $$$7$$$. That requires all six new people to occupy the same bench. The second example has its minimum $$$k$$$ equal to $$$15$$$ and maximum $$$k$$$ equal to $$$15$$$, as there is just a single bench in the park and all $$$10$$$ people will occupy it. | 1,100 | false | false | true | false | false | false | false | true | false | false | 5,489 |
2008C | Today, Sakurako was studying arrays. An array $$$a$$$ of length $$$n$$$ is considered good if and only if: the array $$$a$$$ is increasing, meaning $$$a_{i - 1} < a_i$$$ for all $$$2 le i le n$$$; the differences between adjacent elements are increasing, meaning $$$a_i - a_{i-1} < a_{i+1} - a_i$$$ for all $$$2 le i < n$$$. Sakurako has come up with boundaries $$$l$$$ and $$$r$$$ and wants to construct a good array of maximum length, where $$$l le a_i le r$$$ for all $$$a_i$$$. Help Sakurako find the maximum length of a good array for the given $$$l$$$ and $$$r$$$. Input The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$) xa0— the number of test cases. The only line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1le lle rle 10^9$$$). Output For each test case, output a single integer xa0— the length of the longest good array Sakurako can form given $$$l$$$ and $$$r$$$. Example Input 5 1 2 1 5 2 2 10 20 1 1000000000 Note For $$$l=1$$$ and $$$r=5$$$, one possible array could be $$$(1,2,5)$$$. It can be proven that an array of length $$$4$$$ does not exist for the given $$$l$$$ and $$$r$$$. For $$$l=2$$$ and $$$r=2$$$, the only possible array is $$$(2)$$$. For $$$l=10$$$ and $$$r=20$$$, the only possible array is $$$(10,11,13,16,20)$$$. | 800 | true | false | false | false | false | false | true | true | false | false | 206 |
1903F | Theofanis wants to play video games, however he should also take care of his sister. Since Theofanis is a CS major, he found a way to do both. He will install some cameras in his house in order to make sure his sister is okay. His house is an undirected graph with $$$n$$$ nodes and $$$m$$$ edges. His sister likes to play at the edges of the graph, so he has to install a camera to at least one endpoint of every edge of the graph. Theofanis wants to find a out of the nodes that you chose. If $$$k = 1$$$ then we assume that the minimum difference between indices of the chosen nodes is $$$n$$$. Can you find the maximum possible minimum difference between indices of the chosen nodes over all vertex covers? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^{5}, 1 le m le 2 cdot 10^{5}$$$)xa0— the number of nodes and the number of edges. The $$$i$$$-th of the following $$$m$$$ lines in the test case contains two positive integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i,v_i le n$$$), meaning that there exists an edge between them in the graph. 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 $$$2 cdot 10^{5}$$$. Output For each test case, print the maximum minimum difference between indices of the chosen nodes over all vertex covers. Example Input 3 7 6 1 2 1 3 1 4 1 6 2 3 5 7 3 3 1 2 1 3 1 1 2 4 1 2 1 2 2 1 1 1 Note In the first test case, we can install cameras at nodes $$$1$$$, $$$3$$$, and $$$7$$$, so the answer is $$$2$$$. In the second test case, we can install only one camera at node $$$1$$$, so the answer is $$$3$$$. | 2,500 | false | false | false | false | true | false | false | true | false | true | 891 |
808B | It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, one week in Berland lasts _k_ days! When Polycarp went to a doctor with his problem, the doctor asked him about his sleeping schedule (more specifically, the average amount of hours of sleep per week). Luckily, Polycarp kept records of sleep times for the last _n_ days. So now he has a sequence _a_1,u2009_a_2,u2009...,u2009_a__n_, where _a__i_ is the sleep time on the _i_-th day. The number of records is so large that Polycarp is unable to calculate the average value by himself. Thus he is asking you to help him with the calculations. To get the average Polycarp is going to consider _k_ consecutive days as a week. So there will be _n_u2009-u2009_k_u2009+u20091 weeks to take into consideration. For example, if _k_u2009=u20092, _n_u2009=u20093 and _a_u2009=u2009[3,u20094,u20097], then the result is . You should write a program which will calculate average sleep times of Polycarp over all weeks. Input The first line contains two integer numbers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u20092·105). The second line contains _n_ integer numbers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009105). Output Output average sleeping time over all weeks. The answer is considered to be correct if its absolute or relative error does not exceed 10u2009-u20096. In particular, it is enough to output real number with at least 6 digits after the decimal point. Examples Input 8 2 1 2 4 100000 123 456 789 1 Note In the third example there are _n_u2009-u2009_k_u2009+u20091u2009=u20097 weeks, so the answer is sums of all weeks divided by 7. | 1,300 | true | false | true | false | true | false | false | false | false | false | 6,534 |
85A | We all know the problem about the number of ways one can tile a 2u2009×u2009_n_ field by 1u2009×u20092 dominoes. You probably remember that it goes down to Fibonacci numbers. We will talk about some other problem below, there you also are going to deal with tiling a rectangular field with dominoes. You are given a 4u2009×u2009_n_ rectangular field, that is the field that contains four lines and _n_ columns. You have to find for it any tiling by 1u2009×u20092 dominoes such that each of the _n_u2009-u20091 potential vertical cuts along the grid lines intersects at least one domino, splitting it in two. No two dominoes in the sought tiling should overlap, each square of the field should be covered by exactly one domino. It is allowed to rotate the dominoes, that is, you can use 2u2009×u20091 as well as 1u2009×u20092 dominoes. Write a program that finds an arbitrary sought tiling. Input The input contains one positive integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of the field's columns. Output If there's no solution, print "-1" (without the quotes). Otherwise, print four lines containing _n_ characters each — that's the description of tiling, where each vertical cut intersects at least one domino. You should print the tiling, having painted the field in no more than 26 colors. Each domino should be painted a color. Different dominoes can be painted the same color, but dominoes of the same color should not be side-neighbouring. To indicate colors you should use lowercase Latin letters. Print any of the acceptable ways of tiling. | 1,300 | false | false | true | false | false | true | false | false | false | false | 9,546 |
868F | Problem - 868F - 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 divide and conquer dp *2500 No tag edit access → Contest materials ) xa0— the length of the array and the number of segments you need to split the array into. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_n_)xa0— the elements of the array. Output Print single integer: the minimum possible total cost of resulting subsegments. Examples Input 7 3 1 1 3 3 3 2 1 Output 1 Input 10 2 1 2 1 2 1 2 1 2 1 2 Output 8 Input 13 3 1 2 2 2 1 2 1 1 1 2 2 1 1 Output 9 Note In the first example it's optimal to split the sequence into the following three subsegments: | 2,500 | false | false | false | true | false | false | false | false | false | false | 6,268 |
1242C | Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to reorder the numbers. There are $$$k$$$ boxes numbered from $$$1$$$ to $$$k$$$. The $$$i$$$-th box contains $$$n_i$$$ integer numbers. The integers can be negative. All of the integers are distinct. Ujan is lazy, so he will do the following reordering of the numbers exactly once. He will pick a single integer from each of the boxes, $$$k$$$ integers in total. Then he will insert the chosen numbersxa0— one integer in each of the boxes, so that the number of integers in each box is the same as in the beginning. Note that he may also insert an integer he picked from a box back into the same box. Ujan will be happy if the sum of the integers in each box is the same. Can he achieve this and make the boxes perfectly balanced, like all things should be? Input The first line contains a single integer $$$k$$$ ($$$1 leq k leq 15$$$), the number of boxes. The $$$i$$$-th of the next $$$k$$$ lines first contains a single integer $$$n_i$$$ ($$$1 leq n_i leq 5,000$$$), the number of integers in box $$$i$$$. Then the same line contains $$$n_i$$$ integers $$$a_{i,1}, ldots, a_{i,n_i}$$$ ($$$a_{i,j} leq 10^9$$$), the integers in the $$$i$$$-th box. It is guaranteed that all $$$a_{i,j}$$$ are distinct. Output If Ujan cannot achieve his goal, output "No" in a single line. Otherwise in the first line output "Yes", and then output $$$k$$$ lines. The $$$i$$$-th of these lines should contain two integers $$$c_i$$$ and $$$p_i$$$. This means that Ujan should pick the integer $$$c_i$$$ from the $$$i$$$-th box and place it in the $$$p_i$$$-th box afterwards. If there are multiple solutions, output any of those. You can print each letter in any case (upper or lower). Examples Input 4 3 1 7 4 2 3 2 2 8 5 1 10 Output Yes 7 2 2 3 5 1 10 4 Note In the first sample, Ujan can put the number $$$7$$$ in the $$$2$$$nd box, the number $$$2$$$ in the $$$3$$$rd box, the number $$$5$$$ in the $$$1$$$st box and keep the number $$$10$$$ in the same $$$4$$$th box. Then the boxes will contain numbers $$${1,5,4}$$$, $$${3, 7}$$$, $$${8,2}$$$ and $$${10}$$$. The sum in each box then is equal to $$$10$$$. In the second sample, it is not possible to pick and redistribute the numbers in the required way. In the third sample, one can swap the numbers $$$-20$$$ and $$$-10$$$, making the sum in each box equal to $$$-10$$$. | 2,400 | false | false | false | true | false | false | false | false | false | true | 4,487 |
1556C | William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers $$$c_1, c_2, dots, c_n$$$ where $$$c_i$$$ is the number of consecutive brackets "(" if $$$i$$$ is an odd number or the number of consecutive brackets ")" if $$$i$$$ is an even number. For example for a bracket sequence "((())()))" a corresponding sequence of numbers is $$$[3, 2, 1, 3]$$$. You need to find the total number of continuous subsequences (subsegments) $$$[l, r]$$$ ($$$l le r$$$) of the original bracket sequence, which are regular bracket sequences. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. Input The first line contains a single integer $$$n$$$ $$$(1 le n le 1000)$$$, the size of the compressed sequence. The second line contains a sequence of integers $$$c_1, c_2, dots, c_n$$$ $$$(1 le c_i le 10^9)$$$, the compressed sequence. Output Output a single integerxa0— the total number of subsegments of the original bracket sequence, which are regular bracket sequences. It can be proved that the answer fits in the signed 64-bit integer data type. Note In the first example a sequence (((()(()))( is described. This bracket sequence contains $$$5$$$ subsegments which form regular bracket sequences: 1. Subsequence from the $$$3$$$rd to $$$10$$$th character: (()(())) 2. Subsequence from the $$$4$$$th to $$$5$$$th character: () 3. Subsequence from the $$$4$$$th to $$$9$$$th character: ()(()) 4. Subsequence from the $$$6$$$th to $$$9$$$th character: (()) 5. Subsequence from the $$$7$$$th to $$$8$$$th character: () In the second example a sequence ()))(()(()))) is described. In the third example a sequence ()()(()) is described. | 1,800 | false | false | true | false | false | false | true | false | false | false | 2,853 |
339E | Xenia the horse breeder has _n_ (_n_u2009>u20091) horses that stand in a row. Each horse has its own unique number. Initially, the _i_-th left horse has number _i_. That is, the sequence of numbers of horses in a row looks as follows (from left to right): 1, 2, 3, ..., _n_. Xenia trains horses before the performance. During the practice sessions, she consistently gives them commands. Each command is a pair of numbers _l_,u2009_r_ (1u2009≤u2009_l_u2009<u2009_r_u2009≤u2009_n_). The command _l_,u2009_r_ means that the horses that are on the _l_-th, (_l_u2009+u20091)-th, (_l_u2009+u20092)-th, ..., _r_-th places from the left must be rearranged. The horses that initially stand on the _l_-th and _r_-th places will swap. The horses on the (_l_u2009+u20091)-th and (_r_u2009-u20091)-th places will swap. The horses on the (_l_u2009+u20092)-th and (_r_u2009-u20092)-th places will swap and so on. In other words, the horses that were on the segment [_l_,u2009_r_] change their order to the reverse one. For example, if Xenia commanded _l_u2009=u20092,u2009_r_u2009=u20095, and the sequence of numbers of horses before the command looked as (2, 1, 3, 4, 5, 6), then after the command the sequence will be (2, 5, 4, 3, 1, 6). We know that during the practice Xenia gave at most three commands of the described form. You have got the final sequence of numbers of horses by the end of the practice. Find what commands Xenia gave during the practice. Note that you do not need to minimize the number of commands in the solution, find any valid sequence of at most three commands. Input The first line contains an integer _n_ (2u2009≤u2009_n_u2009≤u20091000) — the number of horses in the row. The second line contains _n_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_n_), where _a__i_ is the number of the _i_-th left horse in the row after the practice. Output The first line should contain integer _k_ (0u2009≤u2009_k_u2009≤u20093) — the number of commads Xenia gave during the practice. In each of the next _k_ lines print two integers. In the _i_-th line print numbers _l__i_,u2009_r__i_ (1u2009≤u2009_l__i_u2009<u2009_r__i_u2009≤u2009_n_) — Xenia's _i_-th command during the practice. It is guaranteed that a solution exists. If there are several solutions, you are allowed to print any of them. | 2,700 | false | true | false | false | false | true | false | false | false | false | 8,476 |
97E | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology — to make the leaders work in pairs. A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple — we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task. Input The first line contains integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009105,u20090u2009≤u2009_m_u2009≤u2009105) — the number of vertices and edges in the relationship graph correspondingly. Next _m_ lines contain pairs of integers _a_ and _b_ which mean that there is an edge between the _a_-th and the _b_-th vertices (the vertices are numbered starting from 1, 1u2009≤u2009_a_,u2009_b_u2009≤u2009_n_). It is guaranteed that the graph has no loops or multiple edges. Next line contains number _q_ (1u2009≤u2009_q_u2009≤u2009105) — the number of pairs the scientists are interested in. Next _q_ lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices). Output For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No". Examples Input 7 7 1 3 1 4 2 3 2 4 5 6 6 7 7 5 8 1 2 1 3 1 4 2 4 1 5 5 6 5 7 6 7 Output No Yes Yes Yes No Yes Yes Yes Note Notes to the samples: 1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges. 2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3. 5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2,200 | false | false | false | false | false | false | false | false | false | true | 9,497 |
858F | All Berland residents are waiting for an unprecedented tour of wizard in his Blue Helicopter over the cities of Berland! It is well-known that there are _n_ cities in Berland, some pairs of which are connected by bidirectional roads. Each pair of cities is connected by no more than one road. It is not guaranteed that the road network is connected, i.e. it is possible that you can't reach some city from some other. The tour will contain several episodes. In each of the episodes: the wizard will disembark at some city _x_ from the Helicopter; he will give a performance and show a movie for free at the city _x_; he will drive to some neighboring city _y_ using a road; he will give a performance and show a movie for free at the city _y_; he will drive to some neighboring to _y_ city _z_; he will give a performance and show a movie for free at the city _z_; he will embark the Helicopter and fly away from the city _z_. It is known that the wizard doesn't like to use roads, so he agrees to use each road at most once (regardless of direction). In other words, for road between _a_ and _b_ he only can drive once from _a_ to _b_, or drive once from _b_ to _a_, or do not use this road at all. The wizards wants to plan as many episodes as possible without violation the above rules. Help the wizard! Please note that the wizard can visit the same city multiple times, the restriction is on roads only. Input The first line contains two integers _n_, _m_ (1u2009≤u2009_n_u2009≤u20092·105, 0u2009≤u2009_m_u2009≤u20092·105) — the number of cities and the number of roads in Berland, respectively. The roads description follow, one in each line. Each description is a pair of two integers _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_), where _a__i_ and _b__i_ are the ids of the cities connected by the _i_-th road. It is guaranteed that there are no two roads connecting the same pair of cities. Every road is bidirectional. The cities are numbered from 1 to _n_. It is possible that the road network in Berland is not connected. Output In the first line print _w_ — the maximum possible number of episodes. The next _w_ lines should contain the episodes in format _x_, _y_, _z_ — the three integers denoting the ids of the cities in the order of the wizard's visits. Examples Input 5 8 5 3 1 2 4 5 5 1 2 5 4 3 1 4 3 2 Output 4 1 4 5 2 3 4 1 5 3 5 2 1 | 2,300 | false | false | false | false | false | true | false | false | false | true | 6,309 |
930D | Consider the following game for two players. There is one white token and some number of black tokens. Each token is placed on a plane in a point with integer coordinates _x_ and _y_. The players take turn making moves, white starts. On each turn, a player moves all tokens of their color by 1 to up, down, left or right. Black player can choose directions for each token independently. After a turn of the white player the white token can not be in a point where a black token is located. There are no other constraints on locations of the tokens: positions of black tokens can coincide, after a turn of the black player and initially the white token can be in the same point with some black point. If at some moment the white player can't make a move, he loses. If the white player makes 10100500 moves, he wins. You are to solve the following problem. You are given initial positions of all black tokens. It is guaranteed that initially all these positions are distinct. In how many places can the white token be located initially so that if both players play optimally, the black player wins? Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of black points. The (_i_u2009+u20091)-th line contains two integers _x__i_, _y__i_ (u2009-u2009105u2009≤u2009_x__i_,u2009_y__i_,u2009u2009≤u2009105) — the coordinates of the point where the _i_-th black token is initially located. It is guaranteed that initial positions of black tokens are distinct. Output Print the number of points where the white token can be located initially, such that if both players play optimally, the black player wins. Examples Input 16 2 1 1 2 -1 1 0 1 0 0 1 1 2 -1 2 0 1 0 -1 -1 1 -1 2 2 0 -1 -1 0 0 2 -1 2 Note In the first and second examples initial positions of black tokens are shown with black points, possible positions of the white token (such that the black player wins) are shown with white points. The first example: The second example: In the third example the white tokens should be located in the inner square 2u2009×u20092, to make the black player win. | 2,500 | false | false | true | false | true | false | false | false | false | false | 5,983 |
1574E | A matrix of size $$$n imes m$$$, such that each cell of it contains either $$$0$$$ or $$$1$$$, is considered beautiful if the sum in every contiguous submatrix of size $$$2 imes 2$$$ is exactly $$$2$$$, i.u2009e. every "square" of size $$$2 imes 2$$$ contains exactly two $$$1$$$'s and exactly two $$$0$$$'s. You are given a matrix of size $$$n imes m$$$. Initially each cell of this matrix is empty. Let's denote the cell on the intersection of the $$$x$$$-th row and the $$$y$$$-th column as $$$(x, y)$$$. You have to process the queries of three types: $$$x$$$ $$$y$$$ $$$-1$$$ — clear the cell $$$(x, y)$$$, if there was a number in it; $$$x$$$ $$$y$$$ $$$0$$$ — write the number $$$0$$$ in the cell $$$(x, y)$$$, overwriting the number that was there previously (if any); $$$x$$$ $$$y$$$ $$$1$$$ — write the number $$$1$$$ in the cell $$$(x, y)$$$, overwriting the number that was there previously (if any). After each query, print the number of ways to fill the empty cells of the matrix so that the resulting matrix is beautiful. Since the answers can be large, print them modulo $$$998244353$$$. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 le n, m le 10^6$$$; $$$1 le k le 3 cdot 10^5$$$) — the number of rows in the matrix, the number of columns, and the number of queries, respectively. Then $$$k$$$ lines follow, the $$$i$$$-th of them contains three integers $$$x_i$$$, $$$y_i$$$, $$$t_i$$$ ($$$1 le x_i le n$$$; $$$1 le y_i le m$$$; $$$-1 le t_i le 1$$$) — the parameters for the $$$i$$$-th query. Output For each query, print one integer — the number of ways to fill the empty cells of the matrix after the respective query, taken modulo $$$998244353$$$. Example Input 2 2 7 1 1 1 1 2 1 2 1 1 1 1 0 1 2 -1 2 1 -1 1 1 -1 | 2,500 | true | false | true | false | false | true | false | false | false | false | 2,770 |
792A | There are _n_ cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers _a_1,u2009_a_2,u2009...,u2009_a__n_. All coordinates are pairwise distinct. It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates. It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance. Input The first line contains one integer number _n_ (2u2009≤u2009_n_u2009≤u20092·105). The second line contains _n_ integer numbers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009109u2009≤u2009_a__i_u2009≤u2009109). All numbers _a__i_ are pairwise distinct. Output Print two integer numbers — the minimal distance and the quantity of pairs with this distance. Note In the first example the distance between the first city and the fourth city is 4u2009-u20096u2009=u20092, and it is the only pair with this distance. | 1,100 | false | false | true | false | false | false | false | false | true | false | 6,613 |
2001D | You are given an integer sequence $$$a_1, a_2, ldots, a_n$$$. Let $$$S$$$ be the set of all possible non-empty subsequences of $$$a$$$ without duplicate elements. Your goal is to find the longest sequence in $$$S$$$. If there are multiple of them, find the one that minimizes lexicographical order after multiplying terms at odd positions by $$$-1$$$. For example, given $$$a = [3, 2, 3, 1]$$$, $$$S = {[1], [2], [3], [2, 1], [2, 3], [3, 1], [3, 2], [2, 3, 1], [3, 2, 1]}$$$. Then $$$[2, 3, 1]$$$ and $$$[3, 2, 1]$$$ would be the longest, and $$$[3, 2, 1]$$$ would be the answer since $$$[-3, 2, -1]$$$ is lexicographically smaller than $$$[-2, 3, -1]$$$. A sequence $$$c$$$ is a subsequence of a sequence $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by the deletion of several (possibly, zero or all) elements. A sequence $$$c$$$ is lexicographically smaller than a sequence $$$d$$$ if and only if one of the following holds: $$$c$$$ is a prefix of $$$d$$$, but $$$c e d$$$; in the first position where $$$c$$$ and $$$d$$$ differ, the sequence $$$c$$$ has a smaller element than the corresponding element in $$$d$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 5 cdot 10^4$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$) — the length of $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$) — the sequence $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output the answer in the following format: Output an integer $$$m$$$ in the first line — the length of $$$b$$$. Then output $$$m$$$ integers $$$b_1, b_2, ldots, b_m$$$ in the second line — the sequence $$$b$$$. Examples Input 4 4 3 2 1 3 4 1 1 1 1 9 3 2 1 3 2 1 3 2 1 1 1 Output 3 3 2 1 1 1 3 3 1 2 1 1 Input 10 2 1 2 10 5 2 1 7 9 7 2 5 5 2 2 1 2 10 2 2 8 7 7 9 8 1 9 6 9 9 1 7 5 8 5 6 4 1 3 3 3 3 6 1 6 4 4 6 5 6 3 4 4 5 3 3 10 4 1 4 5 4 5 10 1 5 1 7 1 2 1 3 2 4 6 Output 2 1 2 5 5 1 9 7 2 2 1 2 6 2 7 9 8 1 6 7 9 1 7 5 8 6 4 1 3 4 1 4 6 5 3 4 5 3 4 5 4 10 1 5 2 1 3 4 6 Note In the first example, $$$S = {[1], [2], [3], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2], [2, 1, 3], [3, 2, 1]}$$$. Among them, $$$[2, 1, 3]$$$ and $$$[3, 2, 1]$$$ are the longest and $$$[-3, 2, -1]$$$ is lexicographical smaller than $$$[-2, 1, -3]$$$, so $$$[3, 2, 1]$$$ is the answer. In the second example, $$$S = {[1]}$$$, so $$$[1]$$$ is the answer. | 1,900 | false | true | true | false | true | true | true | false | false | false | 252 |
1279B | New Year is coming! Vasya has prepared a New Year's verse and wants to recite it in front of Santa Claus. Vasya's verse contains $$$n$$$ parts. It takes $$$a_i$$$ seconds to recite the $$$i$$$-th part. Vasya can't change the order of parts in the verse: firstly he recites the part which takes $$$a_1$$$ seconds, secondly — the part which takes $$$a_2$$$ seconds, and so on. After reciting the verse, Vasya will get the number of presents equal to the number of parts he fully recited. Vasya can skip at most one part of the verse while reciting it (if he skips more than one part, then Santa will definitely notice it). Santa will listen to Vasya's verse for no more than $$$s$$$ seconds. For example, if $$$s = 10$$$, $$$a = [100, 9, 1, 1]$$$, and Vasya skips the first part of verse, then he gets two presents. Note that it is possible to recite the whole verse (if there is enough time). Determine which part Vasya needs to skip to obtain the maximum possible number of gifts. If Vasya shouldn't skip anything, print 0. If there are multiple answers, print any of them. You have to process $$$t$$$ test cases. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 le n le 10^5, 1 le s le 10^9$$$) — the number of parts in the verse and the maximum number of seconds Santa will listen to Vasya, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) — the time it takes to recite each part of the verse. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Note In the first test case if Vasya skips the second part then he gets three gifts. In the second test case no matter what part of the verse Vasya skips. In the third test case Vasya can recite the whole verse. | 1,300 | false | false | true | false | false | false | true | true | false | false | 4,299 |
160E | The main Bertown street is represented by a straight line. There are 109 bus stops located on the line. The stops are numbered with integers from 1 to 109 in the order in which they follow on the road. The city has _n_ buses. Every day the _i_-th bus drives from stop number _s__i_ to stop number _f__i_ (_s__i_u2009<u2009_f__i_), it stops on all intermediate stops and returns only at night. The bus starts driving at time _t__i_ and drives so fast that it finishes driving also at time _t__i_. The time _t__i_ is different for all buses. The buses have infinite capacity. Bertown has _m_ citizens. Today the _i_-th person should get from stop number _l__i_ to stop number _r__i_ (_l__i_u2009<u2009_r__i_); the _i_-th citizen comes to his initial stop (_l__i_) at time _b__i_. Each person, on the one hand, wants to get to the destination point as quickly as possible, and on the other hand, definitely does not want to change the buses as he rides. More formally: the _i_-th person chooses bus _j_, with minimum time _t__j_, such that _s__j_u2009≤u2009_l__i_, _r__i_u2009≤u2009_f__j_ and _b__i_u2009≤u2009_t__j_. Your task is to determine for each citizen whether he can ride to the destination point today and if he can, find the number of the bus on which the citizen will ride. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the number of buses and the number of people. Then _n_ lines follow, each of them contains three integers: _s__i_, _f__i_, _t__i_ (1u2009≤u2009_s__i_,u2009_f__i_,u2009_t__i_u2009≤u2009109,u2009_s__i_u2009<u2009_f__i_) — the description of the buses. It is guaranteed that all _t__i_-s are different. Then _m_ lines follow, each of them contains three integers: _l__i_, _r__i_, _b__i_ (1u2009≤u2009_l__i_,u2009_r__i_,u2009_b__i_u2009≤u2009109,u2009_l__i_u2009<u2009_r__i_) — the Bertown citizens' description. Some _b__i_-s could coincide. Output In the first line print _m_ space-separated integers: the _i_-th number should be equal either to -1, if the person number _i_ can't get to the destination point, or to the number of the bus that will ride the person number _i_. The buses are numbered with integers from 1 to _n_ in the input order. Examples Input 4 3 1 10 10 5 6 2 6 7 3 5 7 4 5 7 1 1 2 1 1 10 11 Input 1 1 1 1000000000 1000000000 1 1000000000 1000000000 | 2,400 | false | false | false | false | true | false | false | true | true | false | 9,231 |
1498F | Alice and Bob are going to celebrate Christmas by playing a game with a tree of presents. The tree has $$$n$$$ nodes (numbered $$$1$$$ to $$$n$$$, with some node $$$r$$$ as its root). There are $$$a_i$$$ presents are hanging from the $$$i$$$-th node. Before beginning the game, a special integer $$$k$$$ is chosen. The game proceeds as follows: Alice begins the game, with moves alternating each turn; in any move, the current player may choose some node (for example, $$$i$$$) which has depth at least $$$k$$$. Then, the player picks some positive number of presents hanging from that node, let's call it $$$m$$$ $$$(1 le m le a_i)$$$; the player then places these $$$m$$$ presents on the $$$k$$$-th ancestor (let's call it $$$j$$$) of the $$$i$$$-th node (the $$$k$$$-th ancestor of vertex $$$i$$$ is a vertex $$$j$$$ such that $$$i$$$ is a descendant of $$$j$$$, and the difference between the depth of $$$j$$$ and the depth of $$$i$$$ is exactly $$$k$$$). Now, the number of presents of the $$$i$$$-th node $$$(a_i)$$$ is decreased by $$$m$$$, and, correspondingly, $$$a_j$$$ is increased by $$$m$$$; Alice and Bob both play optimally. The player unable to make a move loses the game. For each possible root of the tree, find who among Alice or Bob wins the game. Note: The depth of a node $$$i$$$ in a tree with root $$$r$$$ is defined as the number of edges on the simple path from node $$$r$$$ to node $$$i$$$. The depth of root $$$r$$$ itself is zero. Input The first line contains two space-separated integers $$$n$$$ and $$$k$$$ $$$(3 le n le 10^5, 1 le k le 20)$$$. The next $$$n-1$$$ lines each contain two integers $$$x$$$ and $$$y$$$ $$$(1 le x, y le n, x eq y)$$$, denoting an undirected edge between the two nodes $$$x$$$ and $$$y$$$. These edges form a tree of $$$n$$$ nodes. The next line contains $$$n$$$ space-separated integers denoting the array $$$a$$$ $$$(0 le a_i le 10^9)$$$. Output Output $$$n$$$ integers, where the $$$i$$$-th integer is $$$1$$$ if Alice wins the game when the tree is rooted at node $$$i$$$, or $$$0$$$ otherwise. Example Input 5 1 1 2 1 3 5 2 4 3 0 3 2 4 4 Note Let us calculate the answer for sample input with root node as 1 and as 2. Root node 1 Alice always wins in this case. One possible gameplay between Alice and Bob is: Alice moves one present from node 4 to node 3. Bob moves four presents from node 5 to node 2. Alice moves four presents from node 2 to node 1. Bob moves three presents from node 2 to node 1. Alice moves three presents from node 3 to node 1. Bob moves three presents from node 4 to node 3. Alice moves three presents from node 3 to node 1. Bob is now unable to make a move and hence loses. Root node 2 Bob always wins in this case. One such gameplay is: Alice moves four presents from node 4 to node 3. Bob moves four presents from node 5 to node 2. Alice moves six presents from node 3 to node 1. Bob moves six presents from node 1 to node 2. Alice is now unable to make a move and hence loses. | 2,500 | true | false | false | true | true | false | false | false | false | false | 3,176 |
702D | Vasiliy has a car and he wants to get from home to the post office. The distance which he needs to pass equals to _d_ kilometers. Vasiliy's car is not new — it breaks after driven every _k_ kilometers and Vasiliy needs _t_ seconds to repair it. After repairing his car Vasiliy can drive again (but after _k_ kilometers it will break again, and so on). In the beginning of the trip the car is just from repair station. To drive one kilometer on car Vasiliy spends _a_ seconds, to walk one kilometer on foot he needs _b_ seconds (_a_u2009<u2009_b_). Your task is to find minimal time after which Vasiliy will be able to reach the post office. Consider that in every moment of time Vasiliy can left his car and start to go on foot. Input The first line contains 5 positive integers _d_,u2009_k_,u2009_a_,u2009_b_,u2009_t_ (1u2009≤u2009_d_u2009≤u20091012; 1u2009≤u2009_k_,u2009_a_,u2009_b_,u2009_t_u2009≤u2009106; _a_u2009<u2009_b_), where: _d_ — the distance from home to the post office; _k_ — the distance, which car is able to drive before breaking; _a_ — the time, which Vasiliy spends to drive 1 kilometer on his car; _b_ — the time, which Vasiliy spends to walk 1 kilometer on foot; _t_ — the time, which Vasiliy spends to repair his car. Output Print the minimal time after which Vasiliy will be able to reach the post office. Note In the first example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds) and then to walk on foot 3 kilometers (in 12 seconds). So the answer equals to 14 seconds. In the second example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds), then repair his car (in 5 seconds) and drive 2 kilometers more on the car (in 2 seconds). After that he needs to walk on foot 1 kilometer (in 4 seconds). So the answer equals to 13 seconds. | 1,900 | true | false | false | false | false | false | false | false | false | false | 7,014 |
303E | Imagine a real contest or exam of _n_ participants. Every participant will get a particular score. We can predict the standings board more or less, if we do some statistics on their previous performance. Let's say the score of the participants will be uniformly distributed in interval [_l__i_,u2009_r__i_] (the score can be a real number). Can you predict the standings board according to these data? In other words you should say for each participant the probability that he gets some fixed place in the scoreboard. The participants are sorted by increasing of their scores in the scoreboard. So, the participant with the largest score gets the last place. Input The first line contains integer _n_ (1u2009u2009≤u2009_n_u2009u2009≤u200980), showing how many participants we have. Each of the next _n_ lines contains our predictions, the _i_-th line contains a pair of integers _l__i_,u2009_r__i_ (0u2009≤u2009_l__i_u2009<u2009_r__i_u2009≤u2009109) as the distributed interval for participant _i_. Consider the participants numbered from 1 to _n_ in some way. Output Output a distributed matrix _a_ of order _n_. The element _a__ij_ of the matrix is the probability that participant _i_ has rank _j_. Your answer will considered correct if it has at most 10u2009-u20096 absolute or relative error. Examples Output 0.9200000000 0.080 0.080 0.9200000000 Input 8 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 Output 0.875 0.125 0 0 0 0 0 0 0.125 0.750 0.125 0 0 0 0 0 0 0.125 0.750 0.125 0 0 0 0 0 0 0.125 0.750 0.125 0 0 0 0 0 0 0.125 0.750 0.125 0 0 0 0 0 0 0.125 0.750 0.125 0 0 0 0 0 0 0.125 0.750 0.125 0 0 0 0 0 0 0.125 0.875 Note The score probability distribution is continuous, which means, there is no possibility for a draw. | 3,000 | true | false | false | true | false | false | false | false | false | false | 8,619 |
1877C | Chaneka, a gamer kid, invented a new gaming controller called joyboard. Interestingly, the joyboard she invented can only be used to play one game. The joyboard has a screen containing $$$n+1$$$ slots numbered from $$$1$$$ to $$$n+1$$$ from left to right. The $$$n+1$$$ slots are going to be filled with an array of non-negative integers $$$[a_1,a_2,a_3,ldots,a_{n+1}]$$$. Chaneka, as the player, must assign $$$a_{n+1}$$$ with an integer between $$$0$$$ and $$$m$$$ inclusive. Then, for each $$$i$$$ from $$$n$$$ to $$$1$$$, the value of $$$a_i$$$ will be equal to the remainder of dividing $$$a_{i+1}$$$ (the adjacent value to the right) by $$$i$$$. In other words, $$$a_i = a_{i + 1} bmod i$$$. Chaneka wants it such that after every slot is assigned with an integer, there are exactly $$$k$$$ distinct values in the entire screen (among all $$$n+1$$$ slots). How many valid ways are there for assigning a non-negative integer into slot $$$n+1$$$? Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 2cdot10^4$$$) — the number of test cases. The following lines contain the description of each test case. The only line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 leq n leq 10^9$$$; $$$0 leq m leq 10^9$$$; $$$1 leq k leq n+1$$$) — there are $$$n+1$$$ slots, the integer assigned in slot $$$n+1$$$ must not be bigger than $$$m$$$, and there should be exactly $$$k$$$ distinct values. Output For each test case, output a line containing an integer representing the number of valid ways for assigning a non-negative integer into slot $$$n+1$$$. Example Input 4 4 6 3 2 0 1 265 265 265 3 10 2 Note In the first test case, one of the $$$2$$$ possible ways for Chaneka is to choose $$$a_{n+1}=6$$$. If she does that, then: $$$a_4=a_5bmod 4=6bmod 4=2$$$ $$$a_3=a_4bmod 3=2bmod 3=2$$$ $$$a_2=a_3bmod 2=2bmod 2=0$$$ $$$a_1=a_2bmod 1=0bmod 1=0$$$ $$$a = [0, 0, 2, 2, 6]$$$ There are $$$3$$$ distinct values. In the second test case, the $$$1$$$ possible way for Chaneka is to choose $$$a_{n+1}=0$$$. If she does that, then $$$a = [0, 0, 0]$$$. There is only $$$1$$$ distinct value. In the third test case, there is no possible way for assigning a non-negative integer into slot $$$n+1$$$. | 1,200 | true | false | false | false | false | false | false | false | false | false | 1,018 |
1000D | The sequence of integers $$$a_1, a_2, dots, a_k$$$ is called a good array if $$$a_1 = k - 1$$$ and $$$a_1 > 0$$$. For example, the sequences $$$[3, -1, 44, 0], [1, -99]$$$ are good arrays, and the sequences $$$[3, 7, 8], [2, 5, 4, 1], [0]$$$ — are not. A sequence of integers is called good if it can be divided into a positive number of good arrays. Each good array should be a subsegment of sequence and each element of the sequence should belong to exactly one array. For example, the sequences $$$[2, -3, 0, 1, 4]$$$, $$$[1, 2, 3, -3, -9, 4]$$$ are good, and the sequences $$$[2, -3, 0, 1]$$$, $$$[1, 2, 3, -3 -9, 4, 1]$$$ — are not. For a given sequence of numbers, count the number of its subsequences that are good sequences, and print the number of such subsequences modulo 998244353. Input The first line contains the number $$$n~(1 le n le 10^3)$$$ — the length of the initial sequence. The following line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n~(-10^9 le a_i le 10^9)$$$ — the sequence itself. Note In the first test case, two good subsequences — $$$[a_1, a_2, a_3]$$$ and $$$[a_2, a_3]$$$. In the second test case, seven good subsequences — $$$[a_1, a_2, a_3, a_4], [a_1, a_2], [a_1, a_3], [a_1, a_4], [a_2, a_3], [a_2, a_4]$$$ and $$$[a_3, a_4]$$$. | 1,900 | false | false | false | true | false | false | false | false | false | false | 5,702 |
747E | A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: at first, the text of the comment is written; after that the number of comments is written, for which this comment is a parent comment (i.xa0e. the number of the replies to this comments); after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma. For example, if the comments look like: then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: at first, print a integer _d_xa0— the maximum depth of nesting comments; after that print _d_ lines, the _i_-th of them corresponds to nesting level _i_; for the _i_-th row print comments of nesting level _i_ in the order of their appearance in the Policarp's comments feed, separated by space. Input The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid. Examples Input hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 Output 3 hello test one ok bye two a b Input a,5,A,0,a,0,A,0,a,0,A,0 Input A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0 Output 4 A K M B F H L N O C D G I P E J | 1,700 | false | false | true | false | false | false | false | false | false | false | 6,806 |
917A | As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem. A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if _s_ is a correct bracket sequence, then (_s_) is also a correct bracket sequence. if _s_ and _t_ are correct bracket sequences, then _st_ (concatenation of _s_ and _t_) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence. Will gave his mom a string _s_ consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (_l_,u2009_r_) such that 1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_s_ and the string _s__l__s__l_u2009+u20091... _s__r_ is pretty, where _s__i_ is _i_-th character of _s_. Joyce doesn't know anything about bracket sequences, so she asked for your help. Input The first and only line of input contains string _s_, consisting only of characters '(', ')' and '?' (2u2009≤u2009_s_u2009≤u20095000). Output Print the answer to Will's puzzle in the first and only line of output. Note For the first sample testcase, the pretty substrings of _s_ are: 1. "(?" which can be transformed to "()". 2. "?)" which can be transformed to "()". 3. "((?)" which can be transformed to "(())". 4. "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of _s_ are: 1. "??" which can be transformed to "()". 2. "()". 3. "??()" which can be transformed to "()()". 4. "?()?" which can be transformed to "(())". 5. "??" which can be transformed to "()". 6. "()??" which can be transformed to "()()". 7. "??()??" which can be transformed to "()()()". | 1,800 | true | true | true | true | false | false | false | false | false | false | 6,063 |
804F | In Isart people don't die. There are _n_ gangs of criminals. The _i_-th gang contains _s__i_ evil people numerated from 0 to _s__i_u2009-u20091. Some of these people took part in a big mine robbery and picked one gold bullion each (these people are given in the input). That happened 10100 years ago and then all of the gangs escaped to a remote area, far from towns. During the years, they were copying some gold bullions according to an organized plan in order to not get arrested. They constructed a tournament directed graph (a graph where there is exactly one directed edge between every pair of vertices) of gangs (the graph is given in the input). In this graph an edge from _u_ to _v_ means that in the _i_-th hour the person of the gang _u_ can send a fake gold bullion to person of gang _v_. He sends it if he has some bullion (real or fake), while the receiver doesn't have any. Thus, at any moment each of the gangsters has zero or one gold bullion. Some of them have real bullions, and some of them have fake ones. In the beginning of this year, the police has finally found the gangs, but they couldn't catch them, as usual. The police decided to open a jewelry store so that the gangsters would sell the bullions. Thus, every gangster that has a bullion (fake or real) will try to sell it. If he has a real gold bullion, he sells it without problems, but if he has a fake one, there is a choice of two events that can happen: The person sells the gold bullion successfully. The person is arrested by police. The power of a gang is the number of people in it that successfully sold their bullion. After all selling is done, the police arrests _b_ gangs out of top gangs. Sort the gangs by powers, we call the first _a_ gang top gangs(you can sort the equal powers in each order). Consider all possible results of selling fake gold bullions and all possible choice of _b_ gangs among the top gangs. Count the number of different sets of these _b_ gangs modulo 109u2009+u20097. Two sets _X_ and _Y_ are considered different if some gang is in _X_ and isn't in _Y_. Input The first line contains four integers _n_, _a_ and _b_ (1u2009≤u2009_b_u2009≤u2009_a_u2009≤u2009_n_u2009≤u20095·103)xa0— the number of gangs, the constants _a_ and _b_ from the statement. Then _n_ lines follow, each line contains a string of size _n_ consisting of zeros and ones. The _j_-th character in the _i_-th of these lines is equal to 1, then the vertex _i_ have a directed edge to the vertex _j_. It is guaranteed that _a__ii_u2009=u20090 and _a__ij_u2009+u2009_a__ji_u2009=u20091 if _i_u2009≠u2009_j_. Then _n_ lines follow, each line starts with the integer _s__i_ (1u2009≤u2009_s__i_u2009≤u20092·106)xa0— the number of gangsters in the _i_-th gang, and then contains a string of zeros and ones with length _s__i_. The _j_-th character is 0 if the _j_-th person of the _i_-th gang had a real gold bullion initially, otherwise it is 1. It is guaranteed that the sum of _s__i_ does not exceed 2·106. | 3,400 | false | false | false | true | false | false | false | false | false | true | 6,540 |
1379F1 | Note that the difference between easy and hard versions is that in hard version unavailable cells can become available again and in easy version can't. You can make hacks only if all versions are solved. Ildar and Ivan are tired of chess, but they really like the chessboard, so they invented a new game. The field is a chessboard $$$2n imes 2m$$$: it has $$$2n$$$ rows, $$$2m$$$ columns, and the cell in row $$$i$$$ and column $$$j$$$ is colored white if $$$i+j$$$ is even, and is colored black otherwise. The game proceeds as follows: Ildar marks some of the white cells of the chessboard as unavailable, and asks Ivan to place $$$n imes m$$$ kings on the remaining white cells in such way, so that there are no kings attacking each other. A king can attack another king if they are located in the adjacent cells, sharing an edge or a corner. Ildar would like to explore different combinations of cells. Initially all cells are marked as available, and then he has $$$q$$$ queries. In each query he marks a cell as unavailable. After each query he would like to know whether it is possible to place the kings on the available cells in a desired way. Please help him! Input The first line of input contains three integers $$$n$$$, $$$m$$$, $$$q$$$ ($$$1 leq n, m, q leq 200,000$$$)xa0— the size of the board and the number of queries. $$$q$$$ lines follow, each of them contains a description of a query: two integers $$$i$$$ and $$$j$$$, denoting a white cell $$$(i, j)$$$ on the board ($$$1 leq i leq 2n$$$, $$$1 leq j leq 2m$$$, $$$i + j$$$ is even) that becomes unavailable. It's guaranteed, that each cell $$$(i, j)$$$ appears in input at most once. Output Output $$$q$$$ lines, $$$i$$$-th line should contain answer for a board after $$$i$$$ queries of Ildar. This line should contain "YES" if it is possible to place the kings on the available cells in the desired way, or "NO" otherwise. Examples Input 3 2 7 4 2 6 4 1 3 2 2 2 4 4 4 3 1 Output YES YES NO NO NO NO NO Note In the first example case after the second query only cells $$$(1, 1)$$$ and $$$(1, 5)$$$ are unavailable. Then Ivan can place three kings on cells $$$(2, 2)$$$, $$$(2, 4)$$$ and $$$(2, 6)$$$. After the third query three cells $$$(1, 1)$$$, $$$(1, 5)$$$ and $$$(2, 4)$$$ are unavailable, so there remain only 3 available cells: $$$(2, 2)$$$, $$$(1, 3)$$$ and $$$(2, 6)$$$. Ivan can not put 3 kings on those cells, because kings on cells $$$(2, 2)$$$ and $$$(1, 3)$$$ attack each other, since these cells share a corner. | 2,700 | false | false | false | false | true | false | false | true | false | false | 3,773 |
8D | Two neighbours, Alan and Bob, live in the city, where there are three buildings only: a cinema, a shop and the house, where they live. The rest is a big asphalt square. Once they went to the cinema, and the film impressed them so deeply, that when they left the cinema, they did not want to stop discussing it. Bob wants to get home, but Alan has to go to the shop first, and only then go home. So, they agreed to cover some distance together discussing the film (their common path might pass through the shop, or they might walk circles around the cinema together), and then to part each other's company and go each his own way. After they part, they will start thinking about their daily pursuits; and even if they meet again, they won't be able to go on with the discussion. Thus, Bob's path will be a continuous curve, having the cinema and the house as its ends. Alan's path — a continuous curve, going through the shop, and having the cinema and the house as its ends. The film ended late, that's why the whole distance covered by Alan should not differ from the shortest one by more than _t_1, and the distance covered by Bob should not differ from the shortest one by more than _t_2. Find the maximum distance that Alan and Bob will cover together, discussing the film. Input The first line contains two integers: _t_1,u2009_t_2 (0u2009≤u2009_t_1,u2009_t_2u2009≤u2009100). The second line contains the cinema's coordinates, the third one — the house's, and the last line — the shop's. All the coordinates are given in meters, are integer, and do not exceed 100 in absolute magnitude. No two given places are in the same building. | 2,600 | false | false | false | false | false | false | false | true | false | false | 9,952 |
1420C1 | This is the easy version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist. First, Andrew counted all the pokémonxa0— there were exactly $$$n$$$ pikachu. The strength of the $$$i$$$-th pokémon is equal to $$$a_i$$$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $$$b$$$ from $$$k$$$ indices such that $$$1 le b_1 < b_2 < dots < b_k le n$$$, and his army will consist of pokémons with forces $$$a_{b_1}, a_{b_2}, dots, a_{b_k}$$$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $$$a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + dots$$$. Andrew is experimenting with pokémon order. He performs $$$q$$$ operations. In $$$i$$$-th operation Andrew swaps $$$l_i$$$-th and $$$r_i$$$-th pokémon. Note: $$$q=0$$$ in this version of the task. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokémon, or team R will realize their tricky plan! Input Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 le t le 10^3$$$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n le 3 cdot 10^5, q = 0$$$) denoting the number of pokémon and number of operations respectively. The second line contains $$$n$$$ distinct positive integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$) denoting the strengths of the pokémon. $$$i$$$-th of the last $$$q$$$ lines contains two positive integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$) denoting the indices of pokémon that were swapped in the $$$i$$$-th operation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$, and the sum of $$$q$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. | 1,300 | false | true | false | true | false | true | false | false | false | false | 3,578 |
1093E | You are given two permutations $$$a$$$ and $$$b$$$, both consisting of $$$n$$$ elements. Permutation of $$$n$$$ elements is such a integer sequence that each value from $$$1$$$ to $$$n$$$ appears exactly once in it. You are asked to perform two types of queries with them: $$$1~l_a~r_a~l_b~r_b$$$ — calculate the number of values which appear in both segment $$$[l_a; r_a]$$$ of positions in permutation $$$a$$$ and segment $$$[l_b; r_b]$$$ of positions in permutation $$$b$$$; $$$2~x~y$$$ — swap values on positions $$$x$$$ and $$$y$$$ in permutation $$$b$$$. Print the answer for each query of the first type. It is guaranteed that there will be at least one query of the first type in the input. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le m le 2 cdot 10^5$$$) — the number of elements in both permutations and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$) — permutation $$$a$$$. It is guaranteed that each value from $$$1$$$ to $$$n$$$ appears in $$$a$$$ exactly once. The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le n$$$) — permutation $$$b$$$. It is guaranteed that each value from $$$1$$$ to $$$n$$$ appears in $$$b$$$ exactly once. Each of the next $$$m$$$ lines contains the description of a certain query. These are either: $$$1~l_a~r_a~l_b~r_b$$$ ($$$1 le l_a le r_a le n$$$, $$$1 le l_b le r_b le n$$$); $$$2~x~y$$$ ($$$1 le x, y le n$$$, $$$x e y$$$). Output Print the answers for the queries of the first type, each answer in the new line — the number of values which appear in both segment $$$[l_a; r_a]$$$ of positions in permutation $$$a$$$ and segment $$$[l_b; r_b]$$$ of positions in permutation $$$b$$$. Example Input 6 7 5 1 4 2 3 6 2 5 3 1 4 6 1 1 2 4 5 2 2 4 1 1 2 4 5 1 2 3 3 5 1 1 6 1 2 2 4 1 1 4 4 1 3 Note Consider the first query of the first example. Values on positions $$$[1; 2]$$$ of $$$a$$$ are $$$[5, 1]$$$ and values on positions $$$[4; 5]$$$ of $$$b$$$ are $$$[1, 4]$$$. Only value $$$1$$$ appears in both segments. After the first swap (the second query) permutation $$$b$$$ becomes $$$[2, 1, 3, 5, 4, 6]$$$. After the second swap (the sixth query) permutation $$$b$$$ becomes $$$[5, 1, 3, 2, 4, 6]$$$. | 2,400 | false | false | false | false | true | false | false | false | false | false | 5,240 |
1615G | Problem - 1615G - Codeforces =============== xa0 ]( "Codeforces Global Round 18") . The value of the array you obtain is the number of integers $$$k$$$ from $$$1$$$ to $$$n$$$ such that the following condition holds: there exist a pair of adjacent elements equal to $$$k$$$ (i.u2009e. there exists some $$$i in | 3,300 | false | false | false | false | false | true | false | false | false | false | 2,558 |
510A | Problem - 510A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *800 No tag edit access → Contest materials . The tail of the snake is located at (1,u20091), then it's body extends to (1,u2009_m_), then goes down 2 rows to (3,u2009_m_), then goes left to (3,u20091) and so on. Your task is to draw this snake for Fox Ciel: the empty cells should be represented as dot characters ('.') and the snake cells should be filled with number signs ('#'). Consider sample tests in order to understand the snake pattern. Input The only line contains two integers: _n_ and _m_ (3u2009≤u2009_n_,u2009_m_u2009≤u200950). _n_ is an odd number. Output Output _n_ lines. Each line should contain a string consisting of _m_ characters. Do not output spaces. Examples Input 3 3 Output ### ..# ### Input 3 4 Output #### ...# #### Input 5 3 Output ### ..# ### #.. ### Input 9 9 Output ######### ........# ######### #........ ######### ........# ######### #........ ######### | 800 | false | false | true | false | false | false | false | false | false | false | 7,800 |
1285B | Today, Yasser and Adel are at the shop buying cupcakes. There are $$$n$$$ cupcake types, arranged from $$$1$$$ to $$$n$$$ on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type $$$i$$$ is an integer $$$a_i$$$. There are both tasty and nasty cupcakes, so the tastiness can be positive, zero or negative. Yasser, of course, wants to try them all, so he will buy exactly one cupcake of each type. On the other hand, Adel will choose some segment $$$[l, r]$$$ $$$(1 le l le r le n)$$$ that does not include all of cupcakes (he can't choose $$$[l, r] = [1, n]$$$) and buy exactly one cupcake of each of types $$$l, l + 1, dots, r$$$. After that they will compare the total tastiness of the cupcakes each of them have bought. Yasser will be happy if the total tastiness of cupcakes he buys is strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel's choice. For example, let the tastinesses of the cupcakes be $$$[7, 4, -1]$$$. Yasser will buy all of them, the total tastiness will be $$$7 + 4 - 1 = 10$$$. Adel can choose segments $$$[7], [4], [-1], [7, 4]$$$ or $$$[4, -1]$$$, their total tastinesses are $$$7, 4, -1, 11$$$ and $$$3$$$, respectively. Adel can choose segment with tastiness $$$11$$$, and as $$$10$$$ is not strictly greater than $$$11$$$, Yasser won't be happy :( Find out if Yasser will be happy after visiting the shop. 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$$$ ($$$2 le n le 10^5$$$). 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$$$), where $$$a_i$$$ represents the tastiness of the $$$i$$$-th type of cupcake. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Output For each test case, print "YES", if the total tastiness of cupcakes Yasser buys will always be strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel's choice. Otherwise, print "NO". Note In the first example, the total tastiness of any segment Adel can choose is less than the total tastiness of all cupcakes. In the second example, Adel will choose the segment $$$[1, 2]$$$ with total tastiness $$$11$$$, which is not less than the total tastiness of all cupcakes, which is $$$10$$$. In the third example, Adel can choose the segment $$$[3, 3]$$$ with total tastiness of $$$5$$$. Note that Yasser's cupcakes' total tastiness is also $$$5$$$, so in that case, the total tastiness of Yasser's cupcakes isn't strictly greater than the total tastiness of Adel's cupcakes. | 1,300 | false | true | true | true | false | false | false | false | false | false | 4,266 |
2020C | Problem - 2020C - Codeforces =============== xa0 and Divide By Zero 9.0]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags bitmasks hashing implementation math schedules ternary search *1400 No tag edit access → Contest materials and Divide By Zero 9.0") and Divide By Zero 9.0") -(a, &, c)=d$$$, where $$$$$$ and $$$&$$$ denote the . The description of the test cases follows. The only line of each test case contains three positive integers $$$b$$$, $$$c$$$, and $$$d$$$ ($$$0 le b, c, d le 10^{18}$$$). Output For each test case, output the value of $$$a$$$, or $$$-1$$$ if there is no solution. Please note that $$$a$$$ must be non-negative and cannot exceed $$$2^{61}$$$. Example Input 3 2 2 2 4 2 6 10 2 14 Output 0 -1 12 Note In the first test case, $$$(0,,2)-(0,&,2)=2-0=2$$$. So, $$$a = 0$$$ is a correct answer. In the second test case, no value of $$$a$$$ satisfies the equation. In the third test case, $$$(12,,10)-(12,&,2)=14-0=14$$$. So, $$$a = 12$$$ is a correct answer. | 1,400 | true | false | true | false | false | false | false | false | false | false | 150 |
651A | Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at _a_1 percent and second one is charged at _a_2 percent. You can connect charger to a joystick only at the beginning of each minute. In one minute joystick either discharges by 2 percent (if not connected to a charger) or charges by 1 percent (if connected to a charger). Game continues while both joysticks have a positive charge. Hence, if at the beginning of minute some joystick is charged by 1 percent, it has to be connected to a charger, otherwise the game stops. If some joystick completely discharges (its charge turns to 0), the game also stops. Determine the maximum number of minutes that game can last. It is prohibited to pause the game, i. e. at each moment both joysticks should be enabled. It is allowed for joystick to be charged by more than 100 percent. Input The first line of the input contains two positive integers _a_1 and _a_2 (1u2009≤u2009_a_1,u2009_a_2u2009≤u2009100), the initial charge level of first and second joystick respectively. Output Output the only integer, the maximum number of minutes that the game can last. Game continues until some joystick is discharged. Note In the first sample game lasts for 6 minute by using the following algorithm: at the beginning of the first minute connect first joystick to the charger, by the end of this minute first joystick is at 4%, second is at 3%; continue the game without changing charger, by the end of the second minute the first joystick is at 5%, second is at 1%; at the beginning of the third minute connect second joystick to the charger, after this minute the first joystick is at 3%, the second one is at 2%; continue the game without changing charger, by the end of the fourth minute first joystick is at 1%, second one is at 3%; at the beginning of the fifth minute connect first joystick to the charger, after this minute the first joystick is at 2%, the second one is at 1%; at the beginning of the sixth minute connect second joystick to the charger, after this minute the first joystick is at 0%, the second one is at 2%. After that the first joystick is completely discharged and the game is stopped. | 1,100 | true | true | true | true | false | false | false | false | false | false | 7,212 |
1634E | Even a cat has things it can do that AI cannot. — Fei-Fei Li You are given $$$m$$$ arrays of positive integers. Each array is of even length. You need to split all these integers into two equal multisets $$$L$$$ and $$$R$$$, that is, each element of each array should go into one of two multisets (but not both). Additionally, for each of the $$$m$$$ arrays, exactly half of its elements should go into $$$L$$$, and the rest should go into $$$R$$$. Give an example of such a division or determine that no such division exists. Input The first line contains an integer $$$m$$$ ($$$1 le m le 10 ^ 5$$$)xa0— the number of arrays. The next $$$2 cdot m$$$ lines contain descriptions of the arrays. For each array, the first line contains an even integer $$$n$$$ ($$$2 le n le 2 cdot 10 ^ 5$$$)xa0— the length of the array. The second line consists of $$$n$$$ space-separated integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10 ^ 9$$$)xa0— array elements. It is guaranteed that the sum of $$$n$$$ over all arrays does not exceed $$$2 cdot 10^5$$$. Output If the answer exists, print "YES", and then print $$$m$$$ lines. On each line, for each element, print the letter "L" or "R" (capitalized, without spaces), depending on which multiset the element should go into. If there is no answer, print "NO" on the only line. Example Input 3 2 1 2 4 1 2 3 3 6 1 1 2 2 3 3 Note In the first array, we add the first element to $$$R$$$ and the second to $$$L$$$. Now $$$L = {2}$$$, and $$$R = {1}$$$. In the second array, we add the first and third elements to $$$L$$$ and the rest to $$$R$$$. Now $$$L = {1, 2, 3}$$$ and $$$R = {1, 2, 3}$$$. In the third array, we add elements 2, 3, and 6 to $$$L$$$, and othersxa0— to $$$R$$$. As a result, $$$L = R = {1, 1, 2, 2, 3, 3}$$$. | 2,400 | false | false | false | false | true | true | false | false | false | true | 2,442 |
462A | Problem - 462A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force implementation *1000 No tag edit access → Contest materials . Then _n_ lines follow containing the description of the checkerboard. Each of them contains _n_ characters (either 'x' or 'o') without spaces. Output Print "YES" or "NO" (without the quotes) depending on the answer to the problem. Examples Input 3 xxo xox oxx Output YES Input 4 xxxo xoxo oxox xxxx Output NO | 1,000 | false | false | true | false | false | false | true | false | false | false | 7,991 |
1368C | # Even Picture Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 512 megabytes Leo Jr. draws pictures in his notebook with checkered sheets (that is, each sheet has a regular square grid printed on it). We can assume that the sheets are infinitely large in any direction. To draw a picture, Leo Jr. colors some of the cells on a sheet gray. He considers the resulting picture beautiful if the following conditions are satisfied: • The picture is connected , that is, it is possible to get from any gray cell to any other by following a chain of gray cells, with each pair of adjacent cells in the path being neighbours (that is, sharing a side). • Each gray cell has an even number of gray neighbours . • There are exactly n gray cells with all gray neighbours . The number of other gray cells can be arbitrary (but reasonable, so that they can all be listed) .Leo Jr. is now struggling to draw a beautiful picture with a particular choice of n. Help him, and provide any example of a beautiful picture. To output cell coordinates in your answer, assume that the sheet is provided with a Cartesian coordinate system such that one of the cells is chosen to be the origin (0 , 0) , axes 0x and 0y are orthogonal and parallel to grid lines, and a unit step along any axis in any direction takes you to a neighbouring cell. # Input The only line contains a single integer n (1 ≤ n ≤ 500 ) x16 the number of gray cells with all gray neighbours in a beautiful picture. # Output In the first line, print a single integer k x16 the number of gray cells in your picture. For technical reasons, k should not exceed 5 · 10 5.Each of the following k lines should contain two integers x16 coordinates of a gray cell in your picture. All listed cells should be distinct, and the picture should satisdfy all the properties listed above. All coordinates should not exceed 10 9 by absolute value. One can show that there exists an answer satisfying all requirements with a small enough k. Page 1 of 2 Example standard input standard output 4 12 1 0 2 0 0 1 1 1 2 1 3 1 0 2 1 2 2 2 3 2 1 3 2 3 # Note The answer for the sample is pictured below: Page 2 of 2 | 1,500 | false | false | false | false | false | true | false | false | false | false | 3,834 |
1740I | In the world of Compfestnesia, Pak Chanek discovers a secret underground dungeon. Inside it, there is a treasure chest that is surrounded by $$$n$$$ statues that are arranged in a circular manner. The statues are numbered from $$$0$$$ to $$$n-1$$$ with statue $$$i$$$ being to the left of statue $$$i+1$$$ and statue $$$n-1$$$ being to the left of statue $$$0$$$. Pak Chanek observes that each statue is holding a crystal ball with an integer between $$$0$$$ and $$$m-1$$$ inclusive. Let's say the integer in the crystal ball of statue $$$i$$$ is $$$a_i$$$. The dungeon provides instructions that every integer in the crystal balls must be $$$0$$$ in order to open the treasure chest. To achieve that, Pak Chanek is given an integer $$$k$$$, and he can do zero or more operations. In a single operation, Pak Chanek does the following: 1. Choose exactly $$$k$$$ consecutive statues. In other words, choose the statues $$$p, (p+1) bmod n, (p+2) bmod n, (p+3) bmod n, ldots, (p+k-1) bmod n$$$ for some chosen index $$$p$$$. 2. Do one of the following: For all chosen statues, change their values of $$$a_i$$$ into $$$(a_i+1) bmod m$$$. For all chosen statues, change their values of $$$a_i$$$ into $$$(a_i-1) bmod m$$$. Help Pak Chanek find the minimum possible number of operations to open the treasure chest. Input The first line contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$2 leq n,m leq 10^6$$$, $$$nm leq 2 cdot 10^6$$$, $$$1 leq k < n$$$) — the number of statues, the bound of the integers in the crystal balls, and the number of statues that can be operated in a single operation. The second line contains $$$n$$$ integers $$$a_0,a_1,ldots,a_{n-1}$$$ ($$$0 leq a_i < m$$$) — the integers in the crystal balls. Output If it is possible to perform zero or more operations so that $$$a_0=a_1=ldots=a_{n-1}=0$$$, output the minimum number of operations required. Otherwise, output $$$-1$$$. Note In the first example, Pak Chanek can do the following operations: 1. Do the $$$a_i := (a_i-1) bmod m$$$ operation $$$3$$$ times for statues $$$1$$$, $$$2$$$, and $$$3$$$. Now $$$a=[8,7,1,2,0]$$$. 2. Do the $$$a_i := (a_i-1) bmod m$$$ operation $$$1$$$ time for statues $$$3$$$, $$$4$$$, and $$$0$$$. Now $$$a=[7,7,1,1,8]$$$. 3. Do the $$$a_i := (a_i+1) bmod m$$$ operation $$$2$$$ times for statues $$$4$$$, $$$0$$$, and $$$1$$$. Now $$$a=[0,0,1,1,1]$$$. 4. Do the $$$a_i := (a_i-1) bmod m$$$ operation $$$1$$$ time for statues $$$2$$$, $$$3$$$, and $$$4$$$. Now $$$a=[0,0,0,0,0]$$$. | 3,500 | false | false | false | true | true | false | false | false | false | true | 1,839 |
750F | This is an interactive problem. In the interaction section below you will find the information about flushing the output. The New Year tree of height _h_ is a perfect binary tree with vertices numbered 1 through 2_h_u2009-u20091 in some order. In this problem we assume that _h_ is at least 2. The drawing below shows one example New Year tree of height 3: Polar bears love decorating the New Year tree and Limak is no exception. To decorate the tree, he must first find its root, i.e. a vertex with exactly two neighbours (assuming that _h_u2009≥u20092). It won't be easy because Limak is a little bear and he doesn't even see the whole tree. Can you help him? There are _t_ testcases. In each testcase, you should first read _h_ from the input. Then you can ask at most 16 questions of format "? x" (without quotes), where _x_ is an integer between 1 and 2_h_u2009-u20091, inclusive. As a reply you will get the list of neighbours of vertex _x_ (more details in the "Interaction" section below). For example, for a tree on the drawing above after asking "? 1" you would get a response with 3 neighbours: 4, 5 and 7. Your goal is to find the index of the root _y_ and print it in the format "! y". You will be able to read _h_ for a next testcase only after printing the answer in a previous testcase and flushing the output. Each tree is fixed from the beginning and it doesn't change during your questions. Input The first line of the input contains a single integer _t_ (1u2009≤u2009_t_u2009≤u2009500)xa0— the number of testcases. At the beginning of each testcase you should read from the input a single integer _h_ (2u2009≤u2009_h_u2009≤u20097)xa0— the height of the tree. You can't read the value of _h_ in a next testcase until you answer a previous testcase. Interaction To ask a question about neighbours of vertex _x_, print "? x" (without quotes) on a separate line. Note, you must print an end-of-line character after the last character of the line and flush your output to get a response. The response will consist of two lines. The first line will contain a single integer _k_ (1u2009≤u2009_k_u2009≤u20093)xa0— the number of neighbours of vertex _x_. The second line will contain _k_ distinct integers _t_1,u2009...,u2009_t__k_ (1u2009≤u2009_t_1u2009<u2009...u2009<u2009_t__k_u2009≤u20092_h_u2009-u20091)xa0— indices of neighbours of vertex _x_, gives in the increasing order. After asking at most 16 questions you have to say _y_xa0— the index of the root. Print "! y" (without quotes) and an end-of-line character, and flush the output. Each tree is fixed from the beginning and it doesn't change during your questions. You can get Idleness Limit Exceeded if you don't print anything or if you forget to flush the output. To flush you can use (just printing a query/answer 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. In any moment if the program reads _h_u2009=u20090 or _k_u2009=u20090 it should immediately terminate normally (for example, calling exit(0)). It means that the system detected incorrect request/output from your program and printed 0 because if can't process your requests anymore. In this case you'll receive verdict "Wrong Answer", but if you ignore case _h_u2009=u20090 or _k_u2009=u20090 it could lead to "Runtime Error", "Time/Memory limit exceeded" or any other verdict because your program could read a trash from the closed input stream. Hacking. To hack someone, use the following format: The first line should contain a single integer _t_ equal to 1 (only one testcase is allowed in hacks). The second line should contain a single integer _h_. Each of next 2_h_u2009-u20092 lines should contain two distinct integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u20092_h_u2009-u20091), denoting two nodes connected with an edge. The printed edges must form a perfect binary tree of height _h_. Of course, contestant programs will not be able to see this input. Note In the first sample, a tree corresponds to the drawing from the statement. In the second sample, there are two two testcases. A tree in the first testcase has height 2 and thus 3 vertices. A tree in the second testcase has height 4 and thus 15 vertices. You can see both trees on the drawing below. | 2,800 | false | false | true | false | false | true | false | false | false | false | 6,788 |
2025F | You are given an array $$$a$$$, consisting of $$$n$$$ integers (numbered from $$$1$$$ to $$$n$$$). Initially, they are all zeroes. You have to process $$$q$$$ queries. The $$$i$$$-th query consists of two different integers $$$x_i$$$ and $$$y_i$$$. During the $$$i$$$-th query, you have to choose an integer $$$p$$$ (which is either $$$x_i$$$ or $$$y_i$$$) and an integer $$$d$$$ (which is either $$$1$$$ or $$$-1$$$), and assign $$$a_p = a_p + d$$$. After each query, every element of $$$a$$$ should be a non-negative integer. Process all queries in such a way that the sum of all elements of $$$a$$$ after the last query is the minimum possible. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 3 cdot 10^5$$$; $$$1 le q le 3 cdot 10^5$$$) — the number of elements in $$$a$$$ and the number of queries, respectively. Then $$$q$$$ lines follow. The $$$i$$$-th of these lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$; $$$x_i e y_i$$$) — the description of the $$$i$$$-th query. Output For each query, print a line containing two characters: the first character should be x if you choose $$$p=x_i$$$, or y if you choose $$$p=y_i$$$; the second character should be + if you choose $$$d=1$$$, or - if you choose $$$d=-1$$$. If there are multiple answers, print any of them. | 2,700 | false | true | false | true | false | true | false | false | false | true | 118 |
300E | Problem - 300E - 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 math number theory *2300 No tag edit access → Contest materials ") ") , . After many years of war the Empire's resources are low. So to reduce the costs, _n_ should be a minimum positive integer that is approved by the commanders. Help the Empire, find the minimum positive integer _n_, where the described fraction is a positive integer. Input The first line contains integer _k_ (1u2009≤u2009_k_u2009≤u2009106). The second line contains _k_ integers _a_1,u2009_a_2,u2009...,u2009_a__k_ (1u2009≤u2009_a__i_u2009≤u2009107). Output Print the minimum positive integer _n_, needed for the Empire to win. Please, do not use the %lld to read or write 64-but integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 2 1000 1000 Output 2000 Input 1 2 Output 2 | 2,300 | true | false | false | false | false | false | false | true | false | false | 8,631 |
1148E | There are $$$n$$$ stones arranged on an axis. Initially the $$$i$$$-th stone is located at the coordinate $$$s_i$$$. There may be more than one stone in a single place. You can perform zero or more operations of the following type: take two stones with indices $$$i$$$ and $$$j$$$ so that $$$s_i leq s_j$$$, choose an integer $$$d$$$ ($$$0 leq 2 cdot d leq s_j - s_i$$$), and replace the coordinate $$$s_i$$$ with $$$(s_i + d)$$$ and replace coordinate $$$s_j$$$ with $$$(s_j - d)$$$. In other words, draw stones closer to each other. You want to move the stones so that they are located at positions $$$t_1, t_2, ldots, t_n$$$. The order of the stones is not importantxa0— you just want for the multiset of the stones resulting positions to be the same as the multiset of $$$t_1, t_2, ldots, t_n$$$. Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don't need to minimize the number of moves. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0– the number of stones. The second line contains integers $$$s_1, s_2, ldots, s_n$$$ ($$$1 le s_i le 10^9$$$)xa0— the initial positions of the stones. The second line contains integers $$$t_1, t_2, ldots, t_n$$$ ($$$1 le t_i le 10^9$$$)xa0— the target positions of the stones. Output If it is impossible to move the stones this way, print "NO". Otherwise, on the first line print "YES", on the second line print the number of operations $$$m$$$ ($$$0 le m le 5 cdot n$$$) required. You don't have to minimize the number of operations. Then print $$$m$$$ lines, each containing integers $$$i, j, d$$$ ($$$1 le i, j le n$$$, $$$s_i le s_j$$$, $$$0 leq 2 cdot d leq s_j - s_i$$$), defining the operations. One can show that if an answer exists, there is an answer requiring no more than $$$5 cdot n$$$ operations. Examples Input 5 2 2 7 4 9 5 4 5 5 5 Output YES 4 4 3 1 2 3 1 2 5 2 1 5 2 Note Consider the first example. After the first move the locations of stones is $$$[2, 2, 6, 5, 9]$$$. After the second move the locations of stones is $$$[2, 3, 5, 5, 9]$$$. After the third move the locations of stones is $$$[2, 5, 5, 5, 7]$$$. After the last move the locations of stones is $$$[4, 5, 5, 5, 5]$$$. | 2,300 | true | true | false | false | false | true | false | false | true | false | 4,973 |
1958H | Monocarp plays a fantasy RPG. His character is a mage, so he casts spells. There are two types of spells he knows — basic spells and composite spells. There are $$$n$$$ basic spells in Monocarp's spell book, numbered from $$$1$$$ to $$$n$$$. Each basic spell simply changes the health of the target: either decreases it or increases it. The $$$i$$$-th basic spell changes the target's health value by $$$b_i$$$ (increases by $$$b_i$$$ if $$$b_i$$$ is non-negative, or decreases by $$$b_i$$$ if $$$b_i$$$ is negative). If the target's health value goes to $$$0$$$ or below, it dies, and all next spells cast at it do nothing. There are also $$$m$$$ composite spells in the spell book, numbered from $$$n+1$$$ to $$$n+m$$$. Each composite spell is a sequence of other spells, cast in specific order. A composite spell can consist both of basic spells and composite spells; the $$$i$$$-th spell consists of $$$s_i$$$ other spells, and each of those spells has index strictly less than $$$i$$$ (so there is no situation that composite spells infinitely cast each other). So, actually, each composite spell can be considered a finite sequence of basic spells, although its length might be huge. Note that the same spell can appear in a composite spell multiple times. Monocarp has decided to cast the $$$(n+m)$$$-th spell from his spell book. The target of this spell is a monster with an initial health value of $$$hp$$$. Monocarp wants to know whether the monster will be killed or not, and if it will be killed, which basic spell will kill it. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Each test case is given as follows: the first line contains two integers $$$n$$$ and $$$hp$$$ ($$$1 le n le 5000$$$; $$$1 le hp le 10^{9}$$$) — the number of basic spells and the initial health value of the monster; the second line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$-10^9 le b_i le 10^9$$$) — the descriptions of basic spells; the third line contains one integer $$$m$$$ ($$$1 le m le 5000$$$) — the number of composite spells; then $$$m$$$ lines follow, the $$$i$$$-th of these lines describes the $$$(n+i)$$$-th spell: it begins with an integer $$$s_{n+i}$$$ ($$$1 le s_{n+i} le 5000$$$) denoting the length of the spell (the number of spells it consists of); then a sequence of integers from $$$1$$$ to $$$(n+i-1)$$$ follows, denoting the sequence of spells. Additional constraints on the input: the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$; the sum of $$$m$$$ over all test cases does not exceed $$$5000$$$; the total length of all composite spells over all test cases does not exceed $$$5000$$$. | 2,600 | false | false | false | true | false | false | false | false | false | false | 519 |
1918G | Problem - 1918G - 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 math *2700 No tag edit access → Contest materials ") Editorial") , you obtain a permutation of the numbers in the original array. Input Each test case contains only one number, $$$n$$$ ($$$2 leq n leq 10^6$$$). Output If a solution exists, output "YES" (without quotes), followed by an array $$$a$$$ ($$$-10^9 leq a_i leq 10^9, a_i eq 0$$$) that satisfies the condition of the problem. If there are multiple possible answers, output any of them. If there is no suitable array, output "NO" (without quotes). The words "YES" and "NO" can be output in any case, for example, "YES", "Yes", "yEs", and so on. Examples Input 4 Output YES 1 2 -2 -1 Input 5 Output NO Note In the first test, the array | 2,700 | true | false | false | false | false | true | false | false | false | false | 782 |
845B | Problem - 845B - Codeforces =============== xa0 — this string denotes Luba's ticket. The ticket can start with the digit 0. Output Print one number — the minimum possible number of digits Luba needs to replace to make the ticket lucky. Examples Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 Note In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required. | 1,600 | false | true | true | false | false | false | true | false | false | false | 6,383 |
1477A | $$$n$$$ distinct integers $$$x_1,x_2,ldots,x_n$$$ are written on the board. Nezzar can perform the following operation multiple times. Select two integers $$$x,y$$$ (not necessarily distinct) on the board, and write down $$$2x-y$$$. Note that you don't remove selected numbers. Now, Nezzar wonders if it is possible to have his favorite number $$$k$$$ on the board after applying above operation multiple times. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$) — the number of test cases. The first line of each test case contains two integers $$$n,k$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$-10^{18} le k le 10^{18}$$$). The second line of each test case contains $$$n$$$ distinct integers $$$x_1,x_2,ldots,x_n$$$ ($$$-10^{18} le x_i le 10^{18}$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print "YES" on a single line if it is possible to have $$$k$$$ on the board. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 2 1 1 2 3 0 2 3 7 2 -1 31415926 27182818 2 1000000000000000000 1 1000000000000000000 2 -1000000000000000000 -1000000000000000000 123 6 80 -5 -20 13 -14 -2 -11 Output YES YES NO YES YES NO Note In the first test case, the number $$$1$$$ is already on the board. In the second test case, Nezzar could perform the following operations to write down $$$k=0$$$ on the board: Select $$$x=3$$$ and $$$y=2$$$ and write down $$$4$$$ on the board. Select $$$x=4$$$ and $$$y=7$$$ and write down $$$1$$$ on the board. Select $$$x=1$$$ and $$$y=2$$$ and write down $$$0$$$ on the board. In the third test case, it is impossible to have the number $$$k = -1$$$ on the board. | 1,800 | true | false | false | false | false | true | false | false | false | false | 3,290 |
835F | In the Kingdom K., there are _n_ towns numbered with integers from 1 to _n_. The towns are connected by _n_ bi-directional roads numbered with integers from 1 to _n_. The _i_-th road connects the towns _u__i_ and _v__i_ and its length is _l__i_. There is no more than one road between two towns. Also, there are no roads that connect the towns with itself. Let's call the inconvenience of the roads the maximum of the shortest distances between all pairs of towns. Because of lack of money, it was decided to close down one of the roads so that after its removal it is still possible to reach any town from any other. You have to find the minimum possible inconvenience of the roads after closing down one of the roads. Input The first line contains the integer _n_ (3u2009≤u2009_n_u2009≤u20092·105)xa0— the number of towns and roads. The next _n_ lines contain the roads description. The _i_-th from these lines contains three integers _u__i_, _v__i_, _l__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, 1u2009≤u2009_l__i_u2009≤u2009109)xa0— the numbers of towns connected by the _i_-th road and the length of the _i_-th road. No road connects a town to itself, no two roads connect the same towns. It's guaranteed that it's always possible to close down one of the roads so that all the towns are still reachable from each other. Output Print a single integerxa0— the minimum possible inconvenience of the roads after the refusal from one of the roads. Examples Input 5 2 3 7 3 1 9 4 1 8 3 5 4 4 5 5 | 2,500 | false | false | false | true | false | false | false | false | false | true | 6,422 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.