Editorial 1
Editorial 1
Editorial 1
Problem: AB
Proposed by: prof. Ionel-Vasile Pit, -Rada
We use the terms AB-permutation and AB-transposition for permutations and transpositions of
the K elements which obey the AB matrix constraints. (A transposition is a permutation where
exactly two elements are swapped, such as (12435).) We will show that if an AB-permutation
exists, then an AB-transposition also exists.
Recall that any permutation consists of a composition of cycles. For example, the permutation
(42613578) consists of the cycles 3 → 5 → 6 → 3 and 1 → 4 → 1, as well as the single-element
cycles 2, 7 and 8. For any AB-permutation P we will show how to find an AB-transposition
between two elements on the same cycle. Once we find it, we simply put the other elements
of P back on their original positions. This will not break any constraints, because the original
matrix was an AB-matrix.
So let us consider one of the cycles in P , call it x0 , x1 , . . . , xc−1 , xc = x0 . Let [ai , bi ] be the range
of values that can replace xi in the matrix. Obviously, ai ≤ xi ≤ bi for all i. The values of ai , bi
can be deduced from the values neighbouring xi : ai is the maximum of the values to the left
and above xi (if they exist), plus one, and bi is the minimum of the values to the right and below
xi (if they exist) minus one.
Assume without loss of generality that x0 is the minimum value (we can rotate the cycle
if not). Then the values x0 , x1 , . . . will increase for a while, until at some point xd there will
necessarily exist a decrease, xd < xd−1 (d can be equal to c if the cycle consists of exactly one
increasing streak). So there exists some i ≥ 1 such that xi−1 ≤ xd < xi . We will show that the
transposition (xi , xd ) is an AB-transposition. Let us collect some useful inequalities.
(1) ai ≤ xi ≤ bi , because xi obviously obeys its own range.
(2) ad ≤ xd ≤ bd , similarly.
(3) xi−1 ≤ xd < xi ≤ xd−1 , by our choices of d and i.
(4) ai ≤ xi−1 ≤ bi because xi−1 fits in place of xi in the cycle.
(5) xd−1 ≤ bd because xd−1 fits in place of xd in the cycle.
From (1), (3) and (4) we get ai ≤ xi−1 ≤ xd < xi ≤ bi . Therefore, xd obeys the constraints of
xi . Conversely, from (2), (3) and (5) we get ad ≤ xd < xi ≤ xd−1 ≤ bd . Therefore, xi obeys the
constraints of xd . By definition, this means that (xi , xd ) is an AB-transposition.
Now, we will explain how to determine whether there is any AB-transposition. We will
iterate over the values X in increasing order. While iterating, we will keep a stack with all the
previous positions in which the current position can still be placed. In other words, at a step of
the iteration xi , we will keep a stack of all previous positions xj for which xi ≤ aj . Therefore
xi can be placed in place of all positions xj from the stack. For an AB-transposition, we need
to check whether there is any xj that is large enough to fit in [ai , ai ]. For this, it is sufficient to
check the maximum value of the all xj , which is the actually the top of the stack.
Now, regarding keeping the stack up-to-date. At each iteration step, we need to eliminate
all xj which have a bj < xi . It is not necessary to remove them from the middle of the stack,
but instead we will remove them from the top of the stack in a lazy manner. As such, we will
pop all tops of the stack that have bj < xi . Once a top exist for which xi ≤ bj , we check that
top whether it generates a swap with xi . If yes, then the solution is not unique. If not, then we
continue to the next iteration.
Here is pseudo-code following this idea:
(1) Iterate over x1 , . . . , xk , keeping a stack S.
1
EDITORIAL JUNIOR BALKAN OLYMPIAD IN INFORMATICS, DAY 1 2
Problem: MPF
Proposed by: prof. Daniela Lica
Consider the following notations:
• V MAX the maximum value that can be assigned to X, with respect to the problem’s
constraints;
• Maxp[i] the largest prime divisor of the positive integer i, where 1 ≤ i ≤ V MAX. This
can be computed in O(V MAX log V MAX) time complexity using an algorithm similar
to the Sieve of Eratosthenes. When marking elements as non-prime, we save the prime
number that divides it. Because the primes are traversed in increasing order, the last
saved divisor is also the largest. Thus, using operation 1 once, a positive integer X
becomes X/Maxp[X];
• Level[i] the sum of the exponents from the prime factor decomposition of the positive
integer i, where 1 ≤ i ≤ V MAX (this can be computed along with the MaxP [] array, such
that Level[X] = 1 + Level[X/Maxp[X]]). In fact, for an integer i, Level[i] represents the
minimum number of operations of type 1 that need to be applied successively on i for it
to become equal to 1 and, at the same time, the minimum number of type 2 operations
that need to be applied successively on 1 to obtain i;
• Query(i, j) the minimum number of operations that need to be applied successively on
i for it to become equal to j. Obviously Query(i, j) = Query(j, i).
Subtask 1. We precompute the results for each possible pair of numbers, with approximately
2
√ MAX T ) time complexity, where T is the number of prime numbers less than or equal to
O(V
V MAX. We use a 2D array ans[i][j] = k, where Query(i, j) = Query(j, i) = k. Every i-th line
can be computed starting using a fill algorithm, starting from the number i with the help of
a queue data structure (FIFO), processing each number from 1 to V MAX exactly once. When
processing a state X, we try to consider state X/Maxp[X] (type 1 operation applied on X) and
all states j · X, where j is a prime number, j ≥ Maxp[X] (type 2 operation applied on X).
For each query, the output can be provided in O(1) time. Final time complexity for answering
all queries is O(Q).
Subtask 2. √ For each query we will compute the prime factor decomposition of the two numbers
(X, Y ) in O( V MAX) time complexity. Consider the number Z the maximum number obtained
as an intermediary value in both transformations of X to 1 and Y to 1. The value of Z is in
fact the product, in order, of the smallest common prime factors of X and Y . The answer for
Query(X, Y ) = (Level[X] − Leve[Z]) + (Level[Y ] − Level[Z]). This is because it takes Level[X] −
Level[Z] operations of type 1 to transform X into Z, and then another (Level[Y ] − Level[Z])
operations
√ of type 2 to transform Z into Y . The final complexity for all queries is therefore
O(Q · V MAX).
Subtask 3. Similarly to the previous approach, for each query pair (X, Y ), we will determine
the value of Z. The approach will follow the transformation of X and Y to the value 1, until
a common value Z is found. While the two values X and Y are different (i.e. a Z value has
not been found), we will pick the maximum of X and Y and apply an operation of type 1 to
it, thus getting closer to the value of Z. The maximum complexity of one individual query is
determined by the maximum number of prime factors that a value can have - O(log2 V MAX).
The final complexity is O((Q + V MAX) · log2 V MAX)
EDITORIAL JUNIOR BALKAN OLYMPIAD IN INFORMATICS, DAY 1 4
Problem: Roboclean
Proposed by: prof. Zoltan Szabo
Solution proposed by Tamio-Vesa Nakajima. First, observe that the parity of the length of any
path from some point in the grid to some other point in the grid is fixed. To see why, consider a
checkerboard pattern overlaid on top of the grid. If the colour of the starting cell is equal to the
colour of the ending cell, then any path between them has odd length; otherwise, it must have
even length. Thus, observe that if we can always create a path of length N × M or N × M − 1,
then we will always create a path of optimal length — if we create one of length N × M then
it is obviously the longest possible path, and if it is of length N × M − 1 then a longer path
of length N × M is impossible due to parity. We will now describe a recursive algorithm that
always generates such a path.
First, assume without loss of generality that we want to create a path between (1, 1) and (i, j),
and also that i ≥ j, and if i = j then N ≤ M. By rotating and flipping the matrix it is always
possible to reach this case. Note that since (i, j) , (1, 1) it follows that i > 1. Our algorithm will
have two cases.
Case 1, n > 2: Observe that by our conditions it is impossible for (i, j) = (2, M) or for
i = 1. In this case we can therefore always end our path with the sequence (2, M) →
(1, M) → . . . → (1, 1). Thus we can reduce to the case of finding a path from (i, j) to (2, M)
without using the first line — or equivalently finding a path from (i − 1, j) to (1, M) in
an (N − 1) × M matrix.
Case 2, N = 2: In this case we can prove that either (i, j) = (2, 1) or (i, j) = (2, 2). In either
case our path is (i, j) → . . . → (2, M) → (1, M) → . . . → (1, 1).
The algorithm has the following steps:
(1) the matrix is rotated until the exit cell lands on (1, 1), i.e. L2 = 1 and C2 = 1;
(2) the matrix is then transposed (i.e. cell (i, j) becomes (j, i)) in order to obtain L1 ≥ C1 , and
if L1 = C1 we want to obtain N ≤ M;
(3) we add to the path the sequence of cells (1, 1), (1, 2), . . . , (1, M), (2, M), and from here we
recursively solve the smaller task of finding the best path from (2, M) to (L1 , C1 ), which
is equivalent to finding a path from (1, M) to (L1 − 1, C1 ) in a (N − 1) × M matrix;
(4) the steps are repeated until we obtain a matrix with only 2 rows for which we construct
the path using the method mentioned in the second case of the above demonstration.
A careful implementation is needed to remap the North, South, East and West directions
when rotating or transposing the matrix.
We will look at the path generated by the proposed approach on a couple of examples.
Example 1. Considering N = 6, M = 9, the starting cell at (4, 6), and the exit cell at (6, 1).
All cells are thus cleaned by the robot and the sequence of moves is the following:
ENWWSSEEENNNWWWWSSSSEEEEENNNNNWWWWWWSSSSWNNNNNWSSSSS
EDITORIAL JUNIOR BALKAN OLYMPIAD IN INFORMATICS, DAY 1 5
Example 2. Considering N = 5, M = 9, the starting cell at (2, 3), and the exit cell at (5, 9).
In this example a single cell is left uncleaned. The sequence of moves is as follows:
ESWWWNNEEEESSSWWWWSEEEEENNNNESSSSENNNNESSSS
Solution proposed by Cristian Frâncu. An alternative solution, to compute the path in reverse:
(1) At each step consider the (at most) four possible moves to the neighboring cells;
(2) Choose the cell that’s farthest from the robot’s start position, using the Euclidean
distance;
(3) In case of equal distance choose the cell that has more possible following moves (i.e. more
adjacent neighbors not yet in the path);
(4) Repeat until reaching the starting point.
This algorithm generates the same paths as the previous solution on the examples mentioned
above.
EDITORIAL JUNIOR BALKAN OLYMPIAD IN INFORMATICS, DAY 1 6
Scientific Committee
The problems were prepared by:
• Gheorghe-Eugen Nodea (chair) - “Tudor Vladimirescu” National College, Târgu Jiu
• Adrian Panaete - “A.T. Laurian” National College, Botos, ani
• Daniela Elena Lica - Centre of Excellence, Ploies, ti
• Ionel-Vasile Pit, -Rada - “Traian” National College, Drobeta Turnu Severin
• Zoltan Szabo - County School Inspectorate, Mures,
• Radu Voroneanu - Google
• Vlad Gavrilă - University of Cambridge
• Emanuela Cerchez - “Emil Racovit, ă” National College, Ias, i
• Marinel S, erban - “Emil Racovit, ă” National College, Ias, i
• Mihai Bunget - “Tudor Vladimirescu” National College, Târgu Jiu
• Tamio-Vesa Nakajima - Oxford, Computer Science department, UK
• Bogdan Iordache - University of Bucharest
• Cristian Frâncu - Clubul Nerdvana Bucures, ti
• Cosmin Pit, Rada - Bolt
• Ciprian-Daniel Ches, că - “Grigore C. Moisil” Technological High School, Buzău
• Marius Nicoli - “Frat, ii Buzes, ti“ National College, Craiova
• Dan Narcis Pracsiu - “Emil Racovit, ă” Theoretical High School, Vaslui
• Flavius Boian - “Spiru Haret” National College, Târgu Jiu
• Petru Simion Oprit, ă - Liceul Regina Maria Dorohoi
• Andrei-Costin Constantinescu - ETH Zurich