The task requires to understand whether there is an unlimited molecular structure in which the molecules have the form of squares with different bonds on the sides. Given the availability of rotation and reflection operations, to solve the problem, it was necessary and sufficient to understand whether a cycle of molecules exists. Then, for example, shifting all the time to the right and down, one would get an unbounded structure on the plane, while no connections, except those adjacent to the cycle, would be involved in the molecule. To find the cycle, it would be possible to construct a graph in which the vertices are types of compounds, and edges from the type of compound X [+/−] go to all types of compounds Y , for which there exists a molecule containing both Y and X [- / +] (double top). Then, finding a cycle in such a graph, we obtain the existence of a cycle in the original one. The task of finding a cycle in a directed graph on 52 vertices ( A +, A -, ..., Z +, Z -) is simple and can be solved, for example, by constructing strong connectivity components (the existence of a component larger than one vertex will mean a cycle).
Note that the decision to stop the game now depends only on the current gain or loss of the player - the story does not matter. It can be shown that the optimal strategy in this case always has the form “stop, if you have winnings A or lose B ”, where A and B are some non-negative integers. With fixed A and B, in order to calculate the expected profit, you need to be able to solve this problem: to find the probability P ( A , B ) that the player at some point reaches win A , while never before having a loss B or more. If this problem is solved, the expected profit of the strategy is calculated aswhere x % is the discount provided by the casino to the player in case of loss.
For the numbers P ( A , B ), the recurrence relation holds:,
where p is the probability of winning each time a coin is thrown. Moreover,P (0, A + B ) = 1, andP ( A + B , 0) = 0. If we solve this linear recurrent of order 2, we get,
Where.
If we could go through all the possible pairs ( A , B ) for each input ( x , p ), then we would do it, and then choose the best one. Par ( A , B ) is an infinite number, but if you conduct several numerical experiments, you can see that as A and B increase, the expected profit first increases, and then begins to fall (you can prove the convexity of the expected profit as a function of A and B ), therefore always enough to check only a finite number of pairs. In addition, it can be noted that the best values for A and B are the larger, the closer p is to 0.5, and x - to 100. The worst possible case is p = 0.4999, x = 99.99. It turns out that in this case it is enough to go through A up to 21000, and B - up to 2500, and such a search is enough for the solution to pass through time limits.
We calculate the shortest distances d ( u ) from all vertices u to the final vertex e with the help of the Dijkstra algorithm with a bunch of O ( m log n ). Since everyone wants to go only along the shortest path, cars will only travel along such edges ( u , v ) in the direction from u to v , that d ( u ) = d ( v ) + l ( u , v ), where l ( u , v ) is the length of the edge ( u , v ). We construct a new directed graph consisting only of such edges.
For any two machines that began to move simultaneously and moving from their starting points to the ending point with the same speed, the difference in distances to the final vertex remains always constant. Therefore, two cars, starting at different distances, can never drive along the same edge at the same time. Group all machines by distance from the end point. Then for each group it is necessary to find the largest number of pairwise disjoint path edges from all starting vertices to the final vertex in the new oriented graph. This problem is solved using the algorithm for finding the maximum flow in the graph: add a source to the graph and draw edges from it to all vertices, in which there are machines with a capacity equal to the number of machines at the top. We make all the edges of the oriented graph throughput 1. If we find in this graph the maximum flow from the source to the final vertex, then we’ll get the maximum number of pairwise non-intersecting paths along the edges.
Let us find the maximum flow for each group of vertices at one distance from the final one and add the answers - this will be the answer to the problem. It takes O ( m ) to find one increasing flow path, and in total such paths will be found not more than c , therefore, in total, all starts to find the maximum flow will be performed in no more than O ( mc ). Total complexity of the solution is O ( m log n + mc ), which goes into the given constraints.
This is a content simple task that has many different solutions. I will give one of the shortest.
Note that if we decompose into simple factors n = p 1 k 1 p 2 k 2 ... p t k t , then the number of ordered representations in the form of a product of simple will be equal to. This follows from simple combinatorial arguments: you can order N different prime factors N! in ways. If among these factors there are identical, then all orderings, which differ only in the order of identical factors, actually coincide. Each group of k i identical factors can be rearranged k i ! in ways. Therefore, each unique permutation was counted k 1 ! K 2 ! ... k t ! times and this number must be divided. This formula allows us to quickly calculate f ( k ), knowing the decomposition of the number k into prime factors.
Further, it should be noted that the number of orderings does not depend on what is equal to p i and how k i are ordered among themselves. Therefore, if we are looking for the minimum number, then we should look for it among the numbers of the form n = 2 k 1 3 k 2 5 k 3 ..., and k 1 ≥k 2 ≥k 3 ≥ ... It turns out that such numbers among the numbers less than 2 63 not so much (in the condition they are asked to search for only those solutions that are less than 2 63 ), there are about 40,000 of them and they are easily generated by recursion. For each of them, you can find f (k) using the formula described above and store in the associative array the minimum values of the number k for each obtained f (k ). After that, each request from the condition is processed during the access to this associative array.
E
, where n is a positive integer, and — , n .
Rn E ;
Vi , i 1 ≤ i ≤ min ( bs; 13 );
10 12 .
Rn E ;
Vi , i 1 ≤ i ≤ min ( bs; 13 );
10 12 .
You can make the observation that there are quite a few interesting mappings of a set of variables on the cells of the data banks to sort them all out. Namely (the cells involved in the display will be called filled, similarly to banks, if all their cells are filled):
- We can assume that the zero bank is full, if there is another bank with a filled cell, because access to the zero bank is free.
- Banks with numbers greater than zero are identical, so we can assume that the minimum numbers of variables displayed in the cells of these banks are ordered, for example, in ascending order.
- We can assume that the total number of filled cells in any two banks is greater than s .
Taking into account the cutoffs 1 - 3 and the limitations of the input data of interesting maps about 3 · 10 6 .
Since recourse to a zero bank is free, you can remove variables displayed on this bank from the program. For each pair of remaining variables i and j, we can predict C ij - the cost of the operation of accessing the variable j after accessing the variable i , and then it remains to simulate the program's work.
This task is one of the easiest. It is necessary to choose from 2 nk n pairs (let's call them representatives) of the batteries, each of which
It has a minimum power on its chip, so that the maximum difference in output power of the batteries in a pair is minimal. Suppose that d is fixed, then you can find out whether there are representatives such that the maximum does not exceed d . We sort the powers p 1 ≤ p 2 ... ≤ p 2nk and we will greedily type pairs: the first pair will get the batteries p 1 and p 2 . If p 2 - p 1 > d , then representatives cannot be selected. In the second pair, we take batteries ( p i2 , p i2 + 1 ) with a difference of no more than d and a minimum index i 2 ≤ 2 k + 1, if possible. Similarly, in the third pair - ( p i3 , p i3 + 1 ), i3 ≤ 4k + 1.
If using this algorithm fails to dial n pairs, then this cannot be done. The answer will find a binary search.
The solution to this problem is divided into two parts: the generation of options for the location of the polygon and counting the number of occupied cells for each of the options for the location.Variations generation
Note that if we have found some solution, then it can be “moved” until the polygon rests on the grid (that is, further movement can change the set of selected cells). In fact, there are several cases when a polygon rests on a grid:
- one of the vertices of the polygon falls on the grid line;
- one of the edges of the polygon stumbles onto a grid node.
When one of these conditions is met, a polygon still has some degree of freedom:
- if the vertex hits the grid line, you can move the polygon along this line;
- in the case of an edge hitting the grid node, you can move the polygon along the edge.
We will make a reservation in advance that shifts that differ in each of the coordinates by a number that is a multiple of the grid step will be considered equal.
Therefore, in fact, there are three options for selecting the “stop” so that the position of the polygon relative to the grid is uniquely defined.
- One of the vertices falls on the vertical line of the grid, and one of the vertices falls on the horizontal line of the grid. This includes the case when one vertex falls into a grid node. That is, the emphasis is uniquely defined by a pair of vertices, total options - n 2 .
- One of the vertices falls on a horizontal or vertical grid line. Without loss of generality, we assume that this grid line is vertical and is given by the equation x = 0.
Now suppose that one of the edges, which is not vertical, falls on a node. To select the shift, it is enough to choose the abscissa of this node, and this can be done in no more than m + 1 ways. For m is the number 10. Total options - n 2 ( m + 1).- Two non-parallel edges abut the mesh node. Here, in order to determine the shift of the polygon, in addition to the edges, it is necessary to fix the “difference” between those grid nodes against which these edges rest. Total - n 2 ( m + 1) 2 options.
As a result, in the worst case, we obtain O ( n 2 m 2 ) polygon shift selection options. Since for each of the options, then you will need to count the number of occupied cells, you need to generate a list of all the options, clear duplicates from it: they say that it helped to reduce the number of options in the "maximum" tests by 3-5 times.
It is worth noting that many teams forgot to consider some of these three cases (most of the teams stopped only before the first one).Calculation of occupied cells
The second difficulty in this task was to effectively calculate the number of cells occupied by a given shift. The naive method of counting - for ( m + 1) 2 cells, check for the presence of an intersection with a polygon - works in O ( m 2 n ) and does not fit into the time limit. It turns out that it will be more efficient to count the number of occupied cells in each of the m + 1 columns. To do this, consider continuous pieces of the polygon border passing through the column: either a piece of the border “crosses” the column (one end of the piece is on the right edge of the column,
and the other end is on the left), or both ends of the piece are on the same border of the column. It is possible to prove that the pieces of the first type are divided into pairs, and all the cells between these pieces will be occupied. After that, the resulting cells need to add cells, through which the pieces of the second type pass. This method can be implemented for O ( m ( m + n )) = O ( mn ).
The total complexity of the algorithm for this problem is O ( n 3 m 3 ).
Let's say that the value of F i is the minimum number of matryoshka openings that must be done in order to break the first i matryoshka into groups. Then F 0 = 0 and F i = min 0≤j <i F j + G j + 1, i , where the value of G l , r is the minimum number of discoveries that must be made to fold the matryoshka from l to r inclusive in one (numbering of matryoshkas from one), and on the interval from j + 1 to i inclusive, all sizes of matryoshkas are different and do not exceed i - j . Then the answer to the problem will be F n , and the complexity of the solution without taking into account the calculation of the values of G will be O ( n 2 ). For G i , i, the value is always 0, and for G i , j and i <j, at the last stage, the two groups of matryoshkas are combined. Let k be the number of the matryoshka on which the left group ends. Then G i , j = min i ≤ k <j G i , k + G k + 1, j + C i , k , j , where C i , k , j is the minimum number of matryoshka openings that will be required for in order to unite the groups of matryoshkas from the i- th to the k- th and c k + 1-th to the j- th. Let the value of M i , j be the minimum size of the matryoshka on the interval from i to j and the value of K i , j , s be the number of matryoshka on the interval from i to j , whose dimensions are less than s . Then C i , k , j can be calculated as j - i + 1 - t , where t is the number of nested dolls that will not be opened when combined, that is, t = K k + 1, j, M i, j + K i , j , M k +1, j (you can see that one of the items will always be zero). The complexity of calculating K and G is equal to.
, h · w , d ,, S — . , h · w, , d . . ( ( a,b )) , .
. , x 0 x 1( x 0 < x 1 , x 1 - x 0 ≤ b ) . , , x 0 x 1 . ( n 3 ). , w = x 1 - x 0 ≤ a , yb , y a . : ( h ), . . y . , , .
( ), . , ≥ . , . , O( n 2 · m · log m ), log .
: , .
( , ). O — , n P 1 , P 2 , …, P n , n - ( x i , y i ). , P n +1 = P 1 .,
S ( O P i P i +1 ) — O , P i P i +1 :.
, :
n , «» , , , ( ).
. , , , . : O P i P i +1 , , .
? P i P i +1 . .
, ( P 1 P 2 P 3 P 4 ), ( P 5 P 1 ) ( P 2 P 3 P 4 P 5 ).
O P i P i +1 (, O P 5 P 1 ). ( ), , . .
,.
void prePrint(TNode t) { output(t.value); if (t.left != null) prePrint(t.left); if (t.right != null) prePrint(t.right); } void inPrint(TNode t) { if (t.left != null) prePrint(t.left); output(t.value); if (t.right != null) prePrint(t.right); } void postPrint(TNode t) { if (t.left != null) prePrint(t.left); if (t.right != null) prePrint(t.right); output(t.value); }
. 90. . . S ( f 1 , f 2 , f 3 , s 1 , s 2 , s 3 , l ) — , , f i , i - , s i l , — , — .
, O( n 2 ).
3 3 n 4 , , 3 3 n 2 , n ≤ 26 .
Source: https://habr.com/ru/post/186316/
All Articles