📜 ⬆️ ⬇️

Analysis of the tasks of the World Cup finals about programming ACM ICPC 2013

At the ACM ICPC 2013 World Team Programming Championship a week ago, there were 11 tasks, one of which for a given time could not be solved correctly by any of the teams.

But besides the teams, there are other people who professionally solve problems - the championship analysts. During the broadcast, they are divided into groups, distribute tasks and then talk about them in the studio. A lot of viewers are watching the air until these guys sort out the latest task. In addition, analysts tell the leader what is happening “on the field,” looking for interesting pieces of code, watching the picture from the participants' webcams.

This year, 21 analysts from Sweden, the Netherlands, the USA, Slovakia, Belarus and Russia were at ACM ICPC. And 10 of them were from Yandex. All of them in different years were ICPC winners. Especially for Habr, they dismantled all the tasks of the championship.
')
Analysis of the "Matryoshka" problem during the ACM ICPC 2013 broadcast

Task A. Self-Assembly


One highly scientific chemical institution is actively experimenting with the Automatic Molecule Collector. The molecules, between which the formation of chemical bonds is possible, are mixed and placed in the AFM, where they form complex molecular structures. However, sometimes a problem arises - molecules form such a large group that the Picker jams.

Write a program that will determine whether the provided set of molecules can be assembled into structures of unlimited size. In this case, 1) the task is limited to flat structures and 2) each molecule has the form of a square, all molecules have the same size. The four sides of the square denote surfaces by which molecules can combine with compatible molecules.

In each test you will be presented with a set of descriptions of molecules. Each type of molecule is described by four pairs of symbols - labels of bonds,
which means the ability of the corresponding surface of the molecule to connect with other molecules.

There are two types of link tags:

Suppose that in the Collector there is an unlimited number of molecules of each of the described species that can be rotated and turned over. In the formation of molecular structures, molecules can be nearby only if they are compatible. Each surface of the molecule can be connected to nothing.

The figure shows an example of three types of molecules and a limited size structure assembled from them (this is not the only possible structure for given types of molecules).

Task A ACM ICPC 2013

Input data Input data consists of one test. Each test consists of two lines. The first line contains the number n (1 ≤ n ≤ 40000) - the number of types of molecules. The second line contains n substrings of 8 characters each - a description of each type of molecule, with the surfaces being described clockwise.

Output Output the word unbounded if the molecules can create an infinite structure and the word bounded otherwise.

Problem Solving A
The author of the analysis is Vasily Astakhov, ACM ICPC 2011 bronze medalist in the team of Moscow State University. Mv Lomonosov.

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).

Problem B. Hey, Better Bettor


The recent recession hurt entertainment facilities, including the gambling business. There is fierce competition among casinos, and, in order to attract players, some of them began to hold particularly attractive promotions. Casino promotions include the following: you can play as much as you want. And after you finish, whatever amount you lose from the moment you start, the casino returns x% of your losses. Naturally, if you are the winner, you take it all.

In this case, there are no restrictions on the duration of the game, nor on the amount of money with which you come into the game, but you can use this action only once.

For simplicity, we assume that all bets are worth $ 1, and the gain is $ 2. Now suppose x is equal to 20. If you make only 10 bets before you finish the game, and only 3 of them win, then your total loss will be 3.2 dollars. If 6 bets win, your winnings will be $ 2.

Given x and p (the probability of winning a single bet as a percentage), you need to write a program to determine the maximum expected gain that you can receive using any strategy of the game.

Input data The input data consists of one test that contains the return percentage x (0 ≤ x <100) and the probability of winning in percents p (0 ≤ p ≤ 50). x and p are no more than two digits after the decimal point.

Output Print the maximum expected gain with an absolute error of no more than 10 -3 .

Solution of Problem B
The author of the analysis is Mikhail Levin , bronze medalist of ACM ICPC 2007, silver medalist of ACM ICPC 2008 as part of the team of Moscow State University. Mv Lomonosov.

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 as where 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, and P ( 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.


Problem C. Surely You Congest


You are responsible for the intelligent traffic management system for new cars. Your goal is to avoid traffic jams from drivers commuting from the sleeping areas to the city center during the morning rush hour, using information about the city and the movement of other cars.

Unfortunately, due to the fact that drivers are selfish, they will never follow the shortest possible way to the center, even if you ask them about it. You can only advise them which of the few shortest paths to choose.

The city consists of intersections connected by two-way roads that can be reached in a given time. All drivers begin their movement at intersections (possibly different) and end at the same intersection number 1 designated as the city center. If two drivers simultaneously start driving along the same road in the same direction, a traffic jam will arise and your goal will fail. However, drivers can drive through the same intersection at the same time or drive along the same road, driving in at different times.

Determine the maximum number of drivers who can get to the city center without traffic jams, if all drivers start their movement at the same time and none of them will go the non-optimal way.

image

In the picture, the cars are shown in their initial position. One driver is already in the center, and of the cars at the 4th intersection, one can move along the dotted line through intersection 3, the other along the dotted line through intersection 2, but the remaining two cannot reach the center without traffic jams. Thus, the answer to this test will be 3.

Input data The input contains one test. The first line contains three numbers n, m and c , where n (1≤ n ≤ 25000) is the number of intersections, m (0 ≤ m ≤ 50,000) is the number of roads in the city and c (0 ≤ c ≤ 1000) is the number of drivers. Each of the following m lines contains three numbers x i , y i and t i describing the road, where x i and y i (1≤ x i , y i ≤ n) are the numbers of the different intersections that are connected by the described road and t i ( 1≤ t i ≤ 10000) - the time that the driver must spend to get from the beginning to the end of the road in any direction. It is guaranteed that you can reach the center from any intersection. The last line contains c numbers describing the initial intersections where cars are located.

Output Print the maximum number of drivers who can reach the city center without traffic jams.

Solution of Problem C
The author of the analysis is Mikhail Levin.

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.


Task D. Factors


One of the fundamental theorems of arithmetic says that any number greater than 1 can be uniquely represented as a product of primes. However, this unique set of prime factors can be ordered in various ways. For example:



Let f (k) denote the number of different ways to order the prime factors of the number k . Then f (20) = 3 and f (10) = 2.

You are given a positive integer n , and it is guaranteed that for any n there exists k such that f (k) = n . Determine such minimum k .

Input data The input file contains no more than 1000 tests, each of which is written in a separate line. Each test consists of a positive integer n <2 63 .

Output For each test, output the number n and the minimum k > 1 such that f ( k ) = n . The numbers in the tests are chosen in such a way that k <2 63 .

Solution of Problem D
The author of the analysis is Renat Gimadeev , ACM ICPC 2012 gold medalist in the Fiztekh team.

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.


Task E. Harvard


Task E ACM ICPC 2013. Picture - Wikimedia Commons The term “Harvard Architecture” applies to a computer with physically shared memory for instructions and data. The term comes from the name of the computer Harvard Mark I , created in 1944. It used paper tape for instructions, and a relay for data.

Some modern microcontrollers use "Harvard architecture", but not paper tapes and relays! The data is in banks, each of which contains the same number of cells. Each data access instruction has a byte-offset f within the bank and a bit a , which is used to determine the bank number. If a is 0, then the call is made to the bank with number 0. If a is equal to 1, then the bank number is stored in the special bank selection register (RMB).

For example, suppose we have 4 banks with 8 cells each. You can access cell number 5 in one of two ways: one instruction with a = 0 and f = 5 or two instructions, setting the value of the RVB to 0 and then using the instruction with a = 1 and f = 5 . Obviously, the first method is faster, because you do not need to assign a value to the RVB. Now suppose we need to access cell 20 in the same memory. Now we can apply only one way: execute the instruction, setting the value of the RTD 2 (if the desired value is not already stored in it), and then the instruction with a = 1 and f = 4. The program is a sequence of operations. Each operation is:

Source: https://habr.com/ru/post/186316/


All Articles