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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
283B | Farmer John has just given the cows a program to play with! The program contains two integer variables, _x_ and _y_, and performs the following operations on a sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ of positive integers: 1. Initially, _x_u2009=u20091 and _y_u2009=u20090. If, after any step, _x_u2009≤u20090 or _x_u2009>u2009_n_, the program immediately terminates. 2. The program increases both _x_ and _y_ by a value equal to _a__x_ simultaneously. 3. The program now increases _y_ by _a__x_ while decreasing _x_ by _a__x_. 4. The program executes steps 2 and 3 (first step 2, then step 3) repeatedly until it terminates (it may never terminate). So, the sequence of executed steps may start with: step 2, step 3, step 2, step 3, step 2 and so on. The cows are not very good at arithmetic though, and they want to see how the program works. Please help them! You are given the sequence _a_2,u2009_a_3,u2009...,u2009_a__n_. Suppose for each _i_ (1u2009≤u2009_i_u2009≤u2009_n_u2009-u20091) we run the program on the sequence _i_,u2009_a_2,u2009_a_3,u2009...,u2009_a__n_. For each such run output the final value of _y_ if the program terminates or -1 if it does not terminate. Input The first line contains a single integer, _n_ (2u2009≤u2009_n_u2009≤u20092·105). The next line contains _n_u2009-u20091 space separated integers, _a_2,u2009_a_3,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Output Output _n_u2009-u20091 lines. On the _i_-th line, print the requested value when the program is run on the sequence _i_,u2009_a_2,u2009_a_3,u2009..._a__n_. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Note In the first sample 1. For _i_u2009=u20091,u2009 _x_ becomes and _y_ becomes 1u2009+u20092u2009=u20093. 2. For _i_u2009=u20092,u2009 _x_ becomes and _y_ becomes 2u2009+u20094u2009=u20096. 3. For _i_u2009=u20093,u2009 _x_ becomes and _y_ becomes 3u2009+u20091u2009+u20094u2009=u20098. | 1,700 | false | false | false | true | false | false | false | false | false | true | 8,702 |
1242E | Ujan has finally cleaned up his house and now wants to decorate the interior. He decided to place a beautiful carpet that would really tie the guest room together. He is interested in carpets that are made up of polygonal patches such that each side of a patch is either a side of another (different) patch, or is an exterior side of the whole carpet. In other words, the carpet can be represented as a planar graph, where each patch corresponds to a face of the graph, each face is a simple polygon. The perimeter of the carpet is the number of the exterior sides. Ujan considers a carpet beautiful if it consists of $$$f$$$ patches, where the $$$i$$$-th patch has exactly $$$a_i$$$ sides, and the perimeter is the smallest possible. Find an example of such a carpet, so that Ujan can order it! Input The first line of input contains a single integer $$$f$$$ ($$$1 leq f leq 10^5$$$), the number of patches in the carpet. The next line contains $$$f$$$ integers $$$a_1, ldots, a_f$$$ ($$$3 leq a_i leq 3cdot 10^5$$$), the number of sides of the patches. The total number of the sides of the patches $$$a_1 + ldots + a_f$$$ does not exceed $$$3cdot10^5$$$. Output Output the description of the carpet as a graph. First, output a single integer $$$n$$$ ($$$3 leq n leq 3 cdot 10^5$$$), the total number of vertices in your graph (the vertices must be numbered from $$$1$$$ to $$$n$$$). Then output $$$f$$$ lines containing the description of the faces. The $$$i$$$-th line should describe the $$$i$$$-th face and contain $$$a_i$$$ distinct integers $$$v_{i,1}, ldots, v_{i,a_i}$$$ ($$$1 leq v_{i,j} leq n$$$), which means that the vertices $$$v_{i,j}$$$ and $$$v_{i,(j bmod{a_i})+1}$$$ are connected by an edge for any $$$1 leq j leq a_i$$$. The graph should be planar and satisfy the restrictions described in the problem statement. Its perimeter should be the smallest possible. There should be no double edges or self-loops in the graph. The graph should be connected. Note that a solution always exists; if there are multiple solutions, output any of them. Examples Output 6 1 2 3 4 5 4 5 6 1 3 4 6 5 Note In the first sample, the two triangular faces are connected by a single edge, which results in the minimum perimeter $$$4$$$. The figure shows one possible configuration for the second sample. The minimum perimeter in this case is $$$3$$$. | 3,200 | false | false | false | false | false | true | false | false | false | true | 4,485 |
817A | Captain Bill the Hummingbird and his crew recieved an interesting challenge offer. Some stranger gave them a map, potion of teleportation and said that only this potion might help them to reach the treasure. Bottle with potion has two values _x_ and _y_ written on it. These values define four moves which can be performed using the potion: Map shows that the position of Captain Bill the Hummingbird is (_x_1,u2009_y_1) and the position of the treasure is (_x_2,u2009_y_2). You task is to tell Captain Bill the Hummingbird whether he should accept this challenge or decline. If it is possible for Captain to reach the treasure using the potion then output "YES", otherwise "NO" (without quotes). The potion can be used infinite amount of times. Input The first line contains four integer numbers _x_1,u2009_y_1,u2009_x_2,u2009_y_2 (u2009-u2009105u2009≤u2009_x_1,u2009_y_1,u2009_x_2,u2009_y_2u2009≤u2009105) — positions of Captain Bill the Hummingbird and treasure respectively. The second line contains two integer numbers _x_,u2009_y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009105) — values on the potion bottle. Output Print "YES" if it is possible for Captain to reach the treasure using the potion, otherwise print "NO" (without quotes). Note In the first example there exists such sequence of moves: 1. — the first type of move 2. — the third type of move | 1,200 | true | false | true | false | false | false | false | false | false | false | 6,493 |
1373A | # Donut Shops Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes There are two rival donut shops. The first shop sells donuts at retail: each donut costs a dollars. The second shop sells donuts only in bulk: box of b donuts costs c dollars. So if you want to buy x donuts from this shop, then you have to buy the smallest number of boxes such that the total number of donuts in them is greater or equal to x.You want to determine two positive integer values: 1. how many donuts can you buy so that they are strictly cheaper in the first shop than in the second shop? 2. how many donuts can you buy so that they are strictly cheaper in the second shop than in the first shop? If any of these values doesn’t exist then that value should be equal to −1. If there are multiple possible answers, then print any of them. The printed values should be less or equal to 10 9. It can be shown that under the given constraints such values always exist if any values exist at all. # Input The first line contains a single integer t (1 ≤ t ≤ 1000 ) x16 the number of testcases. Each of the next t lines contains three integers a, b and c (1 ≤ a ≤ 10 9, 2 ≤ b ≤ 10 9, 1 ≤ c ≤ 10 9). # Output For each testcase print two positive integers. For both shops print such x that buying x donuts in this shop is strictly cheaper than buying x donuts in the other shop. x should be greater than 0 and less or equal to 10 9.If there is no such x, then print −1. If there are multiple answers, then print any of them. # Example standard input standard output 45 10 4 4 5 20 2 2 3 1000000000 1000000000 1000000000 -1 20 8 -1 1 2 -1 1000000000 # Note In the first testcase buying any number of donuts will be cheaper in the second shop. For example, for 3 or 5 donuts you’ll have to buy a box of 10 donuts for 4 dollars. 3 or 5 donuts in the first shop would cost you 15 or 25 dollars, respectively, however. For 20 donuts you’ll have to buy two boxes for 8 dollars total. Note that 3 and 5 are also valid answers for the second shop, along with many other answers. In the second testcase buying any number of donuts will be either cheaper in the first shop or the same price. 8 donuts cost 32 dollars in the first shop and 40 dollars in the second shop (because you have to Page 1 of 2 buy two boxes). 10 donuts will cost 40 dollars in both shops, so 10 is not a valid answer for any of the shops. In the third testcase 1 donut costs 2 and 3 dollars, respectively. 2 donuts cost 4 and 3 dollars. Thus, 1 is a valid answer for the first shop and 2 is a valid answer for the second shop. In the fourth testcase 10 9 donuts cost 10 18 dollars in the first shop and 10 9 dollars in the second shop. Page 2 of 2 | 1,000 | true | true | true | false | false | false | false | false | false | false | 3,801 |
1528A | Parsa has a humongous tree on $$$n$$$ vertices. On each vertex $$$v$$$ he has written two integers $$$l_v$$$ and $$$r_v$$$. To make Parsa's tree look even more majestic, Nima wants to assign a number $$$a_v$$$ ($$$l_v le a_v le r_v$$$) to each vertex $$$v$$$ such that the beauty of Parsa's tree is maximized. Nima's sense of the beauty is rather bizarre. He defines the beauty of the tree as the sum of $$$a_u - a_v$$$ over all edges $$$(u, v)$$$ of the tree. Since Parsa's tree is too large, Nima can't maximize its beauty on his own. Your task is to find the maximum possible beauty for Parsa's tree. Input The first line contains an integer $$$t$$$ $$$(1le tle 250)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(2le nle 10^5)$$$ — the number of vertices in Parsa's tree. The $$$i$$$-th of the following $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ $$$(1 le l_i le r_i le 10^9)$$$. Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ $$$(1 le u , v le n, u eq v)$$$ meaning that there is an edge between the vertices $$$u$$$ and $$$v$$$ in Parsa's tree. It is guaranteed that the given graph is a tree. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case print the maximum possible beauty for Parsa's tree. Example Input 3 2 1 6 3 8 1 2 3 1 3 4 6 7 9 1 2 2 3 6 3 14 12 20 12 19 2 12 10 17 3 17 3 2 6 5 1 5 2 6 4 6 Note The trees in the example: In the first test case, one possible assignment is $$$a = {1, 8}$$$ which results in $$$1 - 8 = 7$$$. In the second test case, one of the possible assignments is $$$a = {1, 5, 9}$$$ which results in a beauty of $$$1 - 5 + 5 - 9 = 8$$$ | 1,600 | false | true | false | true | false | false | false | false | false | false | 3,023 |
946F | Problem - 946F - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp matrices *2400 No tag edit access → Contest materials . Let's denote the cost of string _t_ as the number of occurences of _s_ in _t_. For example, if _s_ is 11 and _t_ is 111011, then the cost of _t_ is 3. Let's also denote the Fibonacci strings sequence as follows: _F_(0) is 0; _F_(1) is 1; _F_(_i_)u2009=u2009_F_(_i_u2009-u20091)u2009+u2009_F_(_i_u2009-u20092) if _i_u2009>u20091, where u2009+u2009 means the concatenation of two strings. Your task is to calculate the sum of costs of all subsequences of the string _F_(_x_). Since answer may be large, calculate it modulo 109u2009+u20097. Input The first line contains two integers _n_ and _x_ (1u2009≤u2009_n_u2009≤u2009100, 0u2009≤u2009_x_u2009≤u2009100) — the length of _s_ and the index of a Fibonacci string you are interested in, respectively. The second line contains _s_ — a string consisting of _n_ characters. Each of these characters is either 0 or 1. Output Print the only integer — the sum of costs of all subsequences of the string _F_(_x_), taken modulo 109u2009+u20097. Examples Input 2 4 11 Output 14 Input 10 100 1010101010 Output 553403224 | 2,400 | false | false | false | true | false | false | false | false | false | false | 5,927 |
1396C | Ziota found a video game called "Monster Invaders". Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns. For the sake of simplicity, we only consider two different types of monsters and three different types of guns. Namely, the two types of monsters are: a normal monster with $$$1$$$ hp. a boss with $$$2$$$ hp. And the three types of guns are: Pistol, deals $$$1$$$ hp in damage to one monster, $$$r_1$$$ reloading time Laser gun, deals $$$1$$$ hp in damage to all the monsters in the current level (including the boss), $$$r_2$$$ reloading time AWP, instantly kills any monster, $$$r_3$$$ reloading time The guns are initially not loaded, and the Ziota can only reload 1 gun at a time. The levels of the game can be considered as an array $$$a_1, a_2, ldots, a_n$$$, in which the $$$i$$$-th stage has $$$a_i$$$ normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the $$$a_i$$$ normal monsters. If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level $$$i$$$ $$$(1 < i < n)$$$ are levels $$$i - 1$$$ and $$$i + 1$$$, the only adjacent level of level $$$1$$$ is level $$$2$$$, the only adjacent level of level $$$n$$$ is level $$$n - 1$$$). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with $$$d$$$ teleportation time. In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation. Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value. Input The first line of the input contains five integers separated by single spaces: $$$n$$$ $$$(2 le n le 10^6)$$$xa0— the number of stages, $$$r_1, r_2, r_3$$$ $$$(1 le r_1 le r_2 le r_3 le 10^9)$$$xa0— the reload time of the three guns respectively, $$$d$$$ $$$(1 le d le 10^9)$$$xa0— the time of moving between adjacent levels. The second line of the input contains $$$n$$$ integers separated by single spaces $$$a_1, a_2, dots, a_n$$$ $$$(1 le a_i le 10^6, 1 le i le n)$$$. Note In the first test case, the optimal strategy is: Use the pistol to kill three normal monsters and AWP to kill the boss (Total time $$$1cdot3+4=7$$$) Move to stage two (Total time $$$7+3=10$$$) Use the pistol twice and AWP to kill the boss (Total time $$$10+1cdot2+4=16$$$) Move to stage three (Total time $$$16+3=19$$$) Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time $$$19+3+3=25$$$) Use the pistol once, use AWP to kill the boss (Total time $$$25+1cdot1+4=30$$$) Move back to stage three (Total time $$$30+3=33$$$) Kill the boss at stage three with the pistol (Total time $$$33+1=34$$$) Note that here, we do not finish at level $$$n$$$, but when all the bosses are killed. | 2,300 | false | true | true | true | false | false | false | false | false | false | 3,691 |
732E | The ICM ACPC World Finals is coming! Unfortunately, the organizers of the competition were so busy preparing tasks that totally missed an important technical point — the organization of electricity supplement for all the participants workstations. There are _n_ computers for participants, the _i_-th of which has power equal to positive integer _p__i_. At the same time there are _m_ sockets available, the _j_-th of which has power euqal to positive integer _s__j_. It is possible to connect the _i_-th computer to the _j_-th socket if and only if their powers are the same: _p__i_u2009=u2009_s__j_. It is allowed to connect no more than one computer to one socket. Thus, if the powers of all computers and sockets are distinct, then no computer can be connected to any of the sockets. In order to fix the situation professor Puch Williams urgently ordered a wagon of adaptersxa0— power splitters. Each adapter has one plug and one socket with a voltage divider between them. After plugging an adapter to a socket with power _x_, the power on the adapter's socket becomes equal to , it means that it is equal to the socket's power divided by two with rounding up, for example and . Each adapter can be used only once. It is possible to connect several adapters in a chain plugging the first to a socket. For example, if two adapters are plugged one after enother to a socket with power 10, it becomes possible to connect one computer with power 3 to this socket. The organizers should install adapters so that it will be possible to supply with electricity the maximum number of computers _c_ at the same time. If there are several possible connection configurations, they want to find the one that uses the minimum number of adapters _u_ to connect _c_ computers. Help organizers calculate the maximum number of connected computers _c_ and the minimum number of adapters _u_ needed for this. The wagon of adapters contains enough of them to do the task. It is guaranteed that it's possible to connect at least one computer. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009200u2009000)xa0— the number of computers and the number of sockets. The second line contains _n_ integers _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009109)xa0— the powers of the computers. The third line contains _m_ integers _s_1,u2009_s_2,u2009...,u2009_s__m_ (1u2009≤u2009_s__i_u2009≤u2009109)xa0— the power of the sockets. Output In the first line print two numbers _c_ and _u_xa0— the maximum number of computers which can at the same time be connected to electricity and the minimum number of adapters needed to connect _c_ computers. In the second line print _m_ integers _a_1,u2009_a_2,u2009...,u2009_a__m_ (0u2009≤u2009_a__i_u2009≤u2009109), where _a__i_ equals the number of adapters orginizers need to plug into the _i_-th socket. The sum of all _a__i_ should be equal to _u_. In third line print _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (0u2009≤u2009_b__i_u2009≤u2009_m_), where the _b__j_-th equals the number of the socket which the _j_-th computer should be connected to. _b__j_u2009=u20090 means that the _j_-th computer should not be connected to any socket. All _b__j_ that are different from 0 should be distinct. The power of the _j_-th computer should be equal to the power of the socket _b__j_ after plugging in _a__b__j_ adapters. The number of non-zero _b__j_ should be equal to _c_. If there are multiple answers, print any of them. | 2,100 | false | true | false | false | false | false | false | false | true | false | 6,866 |
2021E1 | This is the easy version of the problem. In the three versions, the constraints on $$$n$$$ and $$$m$$$ are different. You can make hacks only if all the versions of the problem are solved. Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with $$$n$$$ houses and $$$m$$$ internet cables connecting house $$$u_i$$$ and house $$$v_i$$$, each with a latency of $$$w_i$$$. There are $$$p$$$ houses that require internet. Pak Chanek can install servers in at most $$$k$$$ of the houses. The houses that need internet will then be connected to one of the servers. However, since each cable has its latency, the latency experienced by house $$$s_i$$$ requiring internet will be the maximum latency of the cables between that house and the server it is connected to. For each $$$k = 1,2,ldots,n$$$, help Pak Chanek determine the minimum total latency that can be achieved for all the houses requiring internet. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, $$$p$$$ ($$$2 le n le 400$$$; $$$n-1 le m le 400$$$; $$$1 le p le n$$$)xa0— the number of houses, the number of cables and the number of houses that need internet. The second line of each test case contains $$$p$$$ integers $$$s_1, s_2, ldots, s_p$$$ ($$$1 le s_i le n$$$)xa0— the houses that need internet. It is guaranteed that all elements of $$$s$$$ are distinct. The $$$i$$$-th of the next $$$m$$$ lines of each test case contains three integers $$$u_i$$$, $$$v_i$$$, and $$$w_i$$$ ($$$1 le u_i < v_i le n$$$; $$$1 le w_i le 10^9$$$)xa0— the internet cable connecting house $$$u_i$$$ and house $$$v_i$$$ with latency of $$$w_i$$$. It is guaranteed that the given edges form a connected simple graph. It is guaranteed that the sum of $$$n^3$$$ and the sum of $$$m^3$$$ do not exceed $$$10^8$$$. Output For each test case, output $$$n$$$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $$$k = 1,2,ldots,n$$$. Example Input 2 9 8 5 2 5 6 8 9 1 2 1 1 3 2 3 4 10 4 5 3 4 6 5 1 7 10 7 8 4 7 9 2 3 3 2 3 1 1 2 1 2 3 3 1 3 2 Output 34 19 9 4 0 0 0 0 0 2 0 0 Note In the first test case for $$$k=3$$$, a possible optimal solution is to install servers at vertices $$$2$$$, $$$6$$$ and $$$8$$$ and obtain the following latency: $$$ ext{latency}(2) = 0$$$ $$$ ext{latency}(5) = max(3, 5) = 5$$$ $$$ ext{latency}(6) = 0$$$ $$$ ext{latency}(8) = 0$$$ $$$ ext{latency}(9) = max(2, 4) = 4$$$ So the total latency is $$$9$$$. | 2,300 | true | true | true | true | true | false | true | false | false | true | 141 |
543A | Programmers working on a large project have just received a task to write exactly _m_ lines of code. There are _n_ programmers working on a project, the _i_-th of them makes exactly _a__i_ bugs in every line of code that he writes. Let's call a sequence of non-negative integers _v_1,u2009_v_2,u2009...,u2009_v__n_ a plan, if _v_1u2009+u2009_v_2u2009+u2009...u2009+u2009_v__n_u2009=u2009_m_. The programmers follow the plan like that: in the beginning the first programmer writes the first _v_1 lines of the given task, then the second programmer writes _v_2 more lines of the given task, and so on. In the end, the last programmer writes the remaining lines of the code. Let's call a plan good, if all the written lines of the task contain at most _b_ bugs in total. Your task is to determine how many distinct good plans are there. As the number of plans can be large, print the remainder of this number modulo given positive integer _mod_. Input The first line contains four integers _n_, _m_, _b_, _mod_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009500, 0u2009≤u2009_b_u2009≤u2009500; 1u2009≤u2009_mod_u2009≤u2009109u2009+u20097)xa0— the number of programmers, the number of lines of code in the task, the maximum total number of bugs respectively and the modulo you should use when printing the answer. The next line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009500)xa0— the number of bugs per line for each programmer. Output Print a single integer — the answer to the problem modulo _mod_. Examples Input 3 6 5 1000000007 1 2 3 | 1,800 | false | false | false | true | false | false | false | false | false | false | 7,669 |
1085D | You are given a tree (an undirected connected graph without cycles) and an integer $$$s$$$. Vanya wants to put weights on all edges of the tree so that all weights are non-negative real numbers and their sum is $$$s$$$. At the same time, he wants to make the diameter of the tree as small as possible. Let's define the diameter of a weighed tree as the maximum sum of the weights of the edges lying on the path between two some vertices of the tree. In other words, the diameter of a weighed tree is the length of the longest simple path in the tree, where length of a path is equal to the sum of weights over all edges in the path. Find the minimum possible diameter that Vanya can get. Input The first line contains two integer numbers $$$n$$$ and $$$s$$$ ($$$2 leq n leq 10^5$$$, $$$1 leq s leq 10^9$$$) — the number of vertices in the tree and the sum of edge weights. Each of the following $$$n−1$$$ lines contains two space-separated integer numbers $$$a_i$$$ and $$$b_i$$$ ($$$1 leq a_i, b_i leq n$$$, $$$a_i eq b_i$$$) — the indexes of vertices connected by an edge. The edges are undirected. It is guaranteed that the given edges form a tree. Output Print the minimum diameter of the tree that Vanya can get by placing some non-negative real weights on its edges with the sum equal to $$$s$$$. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is considered correct if $$$frac {a-b} {max(1, b)} leq 10^{-6}$$$. Examples Output 2.000000000000000000 Input 6 1 2 1 2 3 2 5 5 4 5 6 Output 0.500000000000000000 Output 3.333333333333333333 Note In the first example it is necessary to put weights like this: It is easy to see that the diameter of this tree is $$$2$$$. It can be proved that it is the minimum possible diameter. In the second example it is necessary to put weights like this: | 1,700 | false | false | true | false | false | true | false | false | false | false | 5,296 |
1661F | Problem - 1661F - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search greedy *2600 No tag edit access → Contest materials ^2$$$ energy. You want to install some additional teleporters so that it is possible to get from the point $$$0$$$ to the point $$$a_n$$$ (possibly through some other teleporters) spending no more than $$$m$$$ energy in total. Each teleporter you install must be located in an integer point. What is the minimum number of teleporters you have to install? Input The first line contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_1 < a_2 < a_3 < dots < a_n le 10^9$$$). The third line contains one integer $$$m$$$ ($$$a_n le m le 10^{18}$$$). Output Print one integer — the minimum number of teleporters you have to install so that it is possible to get from $$$0$$$ to $$$a_n$$$ spending at most $$$m$$$ energy. It can be shown that it's always possible under the constraints from the input format. Examples Input 2 1 5 7 Output 2 Input 2 1 5 6 Output 3 Input 1 5 5 Output 4 Input 1 1000000000 1000000043 Output 999999978 | 2,600 | false | true | false | false | false | false | false | true | false | false | 2,314 |
340A | Iahub and his friend Floyd have started painting a wall. Iahub is painting the wall red and Floyd is painting it pink. You can consider the wall being made of a very large number of bricks, numbered 1, 2, 3 and so on. Iahub has the following scheme of painting: he skips _x_u2009-u20091 consecutive bricks, then he paints the _x_-th one. That is, he'll paint bricks _x_, 2·_x_, 3·_x_ and so on red. Similarly, Floyd skips _y_u2009-u20091 consecutive bricks, then he paints the _y_-th one. Hence he'll paint bricks _y_, 2·_y_, 3·_y_ and so on pink. After painting the wall all day, the boys observed that some bricks are painted both red and pink. Iahub has a lucky number _a_ and Floyd has a lucky number _b_. Boys wonder how many bricks numbered no less than _a_ and no greater than _b_ are painted both red and pink. This is exactly your task: compute and print the answer to the question. Input The input will have a single line containing four integers in this order: _x_, _y_, _a_, _b_. (1u2009≤u2009_x_,u2009_y_u2009≤u20091000, 1u2009≤u2009_a_,u2009_b_u2009≤u20092·109, _a_u2009≤u2009_b_). Output Output a single integer — the number of bricks numbered no less than _a_ and no greater than _b_ that are painted both red and pink. Note Let's look at the bricks from _a_ to _b_ (_a_u2009=u20096,u2009_b_u2009=u200918). The bricks colored in red are numbered 6, 8, 10, 12, 14, 16, 18. The bricks colored in pink are numbered 6, 9, 12, 15, 18. The bricks colored in both red and pink are numbered with 6, 12 and 18. | 1,200 | true | false | false | false | false | false | false | false | false | false | 8,475 |
1462A | Polycarp has a favorite sequence $$$a[1 dots n]$$$ consisting of $$$n$$$ integers. He wrote it out on the whiteboard as follows: he wrote the number $$$a_1$$$ to the left side (at the beginning of the whiteboard); he wrote the number $$$a_2$$$ to the right side (at the end of the whiteboard); then as far to the left as possible (but to the right from $$$a_1$$$), he wrote the number $$$a_3$$$; then as far to the right as possible (but to the left from $$$a_2$$$), he wrote the number $$$a_4$$$; Polycarp continued to act as well, until he wrote out the entire sequence on the whiteboard. The beginning of the result looks like this (of course, if $$$n ge 4$$$). For example, if $$$n=7$$$ and $$$a=[3, 1, 4, 1, 5, 9, 2]$$$, then Polycarp will write a sequence on the whiteboard $$$[3, 4, 5, 2, 9, 1, 1]$$$. You saw the sequence written on the whiteboard and now you want to restore Polycarp's favorite sequence. Input The first line contains a single positive integer $$$t$$$ ($$$1 le t le 300$$$)xa0— the number of test cases in the test. Then $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 300$$$)xa0— the length of the sequence written on the whiteboard. The next line contains $$$n$$$ integers $$$b_1, b_2,ldots, b_n$$$ ($$$1 le b_i le 10^9$$$)xa0— the sequence written on the whiteboard. Output Output $$$t$$$ answers to the test cases. Each answerxa0— is a sequence $$$a$$$ that Polycarp wrote out on the whiteboard. Example Input 6 7 3 4 5 2 9 1 1 4 9 2 7 1 11 8 4 3 1 2 7 8 7 9 4 2 1 42 2 11 7 8 1 1 1 1 1 1 1 1 Output 3 1 4 1 5 9 2 9 1 2 7 8 2 4 4 3 9 1 7 2 8 7 42 11 7 1 1 1 1 1 1 1 1 Note In the first test case, the sequence $$$a$$$ matches the sequence from the statement. The whiteboard states after each step look like this: $$$[3] Rightarrow [3, 1] Rightarrow [3, 4, 1] Rightarrow [3, 4, 1, 1] Rightarrow [3, 4, 5, 1, 1] Rightarrow [3, 4, 5, 9, 1, 1] Rightarrow [3, 4, 5, 2, 9, 1, 1]$$$. | 800 | false | false | true | false | false | false | false | false | false | false | 3,380 |
1092E | You are given a forest — an undirected graph with $$$n$$$ vertices such that each its connected component is a tree. The diameter (aka "longest shortest path") of a connected undirected graph is the maximum number of edges in the shortest path between any pair of its vertices. You task is to add some edges (possibly zero) to the graph so that it becomes a tree and the diameter of the tree is minimal possible. If there are multiple correct answers, print any of them. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 1000$$$, $$$0 le m le n - 1$$$) — the number of vertices of the graph and the number of edges, respectively. Each of the next $$$m$$$ lines contains two integers $$$v$$$ and $$$u$$$ ($$$1 le v, u le n$$$, $$$v e u$$$) — the descriptions of the edges. It is guaranteed that the given graph is a forest. Output In the first line print the diameter of the resulting tree. Each of the next $$$(n - 1) - m$$$ lines should contain two integers $$$v$$$ and $$$u$$$ ($$$1 le v, u le n$$$, $$$v e u$$$) — the descriptions of the added edges. The resulting graph should be a tree and its diameter should be minimal possible. For $$$m = n - 1$$$ no edges are added, thus the output consists of a single integer — diameter of the given tree. If there are multiple correct answers, print any of them. Note In the first example adding edges (1, 4) or (3, 4) will lead to a total diameter of 3. Adding edge (2, 4), however, will make it 2. Edge (1, 2) is the only option you have for the second example. The diameter is 1. You can't add any edges in the third example. The diameter is already 2. | 2,000 | false | true | false | false | false | true | false | false | false | false | 5,246 |
1144C | Two integer sequences existed initially — one of them was strictly increasing, and the other one — strictly decreasing. Strictly increasing sequence is a sequence of integers $$$[x_1 < x_2 < dots < x_k]$$$. And strictly decreasing sequence is a sequence of integers $$$[y_1 > y_2 > dots > y_l]$$$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. They were merged into one sequence $$$a$$$. After that sequence $$$a$$$ got shuffled. For example, some of the possible resulting sequences $$$a$$$ for an increasing sequence $$$[1, 3, 4]$$$ and a decreasing sequence $$$[10, 4, 2]$$$ are sequences $$$[1, 2, 3, 4, 4, 10]$$$ or $$$[4, 2, 1, 10, 4, 3]$$$. This shuffled sequence $$$a$$$ is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one — strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. If there is a contradiction in the input and it is impossible to split the given sequence $$$a$$$ to increasing and decreasing sequences, print "NO". Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 2 cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. Output If there is a contradiction in the input and it is impossible to split the given sequence $$$a$$$ to increasing and decreasing sequences, print "NO" in the first line. Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. In the second line print $$$n_i$$$ — the number of elements in the strictly increasing sequence. $$$n_i$$$ can be zero, in this case the increasing sequence is empty. In the third line print $$$n_i$$$ integers $$$inc_1, inc_2, dots, inc_{n_i}$$$ in the increasing order of its values ($$$inc_1 < inc_2 < dots < inc_{n_i}$$$) — the strictly increasing sequence itself. You can keep this line empty if $$$n_i = 0$$$ (or just print the empty line). In the fourth line print $$$n_d$$$ — the number of elements in the strictly decreasing sequence. $$$n_d$$$ can be zero, in this case the decreasing sequence is empty. In the fifth line print $$$n_d$$$ integers $$$dec_1, dec_2, dots, dec_{n_d}$$$ in the decreasing order of its values ($$$dec_1 > dec_2 > dots > dec_{n_d}$$$) — the strictly decreasing sequence itself. You can keep this line empty if $$$n_d = 0$$$ (or just print the empty line). $$$n_i + n_d$$$ should be equal to $$$n$$$ and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer). Examples Output YES 2 3 7 5 7 4 3 2 1 | 1,000 | false | false | false | false | false | true | false | false | true | false | 5,003 |
1032D | In this problem we consider a very simplified model of Barcelona city. Barcelona can be represented as a plane with streets of kind $$$x = c$$$ and $$$y = c$$$ for every integer $$$c$$$ (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points $$$(x, y)$$$ for which $$$ax + by + c = 0$$$. One can walk along streets, including the avenue. You are given two integer points $$$A$$$ and $$$B$$$ somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to $$$B$$$ from $$$A$$$. Input The first line contains three integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$-10^9leq a, b, cleq 10^9$$$, at least one of $$$a$$$ and $$$b$$$ is not zero) representing the Diagonal Avenue. The next line contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ ($$$-10^9leq x_1, y_1, x_2, y_2leq 10^9$$$) denoting the points $$$A = (x_1, y_1)$$$ and $$$B = (x_2, y_2)$$$. Output Find the minimum possible travel distance between $$$A$$$ and $$$B$$$. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$frac{a - b}{max{(1, b)}} le 10^{-6}$$$. Note The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot. | 1,900 | false | false | true | false | false | false | false | false | false | false | 5,539 |
362D | You must have heard all about the Foolland on your Geography lessons. Specifically, you must know that federal structure of this country has been the same for many centuries. The country consists of _n_ cities, some pairs of cities are connected by bidirectional roads, each road is described by its length _l__i_. The fools lived in their land joyfully, but a recent revolution changed the king. Now the king is Vasily the Bear. Vasily divided the country cities into regions, so that any two cities of the same region have a path along the roads between them and any two cities of different regions don't have such path. Then Vasily decided to upgrade the road network and construct exactly _p_ new roads in the country. Constructing a road goes like this: 1. We choose a pair of distinct cities _u_, _v_ that will be connected by a new road (at that, it is possible that there already is a road between these cities). 2. We define the length of the new road: if cities _u_, _v_ belong to distinct regions, then the length is calculated as _min_(109,u2009_S_u2009+u20091) (_S_ — the total length of all roads that exist in the linked regions), otherwise we assume that the length equals 1000. 3. We build a road of the specified length between the chosen cities. If the new road connects two distinct regions, after construction of the road these regions are combined into one new region. Vasily wants the road constructing process to result in the country that consists exactly of _q_ regions. Your task is to come up with such road constructing plan for Vasily that it meets the requirement and minimizes the total length of the built roads. Input The first line contains four integers _n_ (1u2009≤u2009_n_u2009≤u2009105), _m_ (0u2009≤u2009_m_u2009≤u2009105), _p_ (0u2009≤u2009_p_u2009≤u2009105), _q_ (1u2009≤u2009_q_u2009≤u2009_n_) — the number of cities in the Foolland, the number of existing roads, the number of roads that are planned to construct and the required number of regions. Next _m_ lines describe the roads that exist by the moment upgrading of the roads begun. Each of these lines contains three integers _x__i_, _y__i_, _l__i_: _x__i_, _y__i_ — the numbers of the cities connected by this road (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_,u2009_x__i_u2009≠u2009_y__i_), _l__i_ — length of the road (1u2009≤u2009_l__i_u2009≤u2009109). Note that one pair of cities can be connected with multiple roads. Output If constructing the roads in the required way is impossible, print a single string "NO" (without the quotes). Otherwise, in the first line print word "YES" (without the quotes), and in the next _p_ lines print the road construction plan. Each line of the plan must consist of two distinct integers, giving the numbers of the cities connected by a road. The road must occur in the plan in the order they need to be constructed. If there are multiple optimal solutions, you can print any of them. Examples Input 9 6 2 2 1 2 2 3 2 1 4 6 20 1 3 8 7 8 3 5 7 2 Note Consider the first sample. Before the reform the Foolland consists of four regions. The first region includes cities 1, 2, 3, the second region has cities 4 and 6, the third region has cities 5, 7, 8, the fourth region has city 9. The total length of the roads in these cities is 11, 20, 5 and 0, correspondingly. According to the plan, we first build the road of length 6 between cities 5 and 9, then the road of length 23 between cities 1 and 9. Thus, the total length of the built roads equals 29. | 2,100 | false | true | false | false | true | false | false | false | false | true | 8,384 |
1217C | You are given a binary string $$$s$$$ (recall that a string is binary if each character is either $$$0$$$ or $$$1$$$). Let $$$f(t)$$$ be the decimal representation of integer $$$t$$$ written in binary form (possibly with leading zeroes). For example $$$f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$$$ and $$$f(000100) = 4$$$. The substring $$$s_{l}, s_{l+1}, dots , s_{r}$$$ is good if $$$r - l + 1 = f(s_l dots s_r)$$$. For example string $$$s = 1011$$$ has $$$5$$$ good substrings: $$$s_1 dots s_1 = 1$$$, $$$s_3 dots s_3 = 1$$$, $$$s_4 dots s_4 = 1$$$, $$$s_1 dots s_2 = 10$$$ and $$$s_2 dots s_4 = 011$$$. Your task is to calculate the number of good substrings of string $$$s$$$. You have to answer $$$t$$$ independent queries. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of queries. The only line of each query contains string $$$s$$$ ($$$1 le s le 2 cdot 10^5$$$), consisting of only digits $$$0$$$ and $$$1$$$. It is guaranteed that $$$sumlimits_{i=1}^{t} s_i le 2 cdot 10^5$$$. Output For each query print one integer — the number of good substrings of string $$$s$$$. Example Input 4 0110 0101 00001000 0001000 | 1,700 | false | false | false | false | false | false | true | true | false | false | 4,588 |
1808E3 | This is the hard version of the problem. The only difference between the three versions is the constraints on $$$n$$$ and $$$k$$$. You can make hacks only if all versions of the problem are solved. Maxim is a minibus driver on Venus. To ride on Maxim's minibus, you need a ticket. Each ticket has a number consisting of $$$n$$$ digits. However, as we know, the residents of Venus use a numeral system with base $$$k$$$, rather than the decimal system. Therefore, the ticket number can be considered as a sequence of $$$n$$$ integers from $$$0$$$ to $$$k-1$$$, inclusive. The residents of Venus consider a ticket to be lucky if there is a digit on it that is equal to the sum of the remaining digits, modulo $$$k$$$. For example, if $$$k=10$$$, then the ticket $$$7135$$$ is lucky because $$$7 + 1 + 5 equiv 3 pmod{10}$$$. On the other hand, the ticket $$$7136$$$ is not lucky because no digit is equal to the sum of the others modulo $$$10$$$. Once, while on a trip, Maxim wondered: how many lucky tickets exist? At the same time, Maxim understands that this number can be very large, so he is interested only in the answer modulo some prime number $$$m$$$. Input The only line of the input contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 le n le 10^{18}$$$, $$$1 le k le 2000$$$, $$$10^8 le m le 10^9 + 7$$$, $$$m$$$ is a prime number)xa0— the number of digits on the ticket, the base of the numeral system on Venus, and the module for answer calculation. Note In the first example, there are only four lucky tickets: $$$000$$$, $$$011$$$, $$$101$$$, and $$$110$$$. | 2,800 | true | false | false | true | false | false | true | false | false | false | 1,421 |
552A | Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right. In this table, Vanya chose _n_ rectangles with sides that go along borders of squares (some rectangles probably occur multiple times). After that for each cell of the table he counted the number of rectangles it belongs to and wrote this number into it. Now he wants to find the sum of values in all cells of the table and as the table is too large, he asks you to help him find the result. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of rectangles. Each of the following _n_ lines contains four integers _x_1,u2009_y_1,u2009_x_2,u2009_y_2 (1u2009≤u2009_x_1u2009≤u2009_x_2u2009≤u2009100, 1u2009≤u2009_y_1u2009≤u2009_y_2u2009≤u2009100), where _x_1 and _y_1 are the number of the column and row of the lower left cell and _x_2 and _y_2 are the number of the column and row of the upper right cell of a rectangle. Output In a single line print the sum of all values in the cells of the table. Note Note to the first sample test: Values of the table in the first three rows and columns will be as follows: 121 121 110 So, the sum of values will be equal to 10. Note to the second sample test: Values of the table in the first three rows and columns will be as follows: 222 222 222 So, the sum of values will be equal to 18. | 1,000 | true | false | true | false | false | false | false | false | false | false | 7,627 |
571E | Problem - 571E - 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 *3200 No tag edit access → Contest materials — the number of geometric progressions. Next _n_ lines contain pairs of integers _a_,u2009_b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009109), that are the first element and the common ratio of the corresponding geometric progression. Output If the intersection of all progressions is empty, then print u2009-u20091, otherwise print the remainder of the minimal positive integer number belonging to all progressions modulo 1000000007 (109u2009+u20097). Examples Input 2 2 2 4 1 Output 4 Input 2 2 2 3 3 Output -1 Note In the second sample test one of the progressions contains only powers of two, the other one contains only powers of three. | 3,200 | true | false | false | false | false | false | false | false | false | false | 7,562 |
263C | One day Vasya came up to the blackboard and wrote out _n_ distinct integers from 1 to _n_ in some order in a circle. Then he drew arcs to join the pairs of integers (_a_,u2009_b_) (_a_u2009≠u2009_b_), that are either each other's immediate neighbors in the circle, or there is number _c_, such that _a_ and _с_ are immediate neighbors, and _b_ and _c_ are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·_n_ arcs. For example, if the numbers are written in the circle in the order 1,u20092,u20093,u20094,u20095 (in the clockwise direction), then the arcs will join pairs of integers (1,u20092), (2,u20093), (3,u20094), (4,u20095), (5,u20091), (1,u20093), (2,u20094), (3,u20095), (4,u20091) and (5,u20092). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·_n_ written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer _n_ (5u2009≤u2009_n_u2009≤u2009105) that shows, how many numbers were written on the board. Next 2·_n_ lines contain pairs of integers _a__i_, _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to _n_ on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of _n_ distinct integers from 1 to _n_. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. | 2,000 | false | false | true | false | false | false | true | false | false | false | 8,781 |
755B | PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses. You're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, if both play optimally? Input The first input line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009103)xa0— number of words PolandBall and EnemyBall know, respectively. Then _n_ strings follow, one per linexa0— words familiar to PolandBall. Then _m_ strings follow, one per linexa0— words familiar to EnemyBall. Note that one Ball cannot know a word more than once (strings are unique), but some words can be known by both players. Each word is non-empty and consists of no more than 500 lowercase English alphabet letters. Output In a single line of print the answerxa0— "YES" if PolandBall wins and "NO" otherwise. Both Balls play optimally. Examples Input 5 1 polandball is a cool character nope Input 2 2 kremowka wadowicka kremowka wiedenska Note In the first example PolandBall knows much more words and wins effortlessly. In the second example if PolandBall says kremowka first, then EnemyBall cannot use that word anymore. EnemyBall can only say wiedenska. PolandBall says wadowicka and wins. | 1,100 | false | true | false | false | true | false | false | true | true | false | 6,776 |
5A | Polycarp is working on a new project called "Polychat". Following modern tendencies in IT, he decided, that this project should contain chat as well. To achieve this goal, Polycarp has spent several hours in front of his laptop and implemented a chat server that can process three types of commands: Include a person to the chat ('Add' command). Remove a person from the chat ('Remove' command). Send a message from a person to all people, who are currently in the chat, including the one, who sends the message ('Send' command). Now Polycarp wants to find out the amount of outgoing traffic that the server will produce while processing a particular set of commands. Polycarp knows that chat server sends no traffic for 'Add' and 'Remove' commands. When 'Send' command is processed, server sends _l_ bytes to each participant of the chat, where _l_ is the length of the message. As Polycarp has no time, he is asking for your help in solving this problem. Input Input file will contain not more than 100 commands, each in its own line. No line will exceed 100 characters. Formats of the commands will be the following: +<name> for 'Add' command. -<name> for 'Remove' command. <sender_name>:<message_text> for 'Send' command. <name> and <sender_name> is a non-empty sequence of Latin letters and digits. <message_text> can contain letters, digits and spaces, but can't start or end with a space. <message_text> can be an empty line. It is guaranteed, that input data are correct, i.e. there will be no 'Add' command if person with such a name is already in the chat, there will be no 'Remove' command if there is no person with such a name in the chat etc. All names are case-sensitive. Output Print a single number — answer to the problem. Examples Input +Mike Mike:hello +Kate +Dmitry -Dmitry Kate:hi -Kate Input +Mike -Mike +Mike Mike:Hi I am here -Mike +Kate -Kate | 1,000 | false | false | true | false | false | false | false | false | false | false | 9,970 |
587E | Duff is the queen of her country, Andarz Gu. She's a competitive programming fan. That's why, when he saw her minister, Malek, free, she gave her a sequence consisting of _n_ non-negative integers, _a_1,u2009_a_2,u2009...,u2009_a__n_ and asked him to perform _q_ queries for her on this sequence. There are two types of queries: 1. given numbers _l_,u2009_r_ and _k_, Malek should perform for each _l_u2009≤u2009_i_u2009≤u2009_r_ (, bitwise exclusive OR of numbers _a_ and _b_). 2. given numbers _l_ and _r_ Malek should tell her the score of sequence _a__l_,u2009_a__l_u2009+u20091,u2009... ,u2009_a__r_. Score of a sequence _b_1,u2009...,u2009_b__k_ is the number of its different Kheshtaks. A non-negative integer _w_ is a Kheshtak of this sequence if and only if there exists a subsequence of _b_, let's denote it as _b__i_1,u2009_b__i_2,u2009... ,u2009_b__i__x_ (possibly empty) such that (1u2009≤u2009_i_1u2009<u2009_i_2u2009<u2009...u2009<u2009_i__x_u2009≤u2009_k_). If this subsequence is empty, then _w_u2009=u20090. Unlike Duff, Malek is not a programmer. That's why he asked for your help. Please help him perform these queries. Input The first line of input contains two integers, _n_ and _q_ (1u2009≤u2009_n_u2009≤u20092u2009×u2009105 and 1u2009≤u2009_q_u2009≤u20094u2009×u2009104). The second line of input contains _n_ integers, _a_1,u2009_a_2,u2009...,u2009_a__n_ separated by spaces (0u2009≤u2009_a__i_u2009≤u2009109 for each 1u2009≤u2009_i_u2009≤u2009_n_). The next _q_ lines contain the queries. Each line starts with an integer _t_ (1u2009≤u2009_t_u2009≤u20092), type of the corresponding query. If _t_u2009=u20091, then there are three more integers in that line, _l_,u2009_r_ and _k_. Otherwise there are two more integers, _l_ and _r_. (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_ and 0u2009≤u2009_k_u2009≤u2009109) Output Print the answer of each query of the second type in one line. Examples Input 5 5 1 2 3 4 2 2 1 5 1 2 2 8 2 1 5 1 1 3 10 2 2 2 Note In the first query, we want all Kheshtaks of sequence 1,u20092,u20093,u20094,u20092 which are: 0,u20091,u20092,u20093,u20094,u20095,u20096,u20097. In the third query, we want all Khestaks of sequence 1,u200910,u20093,u20094,u20092 which are: 0,u20091,u20092,u20093,u20094,u20095,u20096,u20097,u20098,u20099,u200910,u200911,u200912,u200913,u200914,u200915. In the fifth query, we want all Kheshtaks of sequence 0 which is 0. | 2,900 | false | false | false | false | true | false | false | false | false | false | 7,493 |
1166F | In a magical land there are $$$n$$$ cities conveniently numbered $$$1, 2, dots, n$$$. Some pairs of these cities are connected by magical colored roads. Magic is unstable, so at any time, new roads may appear between two cities. Vicky the witch has been tasked with performing deliveries between some pairs of cities. However, Vicky is a beginner, so she can only complete a delivery if she can move from her starting city to her destination city through a double rainbow. A double rainbow is a sequence of cities $$$c_1, c_2, dots, c_k$$$ satisfying the following properties: For each $$$i$$$ with $$$1 le i le k - 1$$$, the cities $$$c_i$$$ and $$$c_{i + 1}$$$ are connected by a road. For each $$$i$$$ with $$$1 le i le frac{k - 1}{2}$$$, the roads connecting $$$c_{2i}$$$ with $$$c_{2i - 1}$$$ and $$$c_{2i + 1}$$$ have the same color. For example if $$$k = 5$$$, the road between $$$c_1$$$ and $$$c_2$$$ must be the same color as the road between $$$c_2$$$ and $$$c_3$$$, and the road between $$$c_3$$$ and $$$c_4$$$ must be the same color as the road between $$$c_4$$$ and $$$c_5$$$. Vicky has a list of events in chronological order, where each event is either a delivery she must perform, or appearance of a new road. Help her determine which of her deliveries she will be able to complete. Input The first line contains four integers $$$n$$$, $$$m$$$, $$$c$$$, and $$$q$$$ ($$$2 le n le 10^5$$$, $$$1 le m, c, q le 10^5$$$), denoting respectively the number of cities, the number of roads initially present, the number of different colors the roads can take, and the number of events. Each of the following $$$m$$$ lines contains three integers $$$x$$$, $$$y$$$, and $$$z$$$ ($$$1 le x, y le n$$$, $$$1 le z le c$$$), describing that there initially exists a bidirectional road with color $$$z$$$ between cities $$$x$$$ and $$$y$$$. Then $$$q$$$ lines follow, describing the events. Each event is one of the following two types: 1. + x y z ($$$1 le x, y le n$$$, $$$1 le z le c$$$), meaning a road with color $$$z$$$ appears between cities $$$x$$$ and $$$y$$$; 2. ? x y ($$$1 le x, y le n$$$), meaning you should determine whether Vicky can make a delivery starting at city $$$x$$$ and ending at city $$$y$$$. It is guaranteed that $$$x eq y$$$. It is guaranteed that at any moment, there is at most one road connecting any pair of cities, and that no road connects a city to itself. It is guaranteed that the input contains at least one event of the second type. Output For each event of the second type, print a single line containing "Yes" (without quotes) if the delivery can be made, or a single line containing "No" (without quotes) otherwise. Example Input 4 3 2 4 1 2 1 2 3 1 3 4 2 ? 1 4 ? 4 1 + 3 1 2 ? 4 1 Note The following picture corresponds to the sample. For her first delivery, Vicky can use the sequence 1, 2, 3, 4 which is a double rainbow. However, she cannot complete the second delivery, as she can only reach city $$$3$$$. After adding the road between cities $$$1$$$ and $$$3$$$, she can now complete a delivery from city $$$4$$$ to city $$$1$$$ by using the double rainbow 4, 3, 1. | 2,400 | false | false | false | false | true | false | false | false | false | true | 4,884 |
1137B | The new camp by widely-known over the country Spring Programming Camp is going to start soon. Hence, all the team of friendly curators and teachers started composing the camp's schedule. After some continuous discussion, they came up with a schedule $$$s$$$, which can be represented as a binary string, in which the $$$i$$$-th symbol is '1' if students will write the contest in the $$$i$$$-th day and '0' if they will have a day off. At the last moment Gleb said that the camp will be the most productive if it runs with the schedule $$$t$$$ (which can be described in the same format as schedule $$$s$$$). Since the number of days in the current may be different from number of days in schedule $$$t$$$, Gleb required that the camp's schedule must be altered so that the number of occurrences of $$$t$$$ in it as a substring is maximum possible. At the same time, the number of contest days and days off shouldn't change, only their order may change. Could you rearrange the schedule in the best possible way? Input The first line contains string $$$s$$$ ($$$1 leqslant s leqslant 500,000$$$), denoting the current project of the camp's schedule. The second line contains string $$$t$$$ ($$$1 leqslant t leqslant 500,000$$$), denoting the optimal schedule according to Gleb. Strings $$$s$$$ and $$$t$$$ contain characters '0' and '1' only. Output In the only line print the schedule having the largest number of substrings equal to $$$t$$$. Printed schedule should consist of characters '0' and '1' only and the number of zeros should be equal to the number of zeros in $$$s$$$ and the number of ones should be equal to the number of ones in $$$s$$$. In case there multiple optimal schedules, print any of them. Note In the first example there are two occurrences, one starting from first position and one starting from fourth position. In the second example there is only one occurrence, which starts from third position. Note, that the answer is not unique. For example, if we move the first day (which is a day off) to the last position, the number of occurrences of $$$t$$$ wouldn't change. In the third example it's impossible to make even a single occurrence. | 1,600 | false | true | false | false | false | false | false | false | false | false | 5,041 |
553B | Let's define the permutation of length _n_ as an array _p_u2009=u2009[_p_1,u2009_p_2,u2009...,u2009_p__n_] consisting of _n_ distinct integers from range from 1 to _n_. We say that this permutation maps value 1 into the value _p_1, value 2 into the value _p_2 and so on. Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of _p_ as a collection of cycles forming _p_. For example, permutation _p_u2009=u2009[4,u20091,u20096,u20092,u20095,u20093] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place. Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4,u20091,u20096,u20092,u20095,u20093] is (421)(5)(63). Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4,u20091,u20096,u20092,u20095,u20093] will become [4,u20092,u20091,u20095,u20096,u20093]. Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length _n_ that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers _n_ and _k_, print the permutation that was _k_-th on Kyoya's list. Note The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1,u20092,u20093,u20094], while the second permutation would be [1,u20092,u20094,u20093]. | 1,900 | true | true | true | false | false | true | false | true | false | false | 7,621 |
1825A | LuoTianyi gives you a palindrome$$$^{dagger}$$$ string $$$s$$$, and she wants you to find out the length of the longest non-empty subsequence$$$^{ddagger}$$$ of $$$s$$$ which is not a palindrome string. If there is no such subsequence, output $$$-1$$$ instead. $$$^{dagger}$$$ A palindrome is a string that reads the same backward as forward. For example, strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not. $$$^{ddagger}$$$ A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from $$$b$$$. For example, strings "a", "aaa", "bab" are subsequences of string "abaab", but strings "codeforces", "bbb", "h" are not. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. The description of test cases follows. The first and the only line of each test case contains a single string $$$s$$$ ($$$1 le s le 50$$$) consisting of lowercase English letters — the string that LuoTianyi gives you. It's guaranteed that $$$s$$$ is a palindrome string. Output For each test case, output a single integer — the length of the longest non-empty subsequence which is not a palindrome string. If there is no such subsequence, output $$$-1$$$. Example Input 4 abacaba aaa codeforcesecrofedoc lol Note In the first test case, "abcaba" is a subsequence of "abacaba" as we can delete the third letter of "abacaba" to get "abcaba", and "abcaba" is not a palindrome string. We can prove that "abcaba" is an example of the longest subsequences of "abacaba" that isn't palindrome, so that the answer is $$$6$$$. In the second test case, we can only get "a" and "aa", but they are all palindrome strings, so the answer is $$$-1$$$. | 800 | false | true | false | false | false | false | false | false | false | false | 1,331 |
1418C | You and your friend are playing the game Mortal Kombat XI. You are trying to pass a challenge tower. There are n bosses in this tower, numbered from 1 to n. The type of the i-th boss is ai. If the i-th boss is easy then its type is ai=0, otherwise this boss is hard and its type is ai=1. During one session, either you or your friend can kill one or two bosses (neither you nor your friend can skip the session, so the minimum number of bosses killed during one session is at least one). After your friend session, your session begins, then again your friend session begins, your session begins, and so on. The first session is your friend's session. Your friend needs to get good because he can't actually kill hard bosses. To kill them, he uses skip points. One skip point can be used to kill one hard boss. Your task is to find the minimum number of skip points your friend needs to use so you and your friend kill all n bosses in the given order. For example: suppose n=8, a=[1,0,1,1,0,1,1,1]. Then the best course of action is the following: your friend kills two first bosses, using one skip point for the first boss; you kill the third and the fourth bosses; your friend kills the fifth boss; you kill the sixth and the seventh bosses; your friend kills the last boss, using one skip point, so the tower is completed using two skip points. You have to answer t independent test cases. Input The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1≤n≤2⋅105) — the number of bosses. The second line of the test case contains n integers a1,a2,…,an (0≤ai≤1), where ai is the type of the i-th boss. It is guaranteed that the sum of n does not exceed 2⋅105 (∑n≤2⋅105). | 1,500 | false | true | false | true | false | false | false | false | false | true | 3,592 |
1534C | When he's not training for IOI, Little Alawn enjoys playing with puzzles of various types to stimulate his brain. Today, he's playing with a puzzle that consists of a $$$2 imes n$$$ grid where each row is a permutation of the numbers $$$1,2,3,ldots,n$$$. The goal of Little Alawn's puzzle is to make sure no numbers on the same column or row are the same (we'll call this state of the puzzle as solved), and to achieve this he is able to swap the numbers in any column. However, after solving the puzzle many times, Little Alawn got bored and began wondering about the number of possible solved configurations of the puzzle he could achieve from an initial solved configuration only by swapping numbers in a column. Unfortunately, Little Alawn got stuck while trying to solve this harder problem, so he was wondering if you could help him with it. Find the answer modulo $$$10^9+7$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 4 cdot 10^5$$$). The next two lines of each test case describe the initial state of the puzzle grid. Each line will be a permutation of the numbers $$$1,2,3,ldots,n$$$ and the numbers in each column and row will be pairwise distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$4 cdot 10^5$$$. Output For each test case output a single integer, the number of possible solved configurations of the puzzle Little Alawn can achieve from an initial solved configuration only by swapping numbers in a column. As the answer can be very large, please output it modulo $$$10^9+7$$$. The answer for each test case should be on a separate line. Example Input 2 4 1 4 2 3 3 2 1 4 8 2 6 5 1 4 3 7 8 3 8 7 5 1 2 4 6 Note The two possible puzzle configurations for example $$$1$$$ are: $$$[1,4,2,3]$$$ in the first row and $$$[3,2,1,4]$$$ in the second; $$$[3,2,1,4]$$$ in the first row and $$$[1,4,2,3]$$$ in the second. | 1,300 | true | false | false | true | false | false | false | false | false | true | 2,981 |
1119H | You have received your birthday gifts — $$$n$$$ triples of integers! The $$$i$$$-th of them is $$$lbrace a_{i}, b_{i}, c_{i} brace$$$. All numbers are greater than or equal to $$$0$$$, and strictly smaller than $$$2^{k}$$$, where $$$k$$$ is a fixed integer. One day, you felt tired playing with triples. So you came up with three new integers $$$x$$$, $$$y$$$, $$$z$$$, and then formed $$$n$$$ arrays. The $$$i$$$-th array consists of $$$a_i$$$ repeated $$$x$$$ times, $$$b_i$$$ repeated $$$y$$$ times and $$$c_i$$$ repeated $$$z$$$ times. Thus, each array has length $$$(x + y + z)$$$. You want to choose exactly one integer from each array such that the XOR (bitwise exclusive or) of them is equal to $$$t$$$. Output the number of ways to choose the numbers for each $$$t$$$ between $$$0$$$ and $$$2^{k} - 1$$$, inclusive, modulo $$$998244353$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 10^{5}$$$, $$$1 leq k leq 17$$$)xa0— the number of arrays and the binary length of all numbers. The second line contains three integers $$$x$$$, $$$y$$$, $$$z$$$ ($$$0 leq x,y,z leq 10^{9}$$$)xa0— the integers you chose. Then $$$n$$$ lines follow. The $$$i$$$-th of them contains three integers $$$a_{i}$$$, $$$b_{i}$$$ and $$$c_{i}$$$ ($$$0 leq a_{i} , b_{i} , c_{i} leq 2^{k} - 1$$$)xa0— the integers forming the $$$i$$$-th array. Output Print a single line containing $$$2^{k}$$$ integers. The $$$i$$$-th of them should be the number of ways to choose exactly one integer from each array so that their XOR is equal to $$$t = i-1$$$ modulo $$$998244353$$$. Examples Input 2 2 1 2 1 0 1 2 1 2 3 Input 4 3 1 2 3 1 3 7 0 2 5 1 0 6 3 3 2 Output 198 198 126 126 126 126 198 198 Note In the first example, the array we formed is $$$(1, 0, 0, 1, 1, 1)$$$, we have two choices to get $$$0$$$ as the XOR and four choices to get $$$1$$$. In the second example, two arrays are $$$(0, 1, 1, 2)$$$ and $$$(1, 2, 2, 3)$$$. There are sixteen $$$(4 cdot 4)$$$ choices in total, $$$4$$$ of them ($$$1 oplus 1$$$ and $$$2 oplus 2$$$, two options for each) give $$$0$$$, $$$2$$$ of them ($$$0 oplus 1$$$ and $$$2 oplus 3$$$) give $$$1$$$, $$$4$$$ of them ($$$0 oplus 2$$$ and $$$1 oplus 3$$$, two options for each) give $$$2$$$, and finally $$$6$$$ of them ($$$0 oplus 3$$$, $$$2 oplus 1$$$ and four options for $$$1 oplus 2$$$) give $$$3$$$. | 3,200 | true | false | false | false | false | false | false | false | false | false | 5,087 |
677D | Vanya is in the palace that can be represented as a grid _n_u2009×u2009_m_. Each room contains a single chest, an the room located in the _i_-th row and _j_-th columns contains the chest of type _a__ij_. Each chest of type _x_u2009≤u2009_p_u2009-u20091 contains a key that can open any chest of type _x_u2009+u20091, and all chests of type 1 are not locked. There is exactly one chest of type _p_ and it contains a treasure. Vanya starts in cell (1,u20091) (top left corner). What is the minimum total distance Vanya has to walk in order to get the treasure? Consider the distance between cell (_r_1,u2009_c_1) (the cell in the row _r_1 and column _c_1) and (_r_2,u2009_c_2) is equal to _r_1u2009-u2009_r_2u2009+u2009_c_1u2009-u2009_c_2. Input The first line of the input contains three integers _n_, _m_ and _p_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009300,u20091u2009≤u2009_p_u2009≤u2009_n_·_m_)xa0— the number of rows and columns in the table representing the palace and the number of different types of the chests, respectively. Each of the following _n_ lines contains _m_ integers _a__ij_ (1u2009≤u2009_a__ij_u2009≤u2009_p_)xa0— the types of the chests in corresponding rooms. It's guaranteed that for each _x_ from 1 to _p_ there is at least one chest of this type (that is, there exists a pair of _r_ and _c_, such that _a__rc_u2009=u2009_x_). Also, it's guaranteed that there is exactly one chest of type _p_. Output Print one integerxa0— the minimum possible total distance Vanya has to walk in order to get the treasure from the chest of type _p_. Examples Input 3 4 3 2 1 1 1 1 1 1 1 2 1 1 3 Input 3 3 9 1 3 5 8 9 7 4 6 2 Input 3 4 12 1 2 3 4 8 7 6 5 9 10 11 12 | 2,300 | false | false | false | true | true | false | false | false | false | true | 7,118 |
1092A | You are given two integers $$$n$$$ and $$$k$$$. Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any. You have to answer $$$t$$$ independent queries. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 le n_i le 100, 1 le k_i le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query. Output Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query. Example Output cbcacab abcd baabab Note In the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct). In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$). In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$). | 800 | false | false | true | false | false | false | false | false | false | false | 5,251 |
293C | Yaroslav, Andrey and Roman love playing cubes. Sometimes they get together and play cubes for hours and hours! Today they got together again and they are playing cubes. Yaroslav took unit cubes and composed them into an _a_u2009×u2009_a_u2009×u2009_a_ cube, Andrey made a _b_u2009×u2009_b_u2009×u2009_b_ cube and Roman made a _c_u2009×u2009_c_u2009×u2009_c_ cube. After that the game was finished and the guys left. But later, Vitaly entered the room. He saw the cubes and wanted to make a cube as well. But what size should the cube be? Of course it should be a large cube with the side of length _a_u2009+u2009_b_u2009+u2009_c_. Besides, Vitaly decided to decompose the cubes built by Yaroslav, Andrey and Roman and compose his own large cube out of them. However, it turned out that the unit cubes he got from destroying the three cubes just weren't enough to make a large cube. We know that Vitaly was short of exactly _n_ cubes. Vitaly got upset, demolished everything and left. As he was leaving, he met Petya and told him that there had been three cubes in the room and that he needed another _n_ unit cubes to make his own large cube. Petya entered the room and saw the messily scattered cubes. He wanted to make it neat and orderly again. But he only knows that there had been three cubes, made of small unit cubes and that Vitaly needed _n_ more unit cubes to make a large one! Help Petya understand, how many ways of sizes _a_, _b_, _c_ are there to restore Yaroslav's, Andrey's and Roman's cubes. Input The single line of the input contains integer _n_ (1u2009≤u2009_n_u2009≤u20091014). We know that all numbers _a_, _b_, _c_ are positive integers. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Output In the single line print the required number of ways. If it turns out that there isn't a single way of suitable sizes of _a_, _b_, _c_, print 0. | 2,400 | true | false | false | false | false | false | true | false | false | false | 8,659 |
152E | Vasya has a very beautiful country garden that can be represented as an _n_u2009×u2009_m_ rectangular field divided into _n_·_m_ squares. One beautiful day Vasya remembered that he needs to pave roads between _k_ important squares that contain buildings. To pave a road, he can cover some squares of his garden with concrete. For each garden square we know number _a__i__j_ that represents the number of flowers that grow in the square with coordinates (_i_,u2009_j_). When a square is covered with concrete, all flowers that grow in the square die. Vasya wants to cover some squares with concrete so that the following conditions were fulfilled: all _k_ important squares should necessarily be covered with concrete from each important square there should be a way to any other important square. The way should go be paved with concrete-covered squares considering that neighboring squares are squares that have a common side the total number of dead plants should be minimum As Vasya has a rather large garden, he asks you to help him. Input The first input line contains three integers _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100, _n_·_m_u2009≤u2009200, 1u2009≤u2009_k_u2009≤u2009_min_(_n_·_m_,u20097)) — the garden's sizes and the number of the important squares. Each of the next _n_ lines contains _m_ numbers _a__i__j_ (1u2009≤u2009_a__i__j_u2009≤u20091000) — the numbers of flowers in the squares. Next _k_ lines contain coordinates of important squares written as "_x_ _y_" (without quotes) (1u2009≤u2009_x_u2009≤u2009_n_, 1u2009≤u2009_y_u2009≤u2009_m_). The numbers written on one line are separated by spaces. It is guaranteed that all _k_ important squares have different coordinates. Output In the first line print the single integer — the minimum number of plants that die during the road construction. Then print _n_ lines each containing _m_ characters — the garden's plan. In this plan use character "X" (uppercase Latin letter X) to represent a concrete-covered square and use character "." (dot) for a square that isn't covered with concrete. If there are multiple solutions, print any of them. Examples Input 3 3 2 1 2 3 1 2 3 1 2 3 1 2 3 3 Input 4 5 4 1 4 5 1 2 2 2 2 2 7 2 4 1 4 5 3 2 1 7 1 1 1 1 5 4 1 4 4 Output 26 X..XX XXXX. X.X.. X.XX. | 2,500 | false | false | false | true | false | false | false | false | false | true | 9,265 |
1854F | Mark loves to move fast. So he made a spaceship that works in $$$4$$$-dimensional space. He wants to use the spaceship to complete missions as fast as possible. In each mission, the spaceship starts at $$$(0, 0, 0, 0)$$$ and needs to end up at $$$(a, b, c, d)$$$. To do this, he instructs the spaceship's computer to execute a series of moves, where each move is a unit step in one of the eight cardinal directions: $$$(pm 1, 0, 0, 0)$$$, $$$(0, pm 1, 0, 0)$$$, $$$(0, 0, pm 1, 0)$$$, $$$(0, 0, 0, pm 1)$$$. Unfortunately, he also moved fast when building the spaceship, so there is a bug in the spaceship's code. The first move will be executed once, the second move will be executed twice, the third move will be executed thrice, and so on. In general, the $$$i$$$-th move will be executed $$$i$$$ times. For any four integers $$$a, b, c, d$$$, let $$$f(a, b, c, d)$$$ be the minimum number of moves of a mission that ends up at $$$(a, b, c, d)$$$. Compute the sum of $$$f(a, b, c, d)$$$ over all points (with integer coordinates) such that $$$-Ale ale A$$$, $$$-Ble ble B$$$, $$$-Cle cle C$$$, $$$-Dle dle D$$$. Input The only line of the input contains the four integers $$$A, B, C, D$$$ ($$$0le A,B,C,Dle 1000$$$). Output Print the sum of $$$f(a, b, c, d)$$$ over the set of points described in the statement. Note In the first sample, one has to compute $$$f(-1, 0, 0, 0)+f(0, 0, 0, 0) + f(1, 0, 0, 0) = 1 + 0 + 1 = 2$$$. In the second sample, one has to compute the sum of $$$f(a, b, c, d)$$$ over $$$27$$$ different points $$$(a, b, c, d)$$$. Let us describe the value of $$$f(a, b, c, d)$$$ for some of them: It holds $$$f(-1, 0, 0, -1)=3$$$ and it may be achieved with the following sequence of moves (an arrow $$$xrightarrow{pm i}$$$ denotes that the move is performed on the $$$i$$$-th coordinate): $$$$$$(0, 0, 0, 0) xrightarrow{-1} (-1, 0, 0, 0) xrightarrow{+4} (-1, 0, 0, 2) xrightarrow{-4} (-1, 0, 0, -1).$$$$$$ It holds $$$f(1, 1, 0, 1) = 5$$$ and it may be achieved with the following sequence of moves: $$$$$$(0, 0, 0, 0) xrightarrow{+1} (1, 0, 0, 0) xrightarrow{-2} (1, -2, 0, 0) xrightarrow{+2} (1, 1, 0, 0) xrightarrow{-4} (1, 1, 0, -4) xrightarrow{+4} (1, 1, 0, 1).$$$$$$ In the third sample, one has to compute the sum of $$$f(a, b, c, d)$$$ over $$$7cdot5cdot 9cdot 3$$$ points. One of them is $$$(3, 2, 4, 1)$$$. It holds $$$f(3, 2, 4, 1) = 4$$$ and it may be achieved with the following sequence of moves: $$$$$$(0, 0, 0, 0) xrightarrow{+4} (0, 0, 0, 1) xrightarrow{+2} (0, 2, 0, 1) xrightarrow{+1} (3, 2, 0, 1) xrightarrow{+3} (3, 2, 4, 1).$$$$$$ | 3,500 | false | false | false | true | false | false | true | false | false | false | 1,153 |
1201B | Problem - 1201B - 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 math *1500 No tag edit access → Contest materials ") and decrease each of them by one. You need to check whether it is possible to make all the elements equal to zero or not. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0— the size of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the elements of the array. Output Print "YES" if it is possible to make all elements zero, otherwise print "NO". Examples Input 4 1 1 2 2 Output YES Input 6 1 2 3 4 5 6 Output NO Note In the first example, you can make all elements equal to zero in $$$3$$$ operations: Decrease $$$a_1$$$ and $$$a_2$$$, Decrease $$$a_3$$$ and $$$a_4$$$, Decrease $$$a_3$$$ and $$$a_4$$$ In the second example, one can show that it is impossible to make all elements equal to zero. | 1,500 | true | true | false | false | false | false | false | false | false | false | 4,694 |
1912B | An aircraft manufacturing company wants to optimize their products for passenger airlines. The company's latest research shows that most of the delays happen because of slow boarding. Most of the medium-sized aircraft are designed with 3-3 seat layout, meaning each row has 6 seats: 3 seats on the left side, a single aisle, and 3 seats on the right side. At each of the left and right sides there is a window seat, a middle seat, and an aisle seat. A passenger that boards an aircraft assigned to an aisle seat takes significantly less time than a passenger assigned to a window seat even when there is no one else in the aircraft. The company decided to compute an inconvenience of a layout as the total sum of distances from each of the seats of a single row to the closest aisle. The distance from a seat to an aisle is the number of seats between them. For a 3-3 layout, a window seat has a distance of 2, a middle seatxa0— 1, and an aisle seatxa0— 0. The inconvenience of a 3-3 layout is $$$(2+1+0)+(0+1+2)=6$$$. The inconvenience of a 3-5-3 layout is $$$(2+1+0)+(0+1+2+1+0)+(0+1+2)=10$$$. Formally, a layout is a sequence of positive integers $$$a_1, a_2, ldots, a_{k+1}$$$xa0— group $$$i$$$ having $$$a_i$$$ seats, with $$$k$$$ aisles between groups, the $$$i$$$-th aisle being between groups $$$i$$$ and $$$i+1$$$. This means that in a layout each aisle must always be between two seats, so no aisle can be next to a window, and no two aisles can be next to each other. The company decided to design a layout with a row of $$$n$$$ seats, $$$k$$$ aisles and having the minimum inconvenience possible. Help them find the minimum inconvenience among all layouts of $$$n$$$ seats and $$$k$$$ aisles, and count the number of such layouts modulo $$$998,244,353$$$. Input The first line contains an integer $$$t$$$xa0— the number of test cases you need to solve ($$$1 le t le 10^5$$$). For each of the test cases, there is a single line containing $$$n$$$ and $$$k$$$xa0— the number of seats, and the number of aisles in a row ($$$2 le n le 10^9$$$; $$$1 le k le 10^5$$$; $$$k < n$$$). The total sum of $$$k$$$ in all $$$t$$$ given test cases does not exceed $$$10^6$$$. Output For each test case print two integersxa0— the minimum inconvenience among all possible layouts, and the number of layouts with the minimum inconvenience modulo $$$998,244,353$$$. Note In the last test case of 9 2 the possible layouts with the minimum inconvenience of 6 are 3-4-2, 2-4-3, and 2-5-2. | 2,100 | true | false | false | false | false | false | false | false | false | false | 836 |
1999G2 | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 964 (Div. 4) 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 interactive ternary search *1700 No tag edit access → Contest materials Announcement (en) Discussion stream (en) Video Tutorial (en) Tutorial #2 (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST G2. Ruler (hard version) time limit per test1 second memory limit per test256 megabytes This is the hard version of the problem. The only difference between the two versions is that in this version, you can make at most $$$mathbf{7}$$$ queries. This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read the guide for participants. We have a secret ruler that is missing one number $$$x$$$ ($$$2 leq x leq 999$$$). When you measure an object of length $$$y$$$, the ruler reports the following values: If $$$y < x$$$, the ruler (correctly) measures the object as having length $$$y$$$. If $$$y geq x$$$, the ruler incorrectly measures the object as having length $$$y+1$$$. The ruler above is missing the number $$$4$$$, so it correctly measures the first segment as length $$$3$$$ but incorrectly measures the second segment as length $$$6$$$ even though it is actually $$$5$$$. You need to find the value of $$$x$$$. To do that, you can make queries of the following form: $$$ exttt{?}~a~b$$$xa0— in response, we will measure the side lengths of an $$$a imes b$$$ rectangle with our ruler and multiply the results, reporting the measured area of the rectangle back to you. For example, if $$$x=4$$$ and you query a $$$3 imes 5$$$ rectangle, we will measure its side lengths as $$$3 imes 6$$$ and report $$$18$$$ back to you. Find the value of $$$x$$$. You can ask at most $$$mathbf{7}$$$ queries. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. Interaction There is no initial input for each test case. You should begin the interaction by asking a query. To make a query, output a single line of the form $$$ exttt{?}~a~b$$$ ($$$1 leq a, b leq 1000$$$). In response, you will be told the measured area of the rectangle, according to our secret ruler. When you are ready to print the answer, output a single line of the form $$$ exttt{!}~x$$$ ($$$2 leq x leq 999$$$). After that, proceed to process the next test case or terminate the program if it was the last test case. Printing the answer does not count as a query. The interactor is not adaptive, meaning that the answer is known before the participant asks the queries and doesn't depend on the queries asked by the participant. If your program makes more than $$$7$$$ queries for one set of input data, makes an invalid query, or prints the wrong value of $$$x$$$, then the response to the query will be $$$-1$$$. After receiving such a response, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. After printing a query do not forget to output the end of line and flush the output. Otherwise, you may get Idleness limit exceeded verdict. 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 the documentation for other languages. Hacks To make a hack, use the following format. The first line should contain a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The only line of each test case should contain a single integer $$$x$$$ ($$$2 leq x leq 999$$$)xa0— the missing number on the ruler. Example input 2 18 25 9999 output ? 3 5 ? 4 4 ! 4 ? 99 100 ! 100 Note In the first test, the interaction proceeds as follows. Solution Jury Explanation $$$ exttt{2}$$$ There are 2 test cases. $$$ exttt{? 3 5}$$$ $$$ exttt{18}$$$ Secretly, the jury picked $$$x=4$$$. The solution requests the $$$3 imes 5$$$ rectangle, and the jury responds with $$$3 imes 6 = 18$$$, as described in the statement. $$$ exttt{? 4 4}$$$ $$$ exttt{25}$$$ The solution requests the $$$4 imes 4$$$ rectangle, which the jury measures as $$$5 imes 5$$$ and responds with $$$25$$$. $$$ exttt{! 4}$$$ The solution has somehow determined that $$$x=4$$$, and outputs it. Since the output is correct, the jury continues to the next test case. $$$ exttt{? 99 100}$$$ $$$ exttt{1}$$$ Secretly, the jury picked $$$x=100$$$. The solution requests the $$$99 imes 100$$$ rectangle, which the jury measures as $$$99 imes 101$$$ and responds with $$$9999$$$. $$$ exttt{! 100}$$$ The solution has somehow determined that $$$x=100$$$, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit. Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction. Codeforces (c) | 1,700 | false | false | false | false | false | false | false | true | false | false | 264 |
487A | A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (_HP_), offensive power (_ATK_) and defensive power (_DEF_). During the battle, every second the monster's HP decrease by _max_(0,u2009_ATK__Y_u2009-u2009_DEF__M_), while Yang's HP decreases by _max_(0,u2009_ATK__M_u2009-u2009_DEF__Y_), where index _Y_ denotes Master Yang and index _M_ denotes monster. Both decreases happen simultaneously Once monster's _HP_u2009≤u20090 and the same time Master Yang's _HP_u2009>u20090, Master Yang wins. Master Yang can buy attributes from the magic shop of Cyberland: _h_ bitcoins per _HP_, _a_ bitcoins per _ATK_, and _d_ bitcoins per _DEF_. Now Master Yang wants to know the minimum number of bitcoins he can spend in order to win. Input The first line contains three integers _HP__Y_,u2009_ATK__Y_,u2009_DEF__Y_, separated by a space, denoting the initial _HP_, _ATK_ and _DEF_ of Master Yang. The second line contains three integers _HP__M_,u2009_ATK__M_,u2009_DEF__M_, separated by a space, denoting the _HP_, _ATK_ and _DEF_ of the monster. The third line contains three integers _h_,u2009_a_,u2009_d_, separated by a space, denoting the price of 1xa0_HP_, 1xa0_ATK_ and 1xa0_DEF_. All numbers in input are integer and lie between 1 and 100 inclusively. Output The only output line should contain an integer, denoting the minimum bitcoins Master Yang should spend in order to win. Examples Input 1 2 1 1 100 1 1 100 100 Input 100 100 100 1 1 1 1 1 1 Note For the first sample, prices for _ATK_ and _DEF_ are extremely high. Master Yang can buy 99 HP, then he can beat the monster with 1 HP left. For the second sample, Master Yang is strong enough to beat the monster, so he doesn't need to buy anything. | 1,800 | false | false | true | false | false | false | true | true | false | false | 7,890 |
1407D | There are $$$n$$$ beautiful skyscrapers in New York, the height of the $$$i$$$-th one is $$$h_i$$$. Today some villains have set on fire first $$$n - 1$$$ of them, and now the only safety building is $$$n$$$-th skyscraper. Let's call a jump from $$$i$$$-th skyscraper to $$$j$$$-th ($$$i < j$$$) discrete, if all skyscrapers between are strictly lower or higher than both of them. Formally, jump is discrete, if $$$i < j$$$ and one of the following conditions satisfied: $$$i + 1 = j$$$ $$$max(h_{i + 1}, ldots, h_{j - 1}) < min(h_i, h_j)$$$ $$$max(h_i, h_j) < min(h_{i + 1}, ldots, h_{j - 1})$$$. At the moment, Vasya is staying on the first skyscraper and wants to live a little longer, so his goal is to reach $$$n$$$-th skyscraper with minimal count of discrete jumps. Help him with calcualting this number. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 3 cdot 10^5$$$) — total amount of skyscrapers. The second line contains $$$n$$$ integers $$$h_1, h_2, ldots, h_n$$$ ($$$1 le h_i le 10^9$$$) — heights of skyscrapers. Output Print single number $$$k$$$ — minimal amount of discrete jumps. We can show that an answer always exists. Note In the first testcase, Vasya can jump in the following way: $$$1 ightarrow 2 ightarrow 4 ightarrow 5$$$. In the second and third testcases, we can reach last skyscraper in one jump. Sequence of jumps in the fourth testcase: $$$1 ightarrow 3 ightarrow 5$$$. | 2,200 | false | false | false | true | true | false | false | false | false | true | 3,638 |
1065D | You stumbled upon a new kind of chess puzzles. The chessboard you are given is not necesserily $$$8 imes 8$$$, but it still is $$$N imes N$$$. Each square has some number written on it, all the numbers are from $$$1$$$ to $$$N^2$$$ and all the numbers are pairwise distinct. The $$$j$$$-th square in the $$$i$$$-th row has a number $$$A_{ij}$$$ written on it. In your chess set you have only three pieces: a knight, a bishop and a rook. At first, you put one of them on the square with the number $$$1$$$ (you can choose which one). Then you want to reach square $$$2$$$ (possibly passing through some other squares in process), then square $$$3$$$ and so on until you reach square $$$N^2$$$. In one step you are allowed to either make a valid move with the current piece or replace it with some other piece. Each square can be visited arbitrary number of times. A knight can move to a square that is two squares away horizontally and one square vertically, or two squares vertically and one square horizontally. A bishop moves diagonally. A rook moves horizontally or vertically. The move should be performed to a different square from the one a piece is currently standing on. You want to minimize the number of steps of the whole traversal. Among all the paths to have the same number of steps you want to choose the one with the lowest number of piece replacements. What is the path you should take to satisfy all conditions? Input The first line contains a single integer $$$N$$$ ($$$3 le N le 10$$$) — the size of the chessboard. Each of the next $$$N$$$ lines contains $$$N$$$ integers $$$A_{i1}, A_{i2}, dots, A_{iN}$$$ ($$$1 le A_{ij} le N^2$$$) — the numbers written on the squares of the $$$i$$$-th row of the board. It is guaranteed that all $$$A_{ij}$$$ are pairwise distinct. Output The only line should contain two integers — the number of steps in the best answer and the number of replacement moves in it. Note Here are the steps for the first example (the starting piece is a knight): 1. Move to $$$(3, 2)$$$ 2. Move to $$$(1, 3)$$$ 3. Move to $$$(3, 2)$$$ 4. Replace the knight with a rook 5. Move to $$$(3, 1)$$$ 6. Move to $$$(3, 3)$$$ 7. Move to $$$(3, 2)$$$ 8. Move to $$$(2, 2)$$$ 9. Move to $$$(2, 3)$$$ 10. Move to $$$(2, 1)$$$ 11. Move to $$$(1, 1)$$$ 12. Move to $$$(1, 2)$$$ | 2,200 | false | false | false | true | false | false | false | false | false | false | 5,385 |
1163E | Kuro has just learned about permutations and he is really excited to create a new permutation type. He has chosen $$$n$$$ distinct positive integers and put all of them in a set $$$S$$$. Now he defines a magical permutation to be: A permutation of integers from $$$0$$$ to $$$2^x - 1$$$, where $$$x$$$ is a non-negative integer. The xa0— the number of elements in the set $$$S$$$. The next line contains $$$n$$$ distinct integers $$$S_1, S_2, ldots, S_n$$$ ($$$1 leq S_i leq 2 cdot 10^5$$$)xa0— the elements in the set $$$S$$$. Output In the first line print the largest non-negative integer $$$x$$$, such that there is a magical permutation of integers from $$$0$$$ to $$$2^x - 1$$$. Then print $$$2^x$$$ integers describing a magical permutation of integers from $$$0$$$ to $$$2^x - 1$$$. If there are multiple such magical permutations, print any of them. Note In the first example, $$$0, 1, 3, 2$$$ is a magical permutation since: $$$0 oplus 1 = 1 in S$$$ $$$1 oplus 3 = 2 in S$$$ $$$3 oplus 2 = 1 in S$$$ Where $$$oplus$$$ denotes [bitwise xor]( operation. | 2,400 | true | false | false | false | true | true | true | false | false | true | 4,898 |
915G | Problem - 915G - 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 number theory *2300 No tag edit access → Contest materials u2009=u20091, where _gcd_ is the greatest common divisor of the arguments. You are given two numbers _n_ and _k_. For each _i_ (1u2009≤u2009_i_u2009≤u2009_k_) you have to determine the number of coprime arrays _a_ of size _n_ such that for every _j_ (1u2009≤u2009_j_u2009≤u2009_n_) 1u2009≤u2009_a__j_u2009≤u2009_i_. Since the answers can be very large, you have to calculate them modulo 109u2009+u20097. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u20092·106) — the size of the desired arrays and the maximum upper bound on elements, respectively. Output Since printing 2·106 numbers may take a lot of time, you have to output the answer in such a way: Let _b__i_ be the number of coprime arrays with elements in range | 2,300 | true | false | false | false | false | false | false | false | false | false | 6,069 |
283D | Problem - 283D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp math number theory *2400 No tag edit access → Contest materials of positive integers is "cool" if _x_ can be expressed as the sum of _y_ consecutive integers (not necessarily positive). A sequence (_a_1,u2009_a_2,u2009...,u2009_a__n_) is "cool" if the pairs (_a_1,u2009_a_2),u2009(_a_2,u2009_a_3),u2009...,u2009(_a__n_u2009-u20091,u2009_a__n_) are all cool. The cows have a sequence of _n_ positive integers, _a_1,u2009_a_2,u2009...,u2009_a__n_. In one move, they may replace some _a__i_ with any other positive integer (there are no other limits on the new value of _a__i_). Determine the smallest number of moves needed to make the resulting sequence cool. Input The first line contains a single integer, _n_ (2u2009≤u2009_n_u2009≤u20095000). The next line contains _n_ space-separated integers, _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20091015). Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Output A single integer, the minimum number of _a__i_ that must be changed to make the sequence cool. Examples Input 3 6 4 1 Output 0 Input 4 20 6 3 4 Output 2 Note In the first sample, the sequence is already cool, so we don't need to change any elements. In the second sample, we can change _a_2 to 5 and _a_3 to 10 to make (20, 5, 10, 4) which is cool. This changes 2 elements. | 2,400 | true | false | false | true | false | false | false | false | false | false | 8,700 |
1919D | There is an edge-weighted complete binary tree with $$$n$$$ leaves. A complete binary tree is defined as a tree where every non-leaf vertex has exactly 2 children. For each non-leaf vertex, we label one of its children as the left child and the other as the right child. The binary tree has a very strange property. For every non-leaf vertex, one of the edges to its children has weight $$$0$$$ while the other edge has weight $$$1$$$. Note that the edge with weight $$$0$$$ can be connected to either its left or right child. You forgot what the tree looks like, but luckily, you still remember some information about the leaves in the form of an array $$$a$$$ of size $$$n$$$. For each $$$i$$$ from $$$1$$$ to $$$n$$$, $$$a_i$$$ represents the distance$$$^dagger$$$ from the root to the $$$i$$$-th leaf in dfs order$$$^ddagger$$$. Determine whether there exists a complete binary tree which satisfies array $$$a$$$. Note that you do not need to reconstruct the tree. $$$^dagger$$$ The distance from vertex $$$u$$$ to vertex $$$v$$$ is defined as the sum of weights of the edges on the path from vertex $$$u$$$ to vertex $$$v$$$. $$$^ddagger$$$ The dfs order of the leaves is found by calling the following $$$ exttt{dfs}$$$ function on the root of the binary tree. dfs_order = []function dfs(v): if v is leaf: append v to the back of dfs_order else: dfs(left child of v) dfs(right child of v) dfs(root) Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2cdot 10^5$$$)xa0— the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le n - 1$$$)xa0— the distance from the root to the $$$i$$$-th leaf. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, print "YES" if there exists a complete binary tree which satisfies array $$$a$$$ and "NO" otherwise. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer). Example Input 2 5 2 1 0 1 1 5 1 0 2 1 3 Note In the first test case, the following tree satisfies the array. In the second test case, it can be proven that there is no complete binary tree that satisfies the array. | 2,100 | false | true | false | false | true | true | false | false | true | false | 778 |
28D | A motorcade of _n_ trucks, driving from city «Z» to city «З», has approached a tunnel, known as Tunnel of Horror. Among truck drivers there were rumours about monster DravDe, who hunts for drivers in that tunnel. Some drivers fear to go first, others - to be the last, but let's consider the general case. Each truck is described with four numbers: _v_ — value of the truck, of its passangers and cargo _c_ — amount of passanger on the truck, the driver included _l_ — total amount of people that should go into the tunnel before this truck, so that the driver can overcome his fear («if the monster appears in front of the motorcade, he'll eat them first») _r_ — total amount of people that should follow this truck, so that the driver can overcome his fear («if the monster appears behind the motorcade, he'll eat them first»). Since the road is narrow, it's impossible to escape DravDe, if he appears from one side. Moreover, the motorcade can't be rearranged. The order of the trucks can't be changed, but it's possible to take any truck out of the motorcade, and leave it near the tunnel for an indefinite period. You, as the head of the motorcade, should remove some of the trucks so, that the rest of the motorcade can move into the tunnel and the total amount of the left trucks' values is maximal. Input The first input line contains integer number _n_ (1u2009≤u2009_n_u2009≤u2009105) — amount of trucks in the motorcade. The following _n_ lines contain four integers each. Numbers in the _i_-th line: _v__i_,u2009_c__i_,u2009_l__i_,u2009_r__i_ (1u2009≤u2009_v__i_u2009≤u2009104,u20091u2009≤u2009_c__i_u2009≤u2009105,u20090u2009≤u2009_l__i_,u2009_r__i_u2009≤u2009105) — describe the _i_-th truck. The trucks are numbered from 1, counting from the front of the motorcade. Output In the first line output number _k_ — amount of trucks that will drive into the tunnel. In the second line output _k_ numbers — indexes of these trucks in ascending order. Don't forget please that you are not allowed to change the order of trucks. If the answer is not unique, output any. Examples Input 5 1 1 0 3 1 1 1 2 1 1 2 1 1 1 3 0 2 1 3 0 Input 5 1 1 0 3 10 1 2 1 2 2 1 1 10 1 1 2 3 1 3 0 | 2,400 | false | false | false | true | true | false | false | true | false | false | 9,855 |
1687C | Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!! — Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly. The state of a robot can be represented by an array of integers of length $$$n$$$. Initially, the robot is at state $$$a$$$. She wishes to turn it into state $$$b$$$. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from $$$b$$$ and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of $$$a$$$ does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment $$$[l,r]$$$ and assign $$$a_i = b_i$$$ ($$$lle ile r$$$) if $$$sumlimits_{i=1}^n a_i$$$ does not change after the operation. Determine whether it is possible for Sanae to successfully turn the robot from the initial state $$$a$$$ to the desired state $$$b$$$ with any (possibly, zero) operations. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 2cdot 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 leq nleq 2cdot 10^5$$$, $$$1 leq mleq 2cdot 10^5$$$) — the length of $$$a$$$, $$$b$$$ and the number of segments. The second line contains $$$n$$$ intergers $$$a_1,a_2,ldots,a_n$$$ ($$$1 leq a_i leq 10^9$$$) — the initial state $$$a$$$. The third line contains $$$n$$$ intergers $$$b_1,b_2,ldots,b_n$$$ ($$$1 leq b_i leq 10^9$$$) — the desired state $$$b$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two intergers $$$l_i,r_i$$$ ($$$1 leq l_i < r_i leq n$$$) — the segments that can be copy-pasted by Sanae. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 cdot 10 ^ 5$$$. Output For each test case, print "YES" (without quotes) if $$$a$$$ can be turned into $$$b$$$, or "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). Example Input 2 5 2 1 5 4 2 3 3 2 5 4 1 1 3 2 5 5 2 1 5 4 2 3 3 2 4 5 1 1 2 2 4 Note Test case 1: One possible way of turning $$$a$$$ to $$$b$$$: First, select $$$[1,3]$$$. After the operation, $$$a=[3,2,5,2,3]$$$. Then, select $$$[2,5]$$$. After the operation, $$$a=[3,2,5,4,1]=b$$$. Test case 2: It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$. | 2,500 | false | true | false | false | true | false | true | true | true | false | 2,155 |
811B | Vladik had started reading a complicated book about algorithms containing _n_ pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation _P_u2009=u2009[_p_1,u2009_p_2,u2009...,u2009_p__n_], where _p__i_ denotes the number of page that should be read _i_-th in turn. Sometimes Vladik’s mom sorted some subsegment of permutation _P_ from position _l_ to position _r_ inclusive, because she loves the order. For every of such sorting Vladik knows number _x_xa0— what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has _p__x_ changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. Input First line contains two space-separated integers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009104)xa0— length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains _n_ space-separated integers _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009_n_)xa0— permutation _P_. Note that elements in permutation are distinct. Each of the next _m_ lines contains three space-separated integers _l__i_, _r__i_, _x__i_ (1u2009≤u2009_l__i_u2009≤u2009_x__i_u2009≤u2009_r__i_u2009≤u2009_n_)xa0— left and right borders of sorted subsegment in _i_-th sorting and position that is interesting to Vladik. Output For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. Examples Input 5 5 5 4 3 2 1 1 5 3 1 3 1 2 4 3 4 4 4 2 5 3 Input 6 5 1 4 3 2 5 6 2 4 3 1 6 2 4 5 4 1 3 3 2 6 3 Note Explanation of first test case: 1. [1,u20092,u20093,u20094,u20095]xa0— permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". 2. [3,u20094,u20095,u20092,u20091]xa0— permutation after sorting, 1-st element has changed, so answer is "No". 3. [5,u20092,u20093,u20094,u20091]xa0— permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". 4. [5,u20094,u20093,u20092,u20091]xa0— permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". 5. [5,u20091,u20092,u20093,u20094]xa0— permutation after sorting, 3-rd element has changed, so answer is "No". | 1,200 | false | false | true | false | false | false | false | false | true | false | 6,520 |
659E | Berland has _n_ cities connected by _m_ bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads. The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another). In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city. Help the Ministry of Transport to find the minimum possible number of separate cities after the reform. Input The first line of the input contains two positive integers, _n_ and _m_ — the number of the cities and the number of roads in Berland (2u2009≤u2009_n_u2009≤u2009100u2009000, 1u2009≤u2009_m_u2009≤u2009100u2009000). Next _m_ lines contain the descriptions of the roads: the _i_-th road is determined by two distinct integers _x__i_,u2009_y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_, _x__i_u2009≠u2009_y__i_), where _x__i_ and _y__i_ are the numbers of the cities connected by the _i_-th road. It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads. Output Print a single integerxa0— the minimum number of separated cities after the reform. Note In the first sample the following road orientation is allowed: , , . The second sample: , , , , . The third sample: , , , , . | 1,600 | false | true | false | false | true | false | false | false | false | true | 7,185 |
485B | Problem - 485B - 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 greedy *1300 No tag edit access → Contest materials . Each of the next _n_ lines contains a pair of integers _x__i_ and _y__i_xa0— the coordinates of the corresponding mine (u2009-u2009109u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009109). All points are pairwise distinct. Output Print the minimum area of the city that can cover all the mines with valuable resources. Examples Input 2 0 0 2 2 Output 4 Input 2 0 0 0 3 Output 9 | 1,300 | false | true | false | false | false | false | true | false | false | false | 7,896 |
1238E | You have a password which you often type — a string $$$s$$$ of length $$$n$$$. Every character of this string is one of the first $$$m$$$ lowercase Latin letters. Since you spend a lot of time typing it, you want to buy a new keyboard. A keyboard is a permutation of the first $$$m$$$ Latin letters. For example, if $$$m = 3$$$, then there are six possible keyboards: abc, acb, bac, bca, cab and cba. Since you type your password with one finger, you need to spend time moving your finger from one password character to the next. The time to move from character $$$s_i$$$ to character $$$s_{i+1}$$$ is equal to the distance between these characters on keyboard. The total time you have to spend typing the password with a keyboard is called the slowness of this keyboard. More formaly, the slowness of keyboard is equal to $$$sumlimits_{i=2}^{n} pos_{s_{i-1}} - pos_{s_i} $$$, where $$$pos_x$$$ is position of letter $$$x$$$ in keyboard. For example, if $$$s$$$ is aacabc and the keyboard is bac, then the total time of typing this password is $$$pos_a - pos_a + pos_a - pos_c + pos_c - pos_a + pos_a - pos_b + pos_b - pos_c$$$ = $$$2 - 2 + 2 - 3 + 3 - 2 + 2 - 1 + 1 - 3$$$ = $$$0 + 1 + 1 + 1 + 2 = 5$$$. Before buying a new keyboard you want to know the minimum possible slowness that the keyboard can have. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^5, 1 le m le 20$$$). The second line contains the string $$$s$$$ consisting of $$$n$$$ characters. Each character is one of the first $$$m$$$ Latin letters (lowercase). Note The first test case is considered in the statement. In the second test case the slowness of any keyboard is $$$0$$$. In the third test case one of the most suitable keyboards is bacd. | 2,200 | false | false | false | true | false | false | false | false | false | false | 4,499 |
1101C | There are $$$n$$$ segments $$$[l_i, r_i]$$$ for $$$1 le i le n$$$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. Input The first line contains one integer $$$T$$$ ($$$1 le T le 50000$$$) — the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $$$n$$$ ($$$2 le n le 10^5$$$) — number of segments. It is guaranteed that $$$sum{n}$$$ over all queries does not exceed $$$10^5$$$. The next $$$n$$$ lines contains two integers $$$l_i$$$, $$$r_i$$$ per line ($$$1 le l_i le r_i le 2 cdot 10^5$$$) — the $$$i$$$-th segment. Output For each query print $$$n$$$ integers $$$t_1, t_2, dots, t_n$$$ ($$$t_i in {1, 2}$$$) — for each segment (in the same order as in the input) $$$t_i$$$ equals $$$1$$$ if the $$$i$$$-th segment will belongs to the first group and $$$2$$$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $$$-1$$$. Example Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Note In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $$$-1$$$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $$$6$$$ possible is correct. | 1,500 | false | false | false | false | false | false | false | false | true | false | 5,198 |
106B | Vasya is choosing a laptop. The shop has _n_ laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number _n_ (1u2009≤u2009_n_u2009≤u2009100). Then follow _n_ lines. Each describes a laptop as _speed_ _ram_ _hdd_ _cost_. Besides, _speed_, _ram_, _hdd_ and _cost_ are integers 1000u2009≤u2009_speed_u2009≤u20094200 is the processor's speed in megahertz 256u2009≤u2009_ram_u2009≤u20094096 the RAM volume in megabytes 1u2009≤u2009_hdd_u2009≤u2009500 is the HDD in gigabytes 100u2009≤u2009_cost_u2009≤u20091000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to _n_ in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. | 1,000 | false | false | true | false | false | false | true | false | false | false | 9,460 |
514B | There are _n_ Imperial stormtroopers on the field. The battle field is a plane with Cartesian coordinate system. Each stormtrooper is associated with his coordinates (_x_,u2009_y_) on this plane. Han Solo has the newest duplex lazer gun to fight these stormtroopers. It is situated at the point (_x_0,u2009_y_0). In one shot it can can destroy all the stormtroopers, situated on some line that crosses point (_x_0,u2009_y_0). Your task is to determine what minimum number of shots Han Solo needs to defeat all the stormtroopers. The gun is the newest invention, it shoots very quickly and even after a very large number of shots the stormtroopers don't have enough time to realize what's happening and change their location. Input The first line contains three integers _n_, _x_0 и _y_0 (1u2009≤u2009_n_u2009≤u20091000, u2009-u2009104u2009≤u2009_x_0,u2009_y_0u2009≤u2009104) — the number of stormtroopers on the battle field and the coordinates of your gun. Next _n_ lines contain two integers each _x__i_, _y__i_ (u2009-u2009104u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009104) — the coordinates of the stormtroopers on the battlefield. It is guaranteed that no stormtrooper stands at the same point with the gun. Multiple stormtroopers can stand at the same point. Output Print a single integer — the minimum number of shots Han Solo needs to destroy all the stormtroopers. Note Explanation to the first and second samples from the statement, respectively: | 1,400 | true | false | true | false | true | false | true | false | false | false | 7,779 |
499B | You have a new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes. You know two languages, and the professor is giving the lecture in the first one. The words in both languages consist of lowercase English characters, each language consists of several words. For each language, all words are distinct, i.e. they are spelled differently. Moreover, the words of these languages have a one-to-one correspondence, that is, for each word in each language, there exists exactly one word in the other language having has the same meaning. You can write down every word the professor says in either the first language or the second language. Of course, during the lecture you write down each word in the language in which the word is shorter. In case of equal lengths of the corresponding words you prefer the word of the first language. You are given the text of the lecture the professor is going to read. Find out how the lecture will be recorded in your notes. Input The first line contains two integers, _n_ and _m_ (1u2009≤u2009_n_u2009≤u20093000, 1u2009≤u2009_m_u2009≤u20093000) — the number of words in the professor's lecture and the number of words in each of these languages. The following _m_ lines contain the words. The _i_-th line contains two strings _a__i_, _b__i_ meaning that the word _a__i_ belongs to the first language, the word _b__i_ belongs to the second language, and these two words have the same meaning. It is guaranteed that no word occurs in both languages, and each word occurs in its language exactly once. The next line contains _n_ space-separated strings _c_1,u2009_c_2,u2009...,u2009_c__n_ — the text of the lecture. It is guaranteed that each of the strings _c__i_ belongs to the set of strings {_a_1,u2009_a_2,u2009... _a__m_}. All the strings in the input are non-empty, each consisting of no more than 10 lowercase English letters. Output Output exactly _n_ words: how you will record the lecture in your notebook. Output the words of the lecture in the same order as in the input. Examples Input 4 3 codeforces codesecrof contest round letter message codeforces contest letter contest Output codeforces round letter round Input 5 3 joll wuqrd euzf un hbnyiyc rsoqqveh hbnyiyc joll joll euzf joll Output hbnyiyc joll joll un joll | 1,000 | false | false | true | false | false | false | false | false | false | false | 7,838 |
1788C | You are given an integer $$$n$$$. Pair the integers $$$1$$$ to $$$2n$$$ (i.e. each integer should be in exactly one pair) so that each sum of matched pairs is consecutive and distinct. Formally, let $$$(a_i, b_i)$$$ be the pairs that you matched. $$${a_1, b_1, a_2, b_2, ldots, a_n, b_n}$$$ should be a permutation of $$${1, 2, ldots, 2n}$$$. Let the sorted list of $$${a_1+b_1, a_2+b_2, ldots, a_n+b_n}$$$ be $$$s_1 < s_2 < ldots < s_n$$$. We must have $$$s_{i+1}-s_i = 1$$$ for $$$1 le i le n - 1$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. For each test case, a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$) is given. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Output For each test case, if it is impossible to make such a pairing, print "No". Otherwise, print "Yes" followed by $$$n$$$ lines. At each line, print two integers that are paired. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. If there are multiple solutions, print any. Example Output Yes 1 2 No Yes 1 6 3 5 4 2 No Note For the third test case, each integer from $$$1$$$ to $$$6$$$ appears once. The sums of matched pairs are $$$4+2=6$$$, $$$1+6=7$$$, and $$$3+5=8$$$, which are consecutive and distinct. | 1,300 | true | true | false | false | false | true | false | false | false | false | 1,552 |
813A | Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place! This contest consists of _n_ problems, and Pasha solves _i_th problem in _a__i_ time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment. Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during _m_ time periods, _j_th period is represented by its starting moment _l__j_ and ending moment _r__j_. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment _T_ iff there exists a period _x_ such that _l__x_u2009≤u2009_T_u2009≤u2009_r__x_. Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems. Input The first line contains one integer _n_xa0(1u2009≤u2009_n_u2009≤u20091000) — the number of problems. The second line contains _n_ integers _a__i_xa0(1u2009≤u2009_a__i_u2009≤u2009105) — the time Pasha needs to solve _i_th problem. The third line contains one integer _m_xa0(0u2009≤u2009_m_u2009≤u20091000) — the number of periods of time when the website is working. Next _m_ lines represent these periods. _j_th line contains two numbers _l__j_ and _r__j_xa0(1u2009≤u2009_l__j_u2009<u2009_r__j_u2009≤u2009105) — the starting and the ending moment of _j_th period. It is guaranteed that the periods are not intersecting and are given in chronological order, so for every _j_u2009>u20091 the condition _l__j_u2009>u2009_r__j_u2009-u20091 is met. Output If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted. Otherwise print "-1" (without brackets). Note In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again. In the second example Pasha invents the solution only after the website stops working for the last time. In the third example Pasha sends the solution exactly at the end of the first period. | 1,100 | false | false | true | false | false | false | false | false | false | false | 6,511 |
1696C | Fishingprince is playing with an array $$$[a_1,a_2,dots,a_n]$$$. He also has a magic number $$$m$$$. He can do the following two operations on it: Select $$$1le ile n$$$ such that $$$a_i$$$ is divisible by $$$m$$$ (that is, there exists an integer $$$t$$$ such that $$$m cdot t = a_i$$$). Replace $$$a_i$$$ with $$$m$$$ copies of $$$frac{a_i}{m}$$$. The order of the other elements doesn't change. For example, when $$$m=2$$$ and $$$a=[2,3]$$$ and $$$i=1$$$, $$$a$$$ changes into $$$[1,1,3]$$$. Select $$$1le ile n-m+1$$$ such that $$$a_i=a_{i+1}=dots=a_{i+m-1}$$$. Replace these $$$m$$$ elements with a single $$$m cdot a_i$$$. The order of the other elements doesn't change. For example, when $$$m=2$$$ and $$$a=[3,2,2,3]$$$ and $$$i=2$$$, $$$a$$$ changes into $$$[3,4,3]$$$. Note that the array length might change during the process. The value of $$$n$$$ above is defined as the current length of the array (might differ from the $$$n$$$ in the input). Fishingprince has another array $$$[b_1,b_2,dots,b_k]$$$. Please determine if he can turn $$$a$$$ into $$$b$$$ using any number (possibly zero) of operations. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1le nle 5cdot 10^4$$$, $$$2le mle 10^9$$$). The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1le a_ile 10^9$$$). The third line of each test case contains one integer $$$k$$$ ($$$1le kle 5cdot 10^4$$$). The fourth line of each test case contains $$$k$$$ integers $$$b_1,b_2,ldots,b_k$$$ ($$$1le b_ile 10^9$$$). It is guaranteed that the sum of $$$n+k$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each testcase, print Yes if it is possible to turn $$$a$$$ into $$$b$$$, and No otherwise. You can print each letter in any case (upper or lower). Example Input 5 5 2 1 2 2 4 2 4 1 4 4 2 6 2 1 2 2 8 2 2 2 1 16 8 3 3 3 3 3 3 3 3 3 4 6 6 6 6 8 3 3 9 6 3 12 12 36 12 16 9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4 8 3 3 9 6 3 12 12 36 12 7 12 2 4 3 4 12 56 Note In the first test case of the sample, we can do the second operation with $$$i=2$$$: $$$[1,color{red}{2,2},4,2] o [1,color{red}{4},4,2]$$$. In the second testcase of the sample, we can: do the second operation with $$$i=2$$$: $$$[1,color{red}{2,2},8,2,2] o [1,color{red}{4},8,2,2]$$$. do the second operation with $$$i=4$$$: $$$[1,4,8,color{red}{2,2}] o [1,4,8,color{red}{4}]$$$. do the first operation with $$$i=3$$$: $$$[1,4,color{red}{8},4] o [1,4,color{red}{4,4},4]$$$. do the second operation with $$$i=2$$$: $$$[1,color{red}{4,4},4,4] o [1,color{red}{8},4,4]$$$. do the second operation with $$$i=3$$$: $$$[1,8,color{red}{4,4}] o [1,8,color{red}{8}]$$$. do the second operation with $$$i=2$$$: $$$[1,color{red}{8,8}] o [1,color{red}{16}]$$$. | 1,400 | true | true | true | false | false | true | false | false | false | false | 2,106 |
223D | A plane contains a not necessarily convex polygon without self-intersections, consisting of _n_ vertexes, numbered from 1 to _n_. There is a spider sitting on the border of the polygon, the spider can move like that: 1. Transfer. The spider moves from the point _p_1 with coordinates (_x_1,u2009_y_1), lying on the polygon border, to the point _p_2 with coordinates (_x_2,u2009_y_2), also lying on the border. The spider can't go beyond the polygon border as it transfers, that is, the spider's path from point _p_1 to point _p_2 goes along the polygon border. It's up to the spider to choose the direction of walking round the polygon border (clockwise or counterclockwise). 2. Descend. The spider moves from point _p_1 with coordinates (_x_1,u2009_y_1) to point _p_2 with coordinates (_x_2,u2009_y_2), at that points _p_1 and _p_2 must lie on one vertical straight line (_x_1u2009=u2009_x_2), point _p_1 must be not lower than point _p_2 (_y_1u2009≥u2009_y_2) and segment _p_1_p_2 mustn't have points, located strictly outside the polygon (specifically, the segment can have common points with the border). Initially the spider is located at the polygon vertex with number _s_. Find the length of the shortest path to the vertex number _t_, consisting of transfers and descends. The distance is determined by the usual Euclidean metric . Input The first line contains integer _n_ (3u2009≤u2009_n_u2009≤u2009105) — the number of vertexes of the given polygon. Next _n_ lines contain two space-separated integers each — the coordinates of the polygon vertexes. The vertexes are listed in the counter-clockwise order. The coordinates of the polygon vertexes do not exceed 104 in their absolute value. The last line contains two space-separated integers _s_ and _t_ (1u2009≤u2009_s_,u2009_t_u2009≤u2009_n_) — the start and the end vertexes of the sought shortest way. Consider the polygon vertexes numbered in the order they are given in the input, that is, the coordinates of the first vertex are located on the second line of the input and the coordinates of the _n_-th vertex are on the (_n_u2009+u20091)-th line. It is guaranteed that the given polygon is simple, that is, it contains no self-intersections or self-tangencies. Output In the output print a single real number — the length of the shortest way from vertex _s_ to vertex _t_. The answer is considered correct, if its absolute or relative error does not exceed 10u2009-u20096. Examples Output 1.000000000000000000e+000 Output 0.000000000000000000e+000 Output 5.650281539872884700e+000 Note In the first sample the spider transfers along the side that connects vertexes 1 and 4. In the second sample the spider doesn't have to transfer anywhere, so the distance equals zero. In the third sample the best strategy for the spider is to transfer from vertex 3 to point (2,3), descend to point (2,1), and then transfer to vertex 1. | 3,000 | false | false | false | false | false | false | false | false | false | true | 8,944 |
1978C | Let's call the Manhattan value of a permutation$$$^{dagger}$$$ $$$p$$$ the value of the expression $$$p_1 - 1 + p_2 - 2 + ldots + p_n - n$$$. For example, for the permutation $$$[1, 2, 3]$$$, the Manhattan value is $$$1 - 1 + 2 - 2 + 3 - 3 = 0$$$, and for the permutation $$$[3, 1, 2]$$$, the Manhattan value is $$$3 - 1 + 1 - 2 + 2 - 3 = 2 + 1 + 1 = 4$$$. You are given integers $$$n$$$ and $$$k$$$. Find a permutation $$$p$$$ of length $$$n$$$ such that its Manhattan value is equal to $$$k$$$, or determine that no such permutation exists. $$$^{dagger}$$$A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). 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 only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^{5}, 0 le k le 10^{12}$$$) — the length of the permutation and the required Manhattan value. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^{5}$$$. Output For each test case, if there is no suitable permutation, output "No". Otherwise, in the first line, output "Yes", and in the second line, output $$$n$$$ distinct integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$) — a suitable permutation. If there are multiple solutions, output any of them. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). Example Input 8 3 4 4 5 7 0 1 1000000000000 8 14 112 777 5 12 5 2 Output Yes 3 1 2 No Yes 1 2 3 4 5 6 7 No Yes 8 2 3 4 5 6 1 7 No Yes 5 4 3 1 2 Yes 2 1 3 4 5 Note In the first test case, the permutation $$$[3, 1, 2]$$$ is suitable, its Manhattan value is $$$3 - 1 + 1 - 2 + 2 - 3 = 2 + 1 + 1 = 4$$$. In the second test case, it can be proven that there is no permutation of length $$$4$$$ with a Manhattan value of $$$5$$$. In the third test case, the permutation $$$[1,2,3,4,5,6,7]$$$ is suitable, its Manhattan value is $$$1-1+2-2+3-3+4-4+5-5+6-6+7-7=0$$$. | 1,300 | true | true | true | false | true | true | false | false | false | false | 420 |
1981C | Turtle was playing with a sequence $$$a_1, a_2, ldots, a_n$$$ consisting of positive integers. Unfortunately, some of the integers went missing while playing. Now the sequence becomes incomplete. There may exist an arbitrary number of indices $$$i$$$ such that $$$a_i$$$ becomes $$$-1$$$. Let the new sequence be $$$a'$$$. Turtle is sad. But Turtle remembers that for every integer $$$i$$$ from $$$1$$$ to $$$n - 1$$$, either $$$a_i = leftlfloorfrac{a_{i + 1}}{2} ight floor$$$ or $$$a_{i + 1} = leftlfloorfrac{a_i}{2} ight floor$$$ holds for the original sequence $$$a$$$. Turtle wants you to help him complete the sequence. But sometimes Turtle makes mistakes, so you need to tell him if you can't complete the sequence. Formally, you need to find another sequence $$$b_1, b_2, ldots, b_n$$$ consisting of positive integers such that: For every integer $$$i$$$ from $$$1$$$ to $$$n$$$, if $$$a'_i e -1$$$, then $$$b_i = a'_i$$$. For every integer $$$i$$$ from $$$1$$$ to $$$n - 1$$$, either $$$b_i = leftlfloorfrac{b_{i + 1}}{2} ight floor$$$ or $$$b_{i + 1} = leftlfloorfrac{b_i}{2} ight floor$$$ holds. For every integer $$$i$$$ from $$$1$$$ to $$$n$$$, $$$1 le b_i le 10^9$$$. If there is no sequence $$$b_1, b_2, ldots, b_n$$$ that satisfies all of the conditions above, you need to report $$$-1$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the length of the sequence. The second line of each test case contains $$$n$$$ integers $$$a'_1, a'_2, ldots, a'_n$$$ ($$$a'_i = -1$$$ or $$$1 le a'_i le 10^8$$$) — the elements of the sequence $$$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, if there is no sequence $$$b_1, b_2, ldots, b_n$$$ that satisfies all of the conditions, output a single integer $$$-1$$$. Otherwise, output $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ — the elements of the sequence $$$b_1, b_2, ldots, b_n$$$ you find. The sequence should satisfy that $$$1 le b_i le 10^9$$$ for every integer $$$i$$$ from $$$1$$$ to $$$n$$$. If there are multiple answers, print any of them. Example Input 9 8 -1 -1 -1 2 -1 -1 1 -1 4 -1 -1 -1 -1 6 3 -1 -1 -1 9 -1 4 -1 5 -1 6 4 2 -1 -1 3 4 1 2 3 4 2 4 2 5 -1 3 -1 3 6 13 -1 -1 3 -1 -1 -1 -1 7 -1 -1 3 -1 -1 Output 4 9 4 2 4 2 1 2 7 3 6 13 3 1 2 4 9 18 -1 -1 -1 4 2 6 3 1 3 6 3 1 3 1 3 7 3 7 3 1 3 1 3 Note In the first test case, $$$[4, 2, 1, 2, 1, 2, 1, 3]$$$ can also be the answer, while $$$[4, 2, 5, 10, 5, 2, 1, 3]$$$ and $$$[4, 2, 1, 2, 1, 2, 1, 4]$$$ cannot. In the second test case, $$$[1, 2, 5, 2]$$$ can also be the answer. From the fourth to the sixth test cases, it can be shown that there is no answer, so you should output $$$-1$$$. | 1,800 | true | true | true | false | false | true | true | false | false | false | 400 |
105B | Dark Assembly is a governing body in the Netherworld. Here sit the senators who take the most important decisions for the player. For example, to expand the range of the shop or to improve certain characteristics of the character the Dark Assembly's approval is needed. The Dark Assembly consists of _n_ senators. Each of them is characterized by his level and loyalty to the player. The level is a positive integer which reflects a senator's strength. Loyalty is the probability of a positive decision in the voting, which is measured as a percentage with precision of up to 10%. Senators make decisions by voting. Each of them makes a positive or negative decision in accordance with their loyalty. If strictly more than half of the senators take a positive decision, the player's proposal is approved. If the player's proposal is not approved after the voting, then the player may appeal against the decision of the Dark Assembly. To do that, player needs to kill all the senators that voted against (there's nothing wrong in killing senators, they will resurrect later and will treat the player even worse). The probability that a player will be able to kill a certain group of senators is equal to _A_u2009/u2009(_A_u2009+u2009_B_), where _A_ is the sum of levels of all player's characters and _B_ is the sum of levels of all senators in this group. If the player kills all undesired senators, then his proposal is approved. Senators are very fond of sweets. They can be bribed by giving them candies. For each received candy a senator increases his loyalty to the player by 10%. It's worth to mention that loyalty cannot exceed 100%. The player can take no more than _k_ sweets to the courtroom. Candies should be given to the senators before the start of voting. Determine the probability that the Dark Assembly approves the player's proposal if the candies are distributed among the senators in the optimal way. Input The first line contains three integers _n_, _k_ and _A_ (1u2009≤u2009_n_,u2009_k_u2009≤u20098, 1u2009≤u2009_A_u2009≤u20099999). Then _n_ lines follow. The _i_-th of them contains two numbers — _b__i_ and _l__i_ — the _i_-th senator's level and his loyalty. The levels of all senators are integers in range from 1 to 9999 (inclusive). The loyalties of all senators are integers in range from 0 to 100 (inclusive) and all of them are divisible by 10. Note In the first sample the best way of candies' distribution is giving them to first three of the senators. It ensures most of votes. It the second sample player should give all three candies to the fifth senator. | 1,800 | false | false | false | false | false | false | true | false | false | false | 9,465 |
1762G | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find any permutation $$$p$$$ of $$$[1,2,dots,n]$$$ such that: $$$p_{i-2} < p_i$$$ for all $$$i$$$, where $$$3 leq i leq n$$$, and $$$a_{p_{i-1}} eq a_{p_i}$$$ for all $$$i$$$, where $$$2 leq i leq n$$$. Or report that no such permutation exists. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 leq n leq 3 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 $$$3 cdot 10^5$$$. Output For each test case, output "NO" if no such permutation exists, otherwise output "YES" in the first line and print the permutation $$$p$$$ in the next line. In case there are multiple permutations, print any one of them. You can output "YES" and "NO" in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response). Example Input 4 3 1 2 1 4 1 2 3 4 3 1 1 1 7 1 2 1 1 3 1 4 Output YES 1 2 3 YES 3 1 4 2 NO YES 1 2 3 5 4 7 6 Note In the first test case, $$$p=[1,2,3]$$$ is the only permutation of $$$[1,2,3]$$$ that satisfy the given constraints. In the second test case, $$$[1,3,2,4]$$$, $$$[2,1,4,3]$$$ and some other permutations are also acceptable. In the third test case, it can be proved that there does not exist any permutation of $$$[1,2,3]$$$ satisfying the given constraints. | 3,100 | false | false | false | false | false | true | false | false | true | false | 1,734 |
87B | Programmer Vasya is studying a new programming language &K*. The &Klanguage resembles the languages of the C family in its syntax. However, it is more powerful, which is why the rules of the actual C-like languages are unapplicable to it. To fully understand the statement, please read the language's description below carefully and follow it and not the similar rules in real programming languages. There is a very powerful system of pointers on &K— you can add an asterisk to the right of the existing type _X_ — that will result in new type _X_u2009*u2009. That is called pointer-definition operation. Also, there is the operation that does the opposite — to any type of _X_, which is a pointer, you can add an ampersand — that will result in a type &_X_, to which refers _X_. That is called a dereference operation. The &Klanguage has only two basic data types — void and errtype. Also, the language has operators typedef and typeof. The operator "typedef _A_ _B_" defines a new data type _B_, which is equivalent to _A_. _A_ can have asterisks and ampersands, and _B_ cannot have them. For example, the operator typedef void*ptptvoid will create a new type ptptvoid, that can be used as void**. The operator "typeof _A_" returns type of _A_, brought to void, that is, returns the type void**...*, equivalent to it with the necessary number of asterisks (the number can possibly be zero). That is, having defined the ptptvoid type, as shown above, the typeof ptptvoid operator will return void**. An attempt of dereferencing of the void type will lead to an error: to a special data type errtype. For errtype the following equation holds true: errtype*u2009=u2009&errtypeu2009=u2009errtype. An attempt to use the data type that hasn't been defined before that will also lead to the errtype. Using typedef, we can define one type several times. Of all the definitions only the last one is valid. However, all the types that have been defined earlier using this type do not change. Let us also note that the dereference operation has the lower priority that the pointer operation, in other words &_T_u2009*u2009 is always equal to _T_. Note, that the operators are executed consecutively one by one. If we have two operators "typedef &void a" and "typedef ab", then at first a becomes errtype, and after that b becomes errtype= errtype, but not &void= void (see sample 2). Vasya does not yet fully understand this powerful technology, that's why he asked you to help him. Write a program that analyzes these operators. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of operators. Then follow _n_ lines with operators. Each operator is of one of two types: either "typedef _A_ _B_", or "typeof _A_". In the first case the _B_ type differs from void and errtype types, and besides, doesn't have any asterisks and ampersands. All the data type names are non-empty lines of no more than 20 lowercase Latin letters. The number of asterisks and ampersands separately in one type in any operator does not exceed 10, however if we bring some types to void with several asterisks, their number may exceed 10. Note Let's look at the second sample. After the first two queries typedef the b type is equivalent to void*, and c — to void**. The next query typedef redefines b — it is now equal to &b = &void= void. At that, the c type doesn't change. After that the c type is defined as &&b= &&void= &void = errtype. It doesn't influence the b type, that's why the next typedef defines c as &void= void. Then the b type is again redefined as &void = errtype. Please note that the c type in the next query is defined exactly as errtype******= errtype, and not &void******= void******. The same happens in the last typedef. | 1,800 | false | false | true | false | false | false | false | false | false | false | 9,535 |
1106D | Lunar New Year is approaching, and Bob decides to take a wander in a nearby park. The park can be represented as a connected graph with $$$n$$$ nodes and $$$m$$$ bidirectional edges. Initially Bob is at the node $$$1$$$ and he records $$$1$$$ on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes $$$a_1, a_2, ldots, a_n$$$ is recorded. Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it. A sequence $$$x$$$ is lexicographically smaller than a sequence $$$y$$$ if and only if one of the following holds: $$$x$$$ is a prefix of $$$y$$$, but $$$x e y$$$ (this is impossible in this problem as all considered sequences have the same length); in the first position where $$$x$$$ and $$$y$$$ differ, the sequence $$$x$$$ has a smaller element than the corresponding element in $$$y$$$. Input The first line contains two positive integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 10^5$$$), denoting the number of nodes and edges, respectively. The following $$$m$$$ lines describe the bidirectional edges in the graph. The $$$i$$$-th of these lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$), representing the nodes the $$$i$$$-th edge connects. Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected. Output Output a line containing the lexicographically smallest sequence $$$a_1, a_2, ldots, a_n$$$ Bob can record. Examples Input 5 5 1 4 3 4 5 4 3 2 1 5 Input 10 10 1 4 6 8 2 5 3 7 9 4 5 6 3 4 8 10 8 9 1 10 Output 1 4 3 7 9 8 6 5 2 10 Note In the first sample, Bob's optimal wandering path could be $$$1 ightarrow 2 ightarrow 1 ightarrow 3$$$. Therefore, Bob will obtain the sequence $$${1, 2, 3}$$$, which is the lexicographically smallest one. In the second sample, Bob's optimal wandering path could be $$$1 ightarrow 4 ightarrow 3 ightarrow 2 ightarrow 3 ightarrow 4 ightarrow 1 ightarrow 5$$$. Therefore, Bob will obtain the sequence $$${1, 4, 3, 2, 5}$$$, which is the lexicographically smallest one. | 1,500 | false | true | false | false | true | false | false | false | false | true | 5,172 |
113C | Problem - 113C - 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 math number theory *2200 No tag edit access → Contest materials are suitable for pair programming. They decided that the day _i_ (_l_u2009≤u2009_i_u2009≤u2009_r_) is suitable for pair programming if and only if the number _i_ is lucky for Peter and lucky for Bob at the same time. Help the boys to find the number of such days. Input The first line of the input contains integer numbers _l_,u2009_r_ (1u2009≤u2009_l_,u2009_r_u2009≤u20093·108). Output In the only line print the number of days on the segment | 2,200 | true | false | false | false | false | false | true | false | false | false | 9,433 |
559E | The main walking trail in Geraldion is absolutely straight, and it passes strictly from the north to the south, it is so long that no one has ever reached its ends in either of the two directions. The Geraldionians love to walk on this path at any time, so the mayor of the city asked the Herald to illuminate this path with a few spotlights. The spotlights have already been delivered to certain places and Gerald will not be able to move them. Each spotlight illuminates a specific segment of the path of the given length, one end of the segment is the location of the spotlight, and it can be directed so that it covers the segment to the south or to the north of spotlight. The trail contains a monument to the mayor of the island, and although you can walk in either directions from the monument, no spotlight is south of the monument. You are given the positions of the spotlights and their power. Help Gerald direct all the spotlights so that the total length of the illuminated part of the path is as much as possible. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of spotlights. Each of the _n_ lines contains two space-separated integers, _a__i_ and _l__i_ (0u2009≤u2009_a__i_u2009≤u2009108, 1u2009≤u2009_l__i_u2009≤u2009108). Number _a__i_ shows how much further the _i_-th spotlight to the north, and number _l__i_ shows the length of the segment it illuminates. It is guaranteed that all the _a__i_'s are distinct. Output Print a single integer — the maximum total length of the illuminated part of the path. | 3,000 | false | false | false | true | false | false | false | false | true | false | 7,594 |
1299C | There are $$$n$$$ water tanks in a row, $$$i$$$-th of them contains $$$a_i$$$ liters of water. The tanks are numbered from $$$1$$$ to $$$n$$$ from left to right. You can perform the following operation: choose some subsegment $$$[l, r]$$$ ($$$1le l le r le n$$$), and redistribute water in tanks $$$l, l+1, dots, r$$$ evenly. In other words, replace each of $$$a_l, a_{l+1}, dots, a_r$$$ by $$$frac{a_l + a_{l+1} + dots + a_r}{r-l+1}$$$. For example, if for volumes $$$[1, 3, 6, 7]$$$ you choose $$$l = 2, r = 3$$$, new volumes of water will be $$$[1, 4.5, 4.5, 7]$$$. You can perform this operation any number of times. What is the lexicographically smallest sequence of volumes of water that you can achieve? As a reminder: A sequence $$$a$$$ is lexicographically smaller than a sequence $$$b$$$ of the same length if and only if the following holds: in the first (leftmost) position where $$$a$$$ and $$$b$$$ differ, the sequence $$$a$$$ has a smaller element than the corresponding element in $$$b$$$. Input The first line contains an integer $$$n$$$ ($$$1 le n le 10^6$$$)xa0— the number of water tanks. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$)xa0— initial volumes of water in the water tanks, in liters. Because of large input, reading input as doubles is not recommended. Output Print the lexicographically smallest sequence you can get. In the $$$i$$$-th line print the final volume of water in the $$$i$$$-th tank. Your answer is considered correct if the absolute or relative error of each $$$a_i$$$ does not exceed $$$10^{-9}$$$. Formally, let your answer be $$$a_1, a_2, dots, a_n$$$, and the jury's answer be $$$b_1, b_2, dots, b_n$$$. Your answer is accepted if and only if $$$frac{a_i - b_i}{max{(1, b_i)}} le 10^{-9}$$$ for each $$$i$$$. Examples Output 5.666666667 5.666666667 5.666666667 7.000000000 Output 7.000000000 8.000000000 8.000000000 10.000000000 12.000000000 Input 10 3 9 5 5 1 7 5 3 8 7 Output 3.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 7.500000000 7.500000000 Note In the first sample, you can get the sequence by applying the operation for subsegment $$$[1, 3]$$$. In the second sample, you can't get any lexicographically smaller sequence. | 2,100 | false | true | false | false | true | false | false | false | false | false | 4,199 |
1245A | Consider the set of all nonnegative integers: $$${0, 1, 2, dots}$$$. Given two integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 10^4$$$). We paint all the numbers in increasing number first we paint $$$0$$$, then we paint $$$1$$$, then $$$2$$$ and so on. Each number is painted white or black. We paint a number $$$i$$$ according to the following rules: if $$$i = 0$$$, it is colored white; if $$$i ge a$$$ and $$$i - a$$$ is colored white, $$$i$$$ is also colored white; if $$$i ge b$$$ and $$$i - b$$$ is colored white, $$$i$$$ is also colored white; if $$$i$$$ is still not colored white, it is colored black. In this way, each nonnegative integer gets one of two colors. For example, if $$$a=3$$$, $$$b=5$$$, then the colors of the numbers (in the order from $$$0$$$) are: white ($$$0$$$), black ($$$1$$$), black ($$$2$$$), white ($$$3$$$), black ($$$4$$$), white ($$$5$$$), white ($$$6$$$), black ($$$7$$$), white ($$$8$$$), white ($$$9$$$), ... Note that: It is possible that there are infinitely many nonnegative integers colored black. For example, if $$$a = 10$$$ and $$$b = 10$$$, then only $$$0, 10, 20, 30$$$ and any other nonnegative integers that end in $$$0$$$ when written in base 10 are white. The other integers are colored black. It is also possible that there are only finitely many nonnegative integers colored black. For example, when $$$a = 1$$$ and $$$b = 10$$$, then there is no nonnegative integer colored black at all. Your task is to determine whether or not the number of nonnegative integers colored black is infinite. If there are infinitely many nonnegative integers colored black, simply print a line containing "Infinite" (without the quotes). Otherwise, print "Finite" (without the quotes). Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases in the input. Then $$$t$$$ lines follow, each line contains two space-separated integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 10^4$$$). Output For each test case, print one line containing either "Infinite" or "Finite" (without the quotes). Output is case-insensitive (i.e. "infinite", "inFiNite" or "finiTE" are all valid answers). | 1,000 | true | false | false | false | false | false | false | false | false | false | 4,474 |
430A | Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw _n_ distinct points and _m_ segments on the _OX_ axis. He can draw each point with either red or blue. The drawing is good if and only if the following requirement is met: for each segment [_l__i_,u2009_r__i_] consider all the red points belong to it (_r__i_ points), and all the blue points belong to it (_b__i_ points); each segment _i_ should satisfy the inequality _r__i_u2009-u2009_b__i_u2009≤u20091. Iahub thinks that point _x_ belongs to segment [_l_,u2009_r_], if inequality _l_u2009≤u2009_x_u2009≤u2009_r_ holds. Iahub gives to you all coordinates of points and segments. Please, help him to find any good drawing. Input The first line of input contains two integers: _n_ (1u2009≤u2009_n_u2009≤u2009100) and _m_ (1u2009≤u2009_m_u2009≤u2009100). The next line contains _n_ space-separated integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (0u2009≤u2009_x__i_u2009≤u2009100) — the coordinates of the points. The following _m_ lines contain the descriptions of the _m_ segments. Each line contains two integers _l__i_ and _r__i_ (0u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009100) — the borders of the _i_-th segment. It's guaranteed that all the points are distinct. Output If there is no good drawing for a given test, output a single integer -1. Otherwise output _n_ integers, each integer must be 0 or 1. The _i_-th number denotes the color of the _i_-th point (0 is red, and 1 is blue). If there are multiple good drawings you can output any of them. Examples Input 3 3 3 7 14 1 5 6 10 11 15 Input 3 4 1 2 3 1 2 2 3 5 6 2 2 | 1,600 | false | false | false | false | false | true | false | false | true | false | 8,121 |
157A | Sherlock Holmes and Dr. Watson played some game on a checkered board _n_u2009×u2009_n_ in size. During the game they put numbers on the board's squares by some tricky rules we don't know. However, the game is now over and each square of the board contains exactly one number. To understand who has won, they need to count the number of winning squares. To determine if the particular square is winning you should do the following. Calculate the sum of all numbers on the squares that share this column (including the given square) and separately calculate the sum of all numbers on the squares that share this row (including the given square). A square is considered winning if the sum of the column numbers is strictly greater than the sum of the row numbers. For instance, lets game was ended like is shown in the picture. Then the purple cell is winning, because the sum of its column numbers equals 8u2009+u20093u2009+u20096u2009+u20097u2009=u200924, sum of its row numbers equals 9u2009+u20095u2009+u20093u2009+u20092u2009=u200919, and 24u2009>u200919. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u200930). Each of the following _n_ lines contain _n_ space-separated integers. The _j_-th number on the _i_-th line represents the number on the square that belongs to the _j_-th column and the _i_-th row on the board. All number on the board are integers from 1 to 100. Output Print the single number — the number of the winning squares. Examples Input 4 5 7 8 4 9 5 3 2 1 6 6 4 9 5 7 3 Note In the first example two upper squares are winning. In the third example three left squares in the both middle rows are winning: 5 7 8 4 9 5 3 2 1 6 6 4 9 5 7 3 | 800 | false | false | false | false | false | false | true | false | false | false | 9,247 |
1659F | There is a tree of $$$n$$$ vertices and a permutation $$$p$$$ of size $$$n$$$. A token is present on vertex $$$x$$$ of the tree. Alice and Bob are playing a game. Alice is in control of the permutation $$$p$$$, and Bob is in control of the token on the tree. In Alice's turn, she must pick two distinct numbers $$$u$$$ and $$$v$$$ (not positions; $$$u eq v$$$), such that the token is neither at vertex $$$u$$$ nor vertex $$$v$$$ on the tree, and swap their positions in the permutation $$$p$$$. In Bob's turn, he must move the token to an adjacent vertex from the one it is currently on. Alice wants to sort the permutation in increasing order. Bob wants to prevent that. Alice wins if the permutation is sorted in increasing order at the beginning or end of her turn. Bob wins if he can make the game go on for an infinite number of moves (which means that Alice is never able to get a sorted permutation). Both players play optimally. Alice makes the first move. Given the tree, the permutation $$$p$$$, and the vertex $$$x$$$ on which the token initially is, find the winner of the game. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) xa0— the number of test cases. The description of each test case follows. The first line of each test case has two integers $$$n$$$ and $$$x$$$ ($$$3 leq n leq 2 cdot 10^5$$$; $$$1 leq x leq n$$$). Each of the next $$$n-1$$$ lines contains two integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le n$$$, $$$a eq b$$$) indicating an undirected edge between vertex $$$a$$$ and vertex $$$b$$$. It is guaranteed that the given edges form a tree. The next line contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$) xa0— the permutation $$$p$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output one line containing Alice or Bobxa0— the winner of the game. The output is case-sensitive. Examples Input 3 6 3 1 3 3 2 4 3 3 6 5 3 2 1 3 6 4 5 3 2 1 2 3 2 1 3 2 3 2 1 2 3 2 1 2 3 Input 1 11 4 3 11 5 9 10 3 4 8 2 4 1 8 6 8 8 7 4 5 5 11 7 4 9 8 6 5 11 10 2 3 1 Note Here is the explanation for the first example: In the first test case, there is a way for Alice to win. Here is a possible sequence of moves: 1. Alice swaps $$$5$$$ and $$$6$$$, resulting in $$$[2,1,3,5,4,6]$$$. 2. Bob moves the token to vertex $$$5$$$. 3. Alice swaps $$$1$$$ and $$$2$$$, resulting in $$$[1,2,3,5,4,6]$$$. 4. Bob moves the token to vertex $$$3$$$. 5. Alice swaps $$$4$$$ and $$$5$$$, resulting in $$$[1,2,3,4,5,6]$$$, and wins. In the second test case, Alice cannot win since Bob can make the game go on forever. Here is the sequence of moves: 1. Alice can only swap $$$1$$$ and $$$3$$$, resulting in $$$[3,1,2]$$$. 2. Bob moves the token to vertex $$$1$$$. 3. Alice can only swap $$$2$$$ and $$$3$$$, resulting in $$$[2,1,3]$$$. 4. Bob moves the token to vertex $$$2$$$. 5. Alice can only swap $$$1$$$ and $$$3$$$, resulting in $$$[2,3,1]$$$. 6. Bob moves the token to vertex $$$3$$$. 7. Alice can only swap $$$1$$$ and $$$2$$$, resulting in $$$[1,3,2]$$$. 8. Bob moves the token to vertex $$$2$$$. And then the sequence repeats forever. In the third test case, Alice wins immediately since the permutation is already sorted. | 3,000 | false | false | false | false | false | false | false | false | false | true | 2,327 |
1864H | Bogocubic is playing a game with amenotiomoi. First, Bogocubic fixed an integer $$$n$$$, and then he gave amenotiomoi an integer $$$x$$$ which is initially equal to $$$1$$$. In one move amenotiomoi performs one of the following operations with the same probability: increase $$$x$$$ by $$$1$$$; multiply $$$x$$$ by $$$2$$$. Bogocubic wants to find the expected number of moves amenotiomoi has to do to make $$$x$$$ greater than or equal to $$$n$$$. Help him find this number modulo $$$998,244,353$$$. Formally, let $$$M = 998,244,353$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output such an integer $$$y$$$ that $$$0 le y < M$$$ and $$$y cdot q equiv p pmod{M}$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). The description of the test cases follows. The only line of each test case contains one integer $$$n$$$ ($$$1 le n le 10^{18}$$$). Output For each test case, output a single integerxa0— the expected number of moves modulo $$$998,244,353$$$. Example Input 7 1 4 8 15 998244353 296574916252563317 494288321850420024 Output 0 499122179 717488133 900515847 93715054 44488799 520723508 Note In the first test case, $$$nle x$$$ without any operations, so the answer is $$$0$$$. In the second test case, for $$$n = 4$$$, here is the list of all possible sequences of operations and their probabilities: $$$1stackrel{+1}{longrightarrow}2stackrel{+1}{longrightarrow}3stackrel{+1}{longrightarrow}4$$$, the probability is $$$frac{1}{8}$$$; $$$1stackrel{ imes 2}{longrightarrow}2stackrel{+1}{longrightarrow}3stackrel{+1}{longrightarrow}4$$$, the probability is $$$frac{1}{8}$$$; $$$1stackrel{+1}{longrightarrow}2stackrel{+1}{longrightarrow}3stackrel{ imes 2}{longrightarrow}6$$$, the probability is $$$frac{1}{8}$$$; $$$1stackrel{ imes 2}{longrightarrow}2stackrel{+1}{longrightarrow}3stackrel{ imes 2}{longrightarrow}6$$$, the probability is $$$frac{1}{8}$$$; $$$1stackrel{+1}{longrightarrow}2stackrel{ imes 2}{longrightarrow}4$$$, the probability is $$$frac{1}{4}$$$; $$$1stackrel{ imes 2}{longrightarrow}2stackrel{ imes 2}{longrightarrow}4$$$, the probability is $$$frac{1}{4}$$$. So the expected number of moves is $$$4 cdot left(3 cdot frac{1}{8} ight) + 2 cdot left(2 cdot frac{1}{4} ight) =frac{5}{2} equiv 499122179 pmod{998244353}$$$. In the third test case, for $$$n = 8$$$, the expected number of moves is $$$frac{137}{32}equiv 717488133pmod{998244353}$$$. In the fourth test case, for $$$n = 15$$$, the expected number of moves is $$$frac{24977}{4096} equiv 900515847 pmod{998244353}$$$. | 3,200 | true | false | false | true | false | false | false | false | false | false | 1,090 |
1030C | Problem - 1030C - Codeforces =============== xa0 ]( "Технокубок 2019 — Отборочный Раунд 1 (и открытые рейтинговые раунды Codeforces Round 512 Div.1, Div.2)") Editorial") — the number of digits in the ticket. The second line contains $$$n$$$ digits $$$a_1 a_2 dots a_n$$$ ($$$0 le a_i le 9$$$) — the golden ticket. Digits are printed without spaces. Output If the golden ticket is lucky then print "YES", otherwise print "NO" (both case insensitive). Examples Input 5 73452 Output YES Input 4 1248 Output NO Note In the first example the ticket can be divided into $$$7$$$, $$$34$$$ and $$$52$$$: $$$7=3+4=5+2$$$. In the second example it is impossible to divide ticket into segments with equal sum. | 1,300 | false | false | true | false | false | false | false | false | false | false | 5,553 |
437D | Of course our child likes walking in a zoo. The zoo has _n_ areas, that are numbered from 1 to _n_. The _i_-th area contains _a__i_ animals in it. Also there are _m_ roads in the zoo, and each road connects two distinct areas. Naturally the zoo is connected, so you can reach any area of the zoo from any other area using the roads. Our child is very smart. Imagine the child want to go from area _p_ to area _q_. Firstly he considers all the simple routes from _p_ to _q_. For each route the child writes down the number, that is equal to the minimum number of animals among the route areas. Let's denote the largest of the written numbers as _f_(_p_,u2009_q_). Finally, the child chooses one of the routes for which he writes down the value _f_(_p_,u2009_q_). After the child has visited the zoo, he thinks about the question: what is the average value of _f_(_p_,u2009_q_) for all pairs _p_,u2009_q_ (_p_u2009≠u2009_q_)? Can you answer his question? Input The first line contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009105; 0u2009≤u2009_m_u2009≤u2009105). The second line contains _n_ integers: _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009105). Then follow _m_ lines, each line contains two integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_; _x__i_u2009≠u2009_y__i_), denoting the road between areas _x__i_ and _y__i_. All roads are bidirectional, each pair of areas is connected by at most one road. Output Output a real number — the value of . The answer will be considered correct if its relative or absolute error doesn't exceed 10u2009-u20094. Examples Input 4 3 10 20 30 40 1 3 2 3 4 3 Input 3 3 10 20 30 1 2 2 3 3 1 Input 7 8 40 20 10 30 20 50 40 1 2 2 3 3 4 4 5 5 6 6 7 1 4 5 7 Note Consider the first sample. There are 12 possible situations: _p_u2009=u20091,u2009_q_u2009=u20093,u2009_f_(_p_,u2009_q_)u2009=u200910. _p_u2009=u20092,u2009_q_u2009=u20093,u2009_f_(_p_,u2009_q_)u2009=u200920. _p_u2009=u20094,u2009_q_u2009=u20093,u2009_f_(_p_,u2009_q_)u2009=u200930. _p_u2009=u20091,u2009_q_u2009=u20092,u2009_f_(_p_,u2009_q_)u2009=u200910. _p_u2009=u20092,u2009_q_u2009=u20094,u2009_f_(_p_,u2009_q_)u2009=u200920. _p_u2009=u20094,u2009_q_u2009=u20091,u2009_f_(_p_,u2009_q_)u2009=u200910. Another 6 cases are symmetrical to the above. The average is . Consider the second sample. There are 6 possible situations: _p_u2009=u20091,u2009_q_u2009=u20092,u2009_f_(_p_,u2009_q_)u2009=u200910. _p_u2009=u20092,u2009_q_u2009=u20093,u2009_f_(_p_,u2009_q_)u2009=u200920. _p_u2009=u20091,u2009_q_u2009=u20093,u2009_f_(_p_,u2009_q_)u2009=u200910. Another 3 cases are symmetrical to the above. The average is . | 1,900 | false | false | false | false | false | false | false | false | true | false | 8,088 |
440B | Problem - 440B - Codeforces =============== xa0 . The second line contains _n_ non-negative numbers that do not exceed 109, the _i_-th written number is the number of matches in the _i_-th matchbox. It is guaranteed that the total number of matches is divisible by _n_. Output Print the total minimum number of moves. Examples Input 6 1 6 2 5 3 7 Output 12 | 1,600 | false | true | true | false | false | false | false | false | false | false | 8,078 |
1520A | Polycarp has $$$26$$$ tasks. Each task is designated by a capital letter of the Latin alphabet. The teacher asked Polycarp to solve tasks in the following way: if Polycarp began to solve some task, then he must solve it to the end, without being distracted by another task. After switching to another task, Polycarp cannot return to the previous task. Polycarp can only solve one task during the day. Every day he wrote down what task he solved. Now the teacher wants to know if Polycarp followed his advice. For example, if Polycarp solved tasks in the following order: "DDBBCCCBBEZ", then the teacher will see that on the third day Polycarp began to solve the task 'B', then on the fifth day he got distracted and began to solve the task 'C', on the eighth day Polycarp returned to the task 'B'. Other examples of when the teacher is suspicious: "BAB", "AABBCCDDEEBZZ" and "AAAAZAAAAA". If Polycarp solved the tasks as follows: "FFGZZZY", then the teacher cannot have any suspicions. Please note that Polycarp is not obligated to solve all tasks. Other examples of when the teacher doesn't have any suspicious: "BA", "AFFFCC" and "YYYYY". Help Polycarp find out if his teacher might be suspicious. Input The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 50$$$)xa0— the number of days during which Polycarp solved tasks. The second line contains a string of length $$$n$$$, consisting of uppercase Latin letters, which is the order in which Polycarp solved the tasks. Output For each test case output: "YES", if the teacher cannot be suspicious; "NO", otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer). | 800 | false | false | true | false | false | false | true | false | false | false | 3,063 |
2041B | Bowling is a national sport in Taiwan; everyone in the country plays the sport on a daily basis since their youth. Naturally, there are a lot of bowling alleys all over the country, and the competition between them is as intense as you can imagine. Maw-Shang owns one such bowling alley. To stand out from other competitors in the industry and draw attention from customers, he decided to hold a special event every month that features various unorthodox bowling rules. For the event this month, he came up with a new version of the game called X-pin bowling. In the traditional $$$10$$$-pin bowling game, a frame is built out of ten bowling pins forming a triangular shape of side length four. The pin closest to the player forms the first row, and the two pins behind it form the second row, and so on. Unlike the standard version, the game of $$$X$$$-pin bowling Maw-Shang designed allows a much larger number of pins that form a larger frame. The following figure shows a standard $$$10$$$-pin frame on the left, and on the right it shows a $$$21$$$-pin frame that forms a triangular shape of side length six which is allowed in the game of $$$X$$$-pin bowling. Being the national sport, the government of Taiwan strictly regulates and standardizes the manufacturing of bowling pins. There are two types of bowling pins allowed, one in black and the other in white, and the bowling alley Maw-Shang owns has $$$w$$$ white pins and $$$b$$$ black pins. To make this new game exciting for the customers, Maw-Shang wants to build the largest possible frame from these $$$w+b$$$ pins. However, even though he is okay with using both colors in building the frame, for aesthetic reasons, Maw-Shang still wants the colors of the pins on the same row to be identical. For example, the following figure shows two possible frames of side length six, but only the left one is acceptable to Maw-Shang since the other one has white and black pins mixed in the third row. The monthly special event is happening in just a few hours. Please help Maw-Shang calculate the side length of the largest frame that he can build from his $$$w+b$$$ pins! Input The first line of the input contains a single integer $$$t$$$, the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$w$$$ and $$$b$$$, the number of white and black pins, respectively. $$$1 leq t leq 100$$$ $$$0 leq w, b leq 10^9$$$ | 1,200 | true | false | false | false | false | false | true | true | false | false | 12 |
1725D | Let's say Pak Chanek has an array $$$A$$$ consisting of $$$N$$$ positive integers. Pak Chanek will do a number of operations. In each operation, Pak Chanek will do the following: 1. Choose an index $$$p$$$ ($$$1 leq p leq N$$$). 2. Let $$$c$$$ be the number of operations that have been done on index $$$p$$$ before this operation. 3. Decrease the value of $$$A_p$$$ by $$$2^c$$$. 4. Multiply the value of $$$A_p$$$ by $$$2$$$. After each operation, all elements of $$$A$$$ must be positive integers. An array $$$A$$$ is said to be sortable if and only if Pak Chanek can do zero or more operations so that $$$A_1 < A_2 < A_3 < A_4 < ldots < A_N$$$. Pak Chanek must find an array $$$A$$$ that is sortable with length $$$N$$$ such that $$$A_1 + A_2 + A_3 + A_4 + ldots + A_N$$$ is the minimum possible. If there are more than one possibilities, Pak Chanek must choose the array that is lexicographically minimum among them. Pak Chanek must solve the following things: Pak Chanek must print the value of $$$A_1 + A_2 + A_3 + A_4 + ldots + A_N$$$ for that array. $$$Q$$$ questions will be given. For the $$$i$$$-th question, an integer $$$P_i$$$ is given. Pak Chanek must print the value of $$$A_{P_i}$$$. Help Pak Chanek solve the problem. Note: an array $$$B$$$ of size $$$N$$$ is said to be lexicographically smaller than an array $$$C$$$ that is also of size $$$N$$$ if and only if there exists an index $$$i$$$ such that $$$B_i < C_i$$$ and for each $$$j < i$$$, $$$B_j = C_j$$$. Input The first line contains two integers $$$N$$$ and $$$Q$$$ ($$$1 leq N leq 10^9$$$, $$$0 leq Q leq min(N, 10^5)$$$) — the required length of array $$$A$$$ and the number of questions. The $$$i$$$-th of the next $$$Q$$$ lines contains a single integer $$$P_i$$$ ($$$1 leq P_1 < P_2 < ldots < P_Q leq N$$$) — the index asked in the $$$i$$$-th question. Output Print $$$Q+1$$$ lines. The $$$1$$$-st line contains an integer representing $$$A_1 + A_2 + A_3 + A_4 + ldots + A_N$$$. For each $$$1 leq i leq Q$$$, the $$$(i+1)$$$-th line contains an integer representing $$$A_{P_i}$$$. Note In the first example, the array $$$A$$$ obtained is $$$[1, 2, 3, 3, 4, 4]$$$. We can see that the array is sortable by doing the following operations: Choose index $$$5$$$, then $$$A = [1, 2, 3, 3, 6, 4]$$$. Choose index $$$6$$$, then $$$A = [1, 2, 3, 3, 6, 6]$$$. Choose index $$$4$$$, then $$$A = [1, 2, 3, 4, 6, 6]$$$. Choose index $$$6$$$, then $$$A = [1, 2, 3, 4, 6, 8]$$$. | 2,900 | true | false | false | false | false | false | false | true | false | false | 1,943 |
1102A | Problem - 1102A - 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 *800 No tag edit access → Contest materials ") - sum(B)$$$ is minimum possible. The value $$$x$$$ is the absolute value of $$$x$$$ and $$$sum(S)$$$ is the sum of elements of the set $$$S$$$. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^9$$$). Output Print one integer — the minimum possible value of $$$sum(A) - sum(B)$$$ if you divide the initial sequence $$$1, 2, dots, n$$$ into two sets $$$A$$$ and $$$B$$$. Examples Input 3 Output 0 Input 5 Output 1 Input 6 Output 1 Note Some (not all) possible answers to examples: In the first example you can divide the initial sequence into sets $$$A = {1, 2}$$$ and $$$B = {3}$$$ so the answer is $$$0$$$. In the second example you can divide the initial sequence into sets $$$A = {1, 3, 4}$$$ and $$$B = {2, 5}$$$ so the answer is $$$1$$$. In the third example you can divide the initial sequence into sets $$$A = {1, 4, 5}$$$ and $$$B = {2, 3, 6}$$$ so the answer is $$$1$$$. | 800 | true | false | false | false | false | false | false | false | false | false | 5,193 |
1999B | Suneet and Slavic play a card game. The rules of the game are as follows: Each card has an integer value between $$$1$$$ and $$$10$$$. Each player receives $$$2$$$ cards which are face-down (so a player doesn't know their cards). The game is turn-based and consists exactly of two turns. In a round, both players pick a random unflipped card and flip it. The player who flipped a card with a strictly greater number wins the round. In case of equality, no one wins the round. A player wins a game if he wins the most number of rounds (i.e. strictly greater than the other player). In case of equality, no one wins the game. Since Suneet and Slavic aren't best friends, you need to calculate the number of ways the game could happen that Suneet would end up as the winner. For a better understanding, please check the notes section. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first and only line of each test case contains $$$4$$$ integers $$$a_1$$$, $$$a_2$$$, $$$b_1$$$, $$$b_2$$$ ($$$1 leq a_1, a_2, b_1, b_2 leq 10$$$) where $$$a_1$$$ and $$$a_2$$$ represent the cards Suneet has, and $$$b_1$$$ and $$$b_2$$$ represent the cards Slavic has, respectively. Output For each test case, output a single integerxa0— the number of games Suneet would win considering all possible games. Example Input 5 3 8 2 6 1 1 1 1 10 10 2 2 1 1 10 10 3 8 7 2 Note Consider the first test case when Slavic starts with the cards that have the values $$$2$$$ and $$$6$$$, and Suneet starts with cards that have the values $$$3$$$ and $$$8$$$. The game could happen in $$$4$$$ different ways: Suneet flips $$$3$$$ and Slavic flips $$$2$$$. Suneet wins the first round. Then, Suneet flips $$$8$$$ and Slavic flips $$$6$$$. Suneet wins the second round as well. Since Suneet won $$$2$$$ rounds, he wins the game. Suneet flips $$$3$$$ and Slavic flips $$$6$$$. Slavic wins the first round. Then, Suneet flips $$$8$$$ and Slavic flips $$$2$$$. Suneet wins the second round. Nobody wins since both players won an equal amount of rounds. Suneet flips $$$8$$$ and Slavic flips $$$6$$$. Suneet wins the first round. Then, Suneet flips $$$3$$$ and Slavic flips $$$2$$$. Suneet wins the second round as well. Since Suneet won $$$2$$$ rounds, he wins the game. Suneet flips $$$8$$$ and Slavic flips $$$2$$$. Suneet wins the first round. Then, Suneet flips $$$3$$$ and Slavic flips $$$6$$$. Slavic wins the round. Nobody wins since both players won an equal amount of rounds. | 1,000 | false | false | true | false | false | true | true | false | false | false | 270 |
547A | Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is _h_1 and height of Abol is _h_2. Each second, Mike waters Abol and Xaniar. So, if height of Xaniar is _h_1 and height of Abol is _h_2, after one second height of Xaniar will become and height of Abol will become where _x_1,u2009_y_1,u2009_x_2 and _y_2 are some integer numbers and denotes the remainder of _a_ modulo _b_. Mike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania is _a_1 and height of Abol is _a_2. Mike has asked you for your help. Calculate the minimum time or say it will never happen. Input The first line of input contains integer _m_ (2u2009≤u2009_m_u2009≤u2009106). The second line of input contains integers _h_1 and _a_1 (0u2009≤u2009_h_1,u2009_a_1u2009<u2009_m_). The third line of input contains integers _x_1 and _y_1 (0u2009≤u2009_x_1,u2009_y_1u2009<u2009_m_). The fourth line of input contains integers _h_2 and _a_2 (0u2009≤u2009_h_2,u2009_a_2u2009<u2009_m_). The fifth line of input contains integers _x_2 and _y_2 (0u2009≤u2009_x_2,u2009_y_2u2009<u2009_m_). It is guaranteed that _h_1u2009≠u2009_a_1 and _h_2u2009≠u2009_a_2. Output Print the minimum number of seconds until Xaniar reaches height _a_1 and Abol reaches height _a_2 or print -1 otherwise. Note In the first sample, heights sequences are following: Xaniar: Abol: | 2,200 | true | true | true | false | false | false | true | false | false | false | 7,652 |
883E | Polycarpus takes part in the "Field of Wonders" TV show. The participants of the show have to guess a hidden word as fast as possible. Initially all the letters of the word are hidden. The game consists of several turns. At each turn the participant tells a letter and the TV show host responds if there is such letter in the word or not. If there is such letter then the host reveals all such letters. For example, if the hidden word is "abacaba" and the player tells the letter "a", the host will reveal letters at all positions, occupied by "a": 1, 3, 5 and 7 (positions are numbered from left to right starting from 1). Polycarpus knows _m_ words of exactly the same length as the hidden word. The hidden word is also known to him and appears as one of these _m_ words. At current moment a number of turns have already been made and some letters (possibly zero) of the hidden word are already revealed. Previously Polycarp has told exactly the letters which are currently revealed. It is Polycarpus' turn. He wants to tell a letter in such a way, that the TV show host will assuredly reveal at least one more letter. Polycarpus cannot tell the letters, which are already revealed. Your task is to help Polycarpus and find out the number of letters he can tell so that the show host will assuredly reveal at least one of the remaining letters. Input The first line contains one integer _n_ (1u2009≤u2009_n_u2009≤u200950) — the length of the hidden word. The following line describes already revealed letters. It contains the string of length _n_, which consists of lowercase Latin letters and symbols "*". If there is a letter at some position, then this letter was already revealed. If the position contains symbol "*", then the letter at this position has not been revealed yet. It is guaranteed, that at least one letter is still closed. The third line contains an integer _m_ (1u2009≤u2009_m_u2009≤u20091000) — the number of words of length _n_, which Polycarpus knows. The following _m_ lines contain the words themselves — _n_-letter strings of lowercase Latin letters. All words are distinct. It is guaranteed that the hidden word appears as one of the given _m_ words. Before the current move Polycarp has told exactly the letters which are currently revealed. Output Output the single integer — the number of letters Polycarpus can tell so that the TV show host definitely reveals at least one more letter. It is possible that this number is zero. Note In the first example Polycarpus can tell letters "b" and "c", which assuredly will be revealed. The second example contains no letters which can be told as it is not clear, which of the letters "v" or "s" is located at the third position of the hidden word. In the third example Polycarpus exactly knows that the hidden word is "aba", because in case it was "aaa", then the second letter "a" would have already been revealed in one of previous turns. | 1,500 | false | false | true | false | false | false | false | false | false | false | 6,223 |
1427A | You are given an array of $$$n$$$ integers $$$a_1,a_2,dots,a_n$$$. You have to create an array of $$$n$$$ integers $$$b_1,b_2,dots,b_n$$$ such that: The array $$$b$$$ is a rearrangement of the array $$$a$$$, that is, it contains the same values and each value appears the same number of times in the two arrays. In other words, the multisets $$${a_1,a_2,dots,a_n}$$$ and $$${b_1,b_2,dots,b_n}$$$ are equal.For example, if $$$a=[1,-1,0,1]$$$, then $$$b=[-1,1,1,0]$$$ and $$$b=[0,1,-1,1]$$$ are rearrangements of $$$a$$$, but $$$b=[1,-1,-1,0]$$$ and $$$b=[1,0,2,-3]$$$ are not rearrangements of $$$a$$$. For all $$$k=1,2,dots,n$$$ the sum of the first $$$k$$$ elements of $$$b$$$ is nonzero. Formally, for all $$$k=1,2,dots,n$$$, it must hold $$$$$$b_1+b_2+cdots+b_k ot=0,.$$$$$$ If an array $$$b_1,b_2,dots, b_n$$$ with the required properties does not exist, you have to print NO. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1le t le 1000$$$) — the number of test cases. The description of the test cases follows. The first line of each testcase contains one integer $$$n$$$ ($$$1le nle 50$$$) xa0— the length of the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1,a_2,dots, a_n$$$ ($$$-50le a_ile 50$$$) xa0— the elements of $$$a$$$. Output For each testcase, if there is not an array $$$b_1,b_2,dots,b_n$$$ with the required properties, print a single line with the word NO. Otherwise print a line with the word YES, followed by a line with the $$$n$$$ integers $$$b_1,b_2,dots,b_n$$$. If there is more than one array $$$b_1,b_2,dots,b_n$$$ satisfying the required properties, you can print any of them. Example Input 4 4 1 -2 3 -4 3 0 0 0 5 1 -1 1 -1 1 6 40 -31 -9 0 13 -40 Output YES 1 -2 3 -4 NO YES 1 1 -1 1 -1 YES -40 13 40 0 -9 -31 Note Explanation of the first testcase: An array with the desired properties is $$$b=[1,-2,3,-4]$$$. For this array, it holds: The first element of $$$b$$$ is $$$1$$$. The sum of the first two elements of $$$b$$$ is $$$-1$$$. The sum of the first three elements of $$$b$$$ is $$$2$$$. The sum of the first four elements of $$$b$$$ is $$$-2$$$. Explanation of the second testcase: Since all values in $$$a$$$ are $$$0$$$, any rearrangement $$$b$$$ of $$$a$$$ will have all elements equal to $$$0$$$ and therefore it clearly cannot satisfy the second property described in the statement (for example because $$$b_1=0$$$). Hence in this case the answer is NO. Explanation of the third testcase: An array with the desired properties is $$$b=[1, 1, -1, 1, -1]$$$. For this array, it holds: The first element of $$$b$$$ is $$$1$$$. The sum of the first two elements of $$$b$$$ is $$$2$$$. The sum of the first three elements of $$$b$$$ is $$$1$$$. The sum of the first four elements of $$$b$$$ is $$$2$$$. The sum of the first five elements of $$$b$$$ is $$$1$$$. Explanation of the fourth testcase: An array with the desired properties is $$$b=[-40,13,40,0,-9,-31]$$$. For this array, it holds: The first element of $$$b$$$ is $$$-40$$$. The sum of the first two elements of $$$b$$$ is $$$-27$$$. The sum of the first three elements of $$$b$$$ is $$$13$$$. The sum of the first four elements of $$$b$$$ is $$$13$$$. The sum of the first five elements of $$$b$$$ is $$$4$$$. The sum of the first six elements of $$$b$$$ is $$$-27$$$. | 900 | true | false | false | false | false | false | false | false | true | false | 3,533 |
344A | Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other. Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own. Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed. Input The first line of the input contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009100000) — the number of magnets. Then _n_ lines follow. The _i_-th line (1u2009≤u2009_i_u2009≤u2009_n_) contains either characters "01", if Mike put the _i_-th magnet in the "plus-minus" position, or characters "10", if Mike put the magnet in the "minus-plus" position. Output On the single line of the output print the number of groups of magnets. Note The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets. The second testcase has two groups, each consisting of two magnets. | 800 | false | false | true | false | false | false | false | false | false | false | 8,458 |
1769D1 | В этой версии задачи нужно определить, кто выиграет в карточной игре для двух игроков в заданном раскладе при оптимальной игре обоих соперников. Алиса и Боб решили сыграть в карточную игру «Девятка». Пожалуйста, внимательно прочитайте условие задачи, поскольку правила могут отличаться от известных вам. Для игры нужна стандартная колода из $$$36$$$ картxa0— по девять карт (от шестёрки до туза) каждой из четырёх мастей (трефы, бубны, пики и черви). Карты по достоинству от младшей к старшей идут следующим образом: шестёрка, семёрка, восьмёрка, девятка, десятка, валет, дама, король, туз. Перед игрой колода перемешивается, и каждому игроку раздаётся по $$$18$$$ карт. Карты нужно выкладывать из руки на стол по определённым правилам. Выигрывает игрок, который первым выложит все карты из своей руки. Игроки ходят по очереди. Ход игрока имеет один из следующих видов: выложить на стол из своей руки девятку любой масти; выложить на стол шестёрку, семёрку или восьмёрку любой масти, если на столе уже лежит карта той же масти достоинством на единицу выше; выложить на стол десятку, валета, даму, короля или туза любой масти, если на столе уже лежит карта той же масти достоинством на единицу ниже. Например, девятку пик можно выложить на стол в любой момент, для выкладывания семёрки треф необходимо наличие на столе восьмёрки треф, а для выкладывания туза червей необходимо наличие на столе короля червей. Если игрок не может выложить на стол ни одну карту из своей руки, то ход переходит к сопернику. Обратите внимание: нельзя пропустить ход просто такxa0— всегда необходимо выложить карту на стол корректным образом, если это возможно. Вам дан расклад карт в начале игры. Алиса будет ходить первой. Определите, кто первым избавится от всех своих карт и выиграет, если оба игрока будут пытаться победить и играть оптимально. Входные данные В первой строке задано $$$18$$$ строк длины $$$2$$$ через пробел, описывающих карты Алисы в случайном порядке. Первый символ строки обозначает достоинство картыxa0— символ из набора 6, 7, 8, 9, T, J, Q, K, A, обозначающий шестёрку, семёрку, восьмёрку, девятку, десятку, валета, даму, короля и туза, соответственно. Второй символ строки обозначает масть картыxa0— символ из набора C, D, S, H, обозначающий трефы, бубны, пики и черви, соответственно. Во второй строке задано $$$18$$$ строк длины $$$2$$$ через пробел, описывающих карты Боба в том же формате. Каждая из $$$36$$$ возможных карт находится в руке одного из двух игроков в единственном экземпляре. В задаче $$$100$$$ тестов. Все тесты сгенерированы случайным образом. | 1,800 | false | false | false | true | false | false | true | false | false | false | 1,680 |
1882A | You are given a sequence $$$a_{1}, a_{2}, ldots, a_{n}$$$. A sequence $$$b_{1}, b_{2}, ldots, b_{n}$$$ is called good, if it satisfies all of the following conditions: $$$b_{i}$$$ is a positive integer for $$$i = 1, 2, ldots, n$$$; $$$b_{i} eq a_{i}$$$ for $$$i = 1, 2, ldots, n$$$; $$$b_{1} < b_{2} < ldots < b_{n}$$$. Find the minimum value of $$$b_{n}$$$ among all good sequences $$$b_{1}, b_{2}, ldots, b_{n}$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^{9}$$$). Output For each test case, print a single integerxa0— the minimum value of $$$b_{n}$$$ among all good sequences $$$b$$$. Example Input 3 5 1 3 2 6 7 4 2 3 4 5 1 1 Note In the first test case, $$$b = [2, 4, 5, 7, 8]$$$ is a good sequence. It can be proved that there is no good $$$b$$$ with $$$b_{5} < 8$$$. In the second test case, $$$b = [1, 2, 3, 4]$$$ is an optimal good sequence. In the third test case, $$$b = [2]$$$ is an optimal good sequence. | 800 | false | true | false | false | false | false | false | false | false | false | 997 |
1539C | There are $$$n$$$ students numerated from $$$1$$$ to $$$n$$$. The level of the $$$i$$$-th student is $$$a_i$$$. You need to split the students into stable groups. A group of students is called stable, if in the sorted array of their levels no two neighboring elements differ by more than $$$x$$$. For example, if $$$x = 4$$$, then the group with levels $$$[1, 10, 8, 4, 4]$$$ is stable (because $$$4 - 1 le x$$$, $$$4 - 4 le x$$$, $$$8 - 4 le x$$$, $$$10 - 8 le x$$$), while the group with levels $$$[2, 10, 10, 7]$$$ is not stable ($$$7 - 2 = 5 > x$$$). Apart from the $$$n$$$ given students, teachers can invite at most $$$k$$$ additional students with arbitrary levels (at teachers' choice). Find the minimum number of stable groups teachers can form from all students (including the newly invited). For example, if there are two students with levels $$$1$$$ and $$$5$$$; $$$x = 2$$$; and $$$k ge 1$$$, then you can invite a new student with level $$$3$$$ and put all the students in one stable group. Input The first line contains three integers $$$n$$$, $$$k$$$, $$$x$$$ ($$$1 le n le 200,000$$$, $$$0 le k le 10^{18}$$$, $$$1 le x le 10^{18}$$$)xa0— the initial number of students, the number of students you can additionally invite, and the maximum allowed level difference. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^{18}$$$)xa0— the students levels. Note In the first example you can invite two students with levels $$$2$$$ and $$$11$$$. Then you can split the students into two stable groups: 1. $$$[1, 1, 2, 5, 8, 11, 12, 13]$$$, 2. $$$[20, 22]$$$. In the second example you are not allowed to invite new students, so you need $$$3$$$ groups: 1. $$$[1, 1, 5, 5, 20, 20]$$$ 2. $$$[60, 70, 70, 70, 80, 90]$$$ 3. $$$[420]$$$ | 1,200 | false | true | false | false | false | false | false | false | true | false | 2,946 |
295D | Greg has a pad. The pad's screen is an _n_u2009×u2009_m_ rectangle, each cell can be either black or white. We'll consider the pad rows to be numbered with integers from 1 to _n_ from top to bottom. Similarly, the pad's columns are numbered with integers from 1 to _m_ from left to right. Greg thinks that the pad's screen displays a cave if the following conditions hold: There is a segment [_l_,u2009_r_] (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_), such that each of the rows _l_,u2009_l_u2009+u20091,u2009...,u2009_r_ has exactly two black cells and all other rows have only white cells. There is a row number _t_ (_l_u2009≤u2009_t_u2009≤u2009_r_), such that for all pairs of rows with numbers _i_ and _j_ (_l_u2009≤u2009_i_u2009≤u2009_j_u2009≤u2009_t_) the set of columns between the black cells in row _i_ (with the columns where is these black cells) is the subset of the set of columns between the black cells in row _j_ (with the columns where is these black cells). Similarly, for all pairs of rows with numbers _i_ and _j_ (_t_u2009≤u2009_i_u2009≤u2009_j_u2009≤u2009_r_) the set of columns between the black cells in row _j_ (with the columns where is these black cells) is the subset of the set of columns between the black cells in row _i_ (with the columns where is these black cells). Greg wondered, how many ways there are to paint a cave on his pad. Two ways can be considered distinct if there is a cell that has distinct colors on the two pictures. Help Greg. Input The first line contains two integers _n_, _m_ — the pad's screen size (1u2009≤u2009_n_,u2009_m_u2009≤u20092000). Output In the single line print the remainder after dividing the answer to the problem by 1000000007 (109u2009+u20097). | 2,400 | false | false | false | true | false | false | false | false | false | false | 8,648 |
776G | Sherlock found a piece of encrypted data which he thinks will be useful to catch Moriarty. The encrypted data consists of two integer _l_ and _r_. He noticed that these integers were in hexadecimal form. He takes each of the integers from _l_ to _r_, and performs the following operations: 1. He lists the distinct digits present in the given number. For example: for 101416, he lists the digits as 1,u20090,u20094. 2. Then he sums respective powers of two for each digit listed in the step above. Like in the above example _sum_u2009=u200921u2009+u200920u2009+u200924u2009=u20091910. 3. He changes the initial number by applying bitwise xor of the initial number and the sum. Example: . Note that xor is done in binary notation. One more example: for integer 1e the sum is _sum_u2009=u200921u2009+u2009214. Letters a, b, c, d, e, f denote hexadecimal digits 10, 11, 12, 13, 14, 15, respertively. Sherlock wants to count the numbers in the range from _l_ to _r_ (both inclusive) which decrease on application of the above four steps. He wants you to answer his _q_ queries for different _l_ and _r_. Input First line contains the integer _q_ (1u2009≤u2009_q_u2009≤u200910000). Each of the next _q_ lines contain two hexadecimal integers _l_ and _r_ (0u2009≤u2009_l_u2009≤u2009_r_u2009<u20091615). The hexadecimal integers are written using digits from 0 to 9 and/or lowercase English letters a, b, c, d, e, f. The hexadecimal integers do not contain extra leading zeros. Output Output _q_ lines, _i_-th line contains answer to the _i_-th query (in decimal notation). Note For the second input, 1416u2009=u20092010 _sum_u2009=u200921u2009+u200924u2009=u200918 Thus, it reduces. And, we can verify that it is the only number in range 1 to 1_e_ that reduces. | 2,900 | false | false | false | true | false | false | false | false | false | false | 6,662 |
983E | In the NN country, there are $$$n$$$ cities, numbered from $$$1$$$ to $$$n$$$, and $$$n - 1$$$ roads, connecting them. There is a roads path between any two cities. There are $$$m$$$ bidirectional bus routes between cities. Buses drive between two cities taking the shortest path with stops in every city they drive through. Travelling by bus, you can travel from any stop on the route to any other. You can travel between cities only by bus. You are interested in $$$q$$$ questions: is it possible to get from one city to another and what is the minimum number of buses you need to use for it? Input The first line contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0— the number of cities. The second line contains $$$n - 1$$$ integers $$$p_2, p_3, ldots, p_n$$$ ($$$1 le p_i < i$$$), where $$$p_i$$$ means that cities $$$p_i$$$ and $$$i$$$ are connected by road. The third line contains a single integer $$$m$$$ ($$$1 le m le 2 cdot 10^5$$$)xa0— the number of bus routes. Each of the next $$$m$$$ lines contains $$$2$$$ integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le n$$$, $$$a eq b$$$), meaning that there is a bus route between cities $$$a$$$ and $$$b$$$. It is possible that there is more than one route between two cities. The next line contains a single integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$)xa0— the number of questions you are interested in. Each of the next $$$q$$$ lines contains $$$2$$$ integers $$$v$$$ and $$$u$$$ ($$$1 le v, u le n$$$, $$$v eq u$$$), meaning that you are interested if it is possible to get from city $$$v$$$ to city $$$u$$$ and what is the minimum number of buses you need to use for it. | 2,800 | false | false | false | false | true | false | false | true | false | false | 5,782 |
466B | Problem - 466B - 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 math *2000 No tag edit access → Contest materials ") ") . The caretaker can enlarge any (possibly both) side of the room by an arbitrary positive integer of meters. Help him change the room so as all _n_ students could live in it and the total area of the room was as small as possible. Input The first line contains three space-separated integers _n_, _a_ and _b_ (1u2009≤u2009_n_,u2009_a_,u2009_b_u2009≤u2009109) — the number of students and the sizes of the room. Output Print three integers _s_, _a_1 and _b_1 (_a_u2009≤u2009_a_1;xa0_b_u2009≤u2009_b_1) — the final area of the room and its sizes. If there are multiple optimal solutions, print any of them. Examples Input 3 3 5 Output 18 3 6 Input 2 4 4 Output 16 4 4 | 2,000 | true | false | false | false | false | false | true | false | false | false | 7,976 |
524C | ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most _k_ bills from it, and the bills may be of at most two distinct denominations. For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at _k_u2009=u200920 such ATM can give sums 100u2009000 burles and 96u2009000 burles, but it cannot give sums 99u2009000 and 101u2009000 burles. Let's suppose that the country uses bills of _n_ distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash _q_ times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the _q_ of requests for cash withdrawal. Input The first line contains two integers _n_, _k_ (1u2009≤u2009_n_u2009≤u20095000, 1u2009≤u2009_k_u2009≤u200920). The next line contains _n_ space-separated integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009107) — the denominations of the bills that are used in the country. Numbers _a__i_ follow in the strictly increasing order. The next line contains integer _q_ (1u2009≤u2009_q_u2009≤u200920) — the number of requests for cash withdrawal that you will make. The next _q_ lines contain numbers _x__i_ (1u2009≤u2009_x__i_u2009≤u20092·108) — the sums of money in burles that you are going to withdraw from the ATM. Output For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print u2009-u20091, if it is impossible to get the corresponding sum. Examples Input 6 20 10 50 100 500 1000 5000 8 4200 100000 95000 96000 99000 10100 2015 9950 Input 5 2 1 2 3 5 8 8 1 3 5 7 9 11 13 15 | 1,900 | false | false | false | false | false | false | false | true | true | false | 7,740 |
801B | You found a mysterious function _f_. The function takes two strings _s_1 and _s_2. These strings must consist only of lowercase English letters, and must be the same length. The output of the function _f_ is another string of the same length. The _i_-th character of the output is equal to the minimum of the _i_-th character of _s_1 and the _i_-th character of _s_2. For example, _f_("ab", "ba") = "aa", and _f_("nzwzl", "zizez") = "niwel". You found two strings _x_ and _y_ of the same length and consisting of only lowercase English letters. Find any string _z_ such that _f_(_x_,u2009_z_)u2009=u2009_y_, or print -1 if no such string _z_ exists. Input The first line of input contains the string _x_. The second line of input contains the string _y_. Both _x_ and _y_ consist only of lowercase English letters, _x_ and _y_ have same length and this length is between 1 and 100. Output If there is no string _z_ such that _f_(_x_,u2009_z_)u2009=u2009_y_, print -1. Otherwise, print a string _z_ such that _f_(_x_,u2009_z_)u2009=u2009_y_. If there are multiple possible answers, print any of them. The string _z_ should be the same length as _x_ and _y_ and consist only of lowercase English letters. Note The first case is from the statement. Another solution for the second case is "zizez" There is no solution for the third case. That is, there is no _z_ such that _f_("ab", _z_)u2009=u2009 "ba". | 900 | false | true | false | false | false | true | false | false | false | false | 6,568 |
1335C | You have $$$n$$$ students under your control and you have to compose exactly two teams consisting of some subset of your students. Each student had his own skill, the $$$i$$$-th student skill is denoted by an integer $$$a_i$$$ (different students can have the same skills). So, about the teams. Firstly, these two teams should have the same size. Two more constraints: The first team should consist of students with distinct skills (i.e. all skills in the first team are unique). The second team should consist of students with the same skills (i.e. all skills in the second team are equal). Note that it is permissible that some student of the first team has the same skill as a student of the second team. Consider some examples (skills are given): $$$[1, 2, 3]$$$, $$$[4, 4]$$$ is not a good pair of teams because sizes should be the same; $$$[1, 1, 2]$$$, $$$[3, 3, 3]$$$ is not a good pair of teams because the first team should not contain students with the same skills; $$$[1, 2, 3]$$$, $$$[3, 4, 4]$$$ is not a good pair of teams because the second team should contain students with the same skills; $$$[1, 2, 3]$$$, $$$[3, 3, 3]$$$ is a good pair of teams; $$$[5]$$$, $$$[6]$$$ is a good pair of teams. Your task is to find the maximum possible size $$$x$$$ for which it is possible to compose a valid pair of teams, where each team size is $$$x$$$ (skills in the first team needed to be unique, skills in the second team should be the same between them). A student cannot be part of more than one team. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of students. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th student. Different students can have the same skills. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each test case, print the answer — the maximum possible size $$$x$$$ for which it is possible to compose a valid pair of teams, where each team size is $$$x$$$. Example Input 4 7 4 2 4 1 4 3 4 5 2 1 5 4 3 1 1 4 1 1 1 3 Note In the first test case of the example, it is possible to construct two teams of size $$$3$$$: the first team is $$$[1, 2, 4]$$$ and the second team is $$$[4, 4, 4]$$$. Note, that there are some other ways to construct two valid teams of size $$$3$$$. | 1,100 | false | true | true | false | false | false | false | true | true | false | 4,028 |
1096F | A permutation of size $$$n$$$ is an array of size $$$n$$$ such that each integer from $$$1$$$ to $$$n$$$ occurs exactly once in this array. An inversion in a permutation $$$p$$$ is a pair of indices $$$(i, j)$$$ such that $$$i > j$$$ and $$$a_i < a_j$$$. For example, a permutation $$$[4, 1, 3, 2]$$$ contains $$$4$$$ inversions: $$$(2, 1)$$$, $$$(3, 1)$$$, $$$(4, 1)$$$, $$$(4, 3)$$$. You are given a permutation $$$p$$$ of size $$$n$$$. However, the numbers on some positions are replaced by $$$-1$$$. Let the valid permutation be such a replacement of $$$-1$$$ in this sequence back to numbers from $$$1$$$ to $$$n$$$ in such a way that the resulting sequence is a permutation of size $$$n$$$. The given sequence was turned into a valid permutation randomly with the equal probability of getting each valid permutation. Calculate the expected total number of inversions in the resulting valid permutation. It can be shown that it is in the form of $$$frac{P}{Q}$$$ where $$$P$$$ and $$$Q$$$ are non-negative integers and $$$Q e 0$$$. Report the value of $$$P cdot Q^{-1} pmod {998244353}$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the length of the sequence. The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$-1 le p_i le n$$$, $$$p_i e 0$$$) — the initial sequence. It is guaranteed that all elements not equal to $$$-1$$$ are pairwise distinct. Output Print a single integer — the expected total number of inversions in the resulting valid permutation. It can be shown that it is in the form of $$$frac{P}{Q}$$$ where $$$P$$$ and $$$Q$$$ are non-negative integers and $$$Q e 0$$$. Report the value of $$$P cdot Q^{-1} pmod {998244353}$$$. Note In the first example two resulting valid permutations are possible: $$$[3, 1, 2]$$$ — $$$2$$$ inversions; $$$[3, 2, 1]$$$ — $$$3$$$ inversions. The expected value is $$$frac{2 cdot 1 + 3 cdot 1}{2} = 2.5$$$. In the second example no $$$-1$$$ are present, thus the only valid permutation is possible — the given one. It has $$$0$$$ inversions. In the third example there are two resulting valid permutations — one with $$$0$$$ inversions and one with $$$1$$$ inversion. | 2,300 | true | false | false | true | false | false | false | false | false | false | 5,226 |
1067E | Problem - 1067E - Codeforces =============== xa0 ]( "Announcement") . Input First line of input contains $$$n$$$ ($$$1 le n le 5 cdot 10^{5}$$$)xa0— number of vertices. Next $$$n-1$$$ lines contains two integers $$$u$$$ $$$v$$$ ($$$1 le u, ,, v le n; ,, u e v$$$)xa0— indices of vertices connected by edge. It is guaranteed that given graph is a tree. Output Print one integerxa0— answer to the problem. Examples Input 3 1 2 2 3 Output 6 Input 4 1 2 1 3 1 4 Output 14 Input 4 1 2 2 3 3 4 Output 18 | 2,800 | true | false | false | true | false | false | false | false | false | false | 5,371 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.