Jump to attached files
Divide by three, multiply by two
Giordano Colli
+TAGS: Sorting, Graphs, Binary Search
+Difficulty: 1400
+Description: A sorting Trick is used into the main proof
+Problem Link: Codeforces Link
Problem Definition
Given an array
It is guaranteed that such rearrangement exists.
Example
Input:Output:
Explanation: starting from
Graph Approach
An intuitive representation of the problem is a directed graphIt is known that finding a Hamiltonian path is an NP-hard problem even on graphs with bounded degrees.
Note that the in-degree plus the out-degree of each node is at most
Observing the graph lets us deduce an interesting property, check the following example:
Lemma 3.1 Â The graph
is acyclic.
Since Proof. Consider a Cycle
.
Each edge of the cycle can be considered an operation that:
Since
are integer numbers greater or equal than
and
is not an integer number,
no matter the choices of
and
.
Each edge of the cycle can be considered an operation that:
- doubles the value of
- divides by 3 the value of
Suppose that there are edges into a cycle,
multiplying by
operations and
dividing by
operations. Clearly
.
A cycle of size can exists if the final result of this cycle is
itself, then
- Inserting vertices into
costs
time.
- Checking if there are vertices to attach edges costs
time for each edge. Moreover, there are
edges since the maximum degree is
.
- Topological sort costs
time, since
.
MEMORY:
The main bottleneck is the construction of the graph.
Checking if there are
- Sorting
costs
time.
- Inserting vertices into
costs
time .
- Checking if there are vertices to attach edges costs
time for each edge. There are
edges since the maximum degree is
. This implies that the total time is
- Topological sort costs
time, since
.
MEMORY:
Lower Bound
A trivial lower bound of this problem can be found by inspecting instances.Consider an instance that has only power of
There is a single feasible permutation of elements:
Sorting Approach
This type of reasoning can be used if it is easy to see that the problem is about sorting elementsSince there must be a total order relation, try to find it.
Think to the solution and call it elements
Call
Proposition 5.1 Â
//in the optimal solution
If Proof. There are 2 cases:
In other words, is not possible to increment
Think to the decomposition of
The algorithm is based on the fact that if
If there are
- Calculating
takes
time, since only values like
are tested.
- Sorting lexicographically takes
time.
MEMORY:
This algorithm is pseudo-polynomial, but if values of
TRICK: Calculating
Think of how is defined
The first
The second
Note that you can not precalculate all the possible