This editorial corresponds to 2018 Chinese Multi-University Training, BeihangU Contest (stage 5), which was held on Aug 6th, 2018. Moreover, this problem set was also used as Jingzhe Tang Contest 1 in Petrozavodsk Winter Camp on Jan 30th, 2019.
This post will try to elaborate on notes, solutions and maybe some data generating. Before that finishes, you can refer to an old published material.
102114A - Always Online
This problem requires to calculate $$$s$$$-$$$t$$$ min cut between any two vertices on a weighted cactus graph. Try to find some feature of this graph.
solutionBy contradiction, we can prove that for a connected undirected graph, each edge belongs to at most one cycle if and only if there are at most two non-intersect paths between any two vertices. In addition, the maximum flow problem and the minimum cut problem are equivalent, so we only need to consider how to cut some edges with the lowest sum of capacity so that the source vertex $$$s$$$ is not connected to the sink vertex $$$t$$$.
Let's take a look at the $$$s$$$-$$$t$$$ cut problem. If there are at least two edges in one cycle that are cut, we could figure out by adjusting that at most two of them should be cut, since there are at most two non-intersect paths between $$$s$$$ to $$$t$$$. In addition, if there are many cycles where some edges of each of them are cut, it can be known by adjusting and contradiction that only the edges of at most one cycle of them should be cut.
One observation is that if there is at least one edge in some cycle should be cut in the best solution of the minimum cut problem, there must be two edges in that cycle need to be cut, one of which is the edge with the smallest capacity in that cycle. Hence, we can remove the edge with the smallest capacity in each cycle and add its capacity to the other edges in the cycle, and the maximum flow between any two vertices will not change in value.
The modified graph forms a tree. If we add edges to an empty graph in descending order of its capacity, we can determine $$$\mathrm{flow}(s, t) = w$$$ $$$(s \in S, t \in T)$$$ for the two sets of vertices $$$S$$$ and $$$T$$$ that are firstly connected after joining the edge of capacity $$$w$$$. Just maintain these sets of vertices by the disjoint set union, count in each set the number of vertices whose bit in some fixed position are $$$1$$$, and then it is easy to calculate the answer by enumerating each bit. Time complexity is $$$\mathcal{O}(m \log n)$$$.
noteNote that $$$\mathrm{flow}(s, t)$$$ can be $$$2 \times 10^9$$$, which means the answer may exceed $$$2^{63}$$$. To reach the peak value, we may just construct a circle with edges of the same weight $$$10^9$$$.
When enumerating bits, we can treat the lowest $$$(\log n)$$$ bits and other bits differently, which explains why the time complexity is $$$\mathcal{O}(m \log n)$$$ rather than $$$\mathcal{O}(m \log w)$$$.
102114B - Beautiful Now
This problem is yet another problem related to swapping. Can you solve it simply and elegantly?
solution 1For each permutation of positions, the minimum number of swaps equals to $$$n$$$ minus the number of disjoint cycles in the permutation.
We can solve the problem by enumerating all the $$$n$$$-permutations. For each permutation, we only need to check if it can be reached within $$$k$$$ swaps as swapping the same position is permitted.
Time complexity is expected to be $$$\mathcal{O}(m!)$$$, where $$$m = \left \lfloor \log_{10} n \right \rfloor + 1$$$. In order to achieve that, we can maintain some information about chains and cycles so that we can modify and retrieve efficiently during searching and backtracking.
solution 2For each permutation of positions, the minimum number of swaps equals to $$$n$$$ minus the number of disjoint cycles in the permutation.
We can precalculate the minimum number of steps to obtain a permutation from the original one, and then enumerate and check. Time complexity is $$$\mathcal{O}\left(\sum_{i = 1}^{9}{(i \cdot i!)} + T m!\right)$$$, where $$$m = \left \lfloor \log_{10} n \right \rfloor + 1$$$.
noteSome solution cannot handle the case $$$n = 10^9$$$, but it can be solved by hand.
Many greedy solutions can be hacked even by random tests, so... the dataset is almost randomized. :P
Wait, wait, wait... Does it seem like a notorious coincidence with this problem? What? This problem has an incredible data range... Does it really solvable??? Oh, I can't believe that!!! >_<
102114C - Call It What You Want
This problem asks you to factorize polynomial $$$(x^n - 1)$$$ over the field of the integers. Besides, the statement contains some mathematical formula you may need to apply. Maybe you just need some observation to solve.
solutionAs we know, the Möbius inversion formula can be expressed as
$$$f(n) = \sum_{d | n}{g(d)} \Leftrightarrow g(n) = \sum_{d | n}{\mu(n / d) f(d)}\text{,}$$$where $$$\mu$$$ is the Möbius function and the sums extend to all positive divisors $$$d$$$ of $$$n$$$.
Similarly, we can obtain the following formula.
$$$f(n) = \prod_{d | n}{g(d)} \Leftrightarrow g(n) = \prod_{d | n}{f(d)^{\mu(n / d)}}$$$If we apply this formula, the problem can be solved in $$$\mathcal{O}(2^{\omega(n)} n)$$$, where $$$\omega(n)$$$ means the number of distinct prime factors of $$$n$$$. To prove that, you need to know what the Möbius function is and observe that $$$f(n)$$$ in this problem is a polynomial with only two non-zero terms.
By the way, $$$\omega(n) \leq 6$$$ when $$$n \leq 10^5$$$, because $$$2 \times 3 \times 5 \times 7 \times 11 \times 13 \times 17 = 510510$$$.
noteQ: Why does Fast Fourier Transform get a verdict of TLE? It seems $$$\log n \approx 2^{\omega(n)}$$$ when $$$n \leq 10^5$$$.
A: FFT implies big constant factor. Also, the above solution does not require floating point operation.
102114D - Daylight
This problem can be explained as given an unweighted tree, you need to determine the union of two sets and report its size, where each set is a set of vertices whose distances to a given vertice are no more than a fixed value, and the values for two sets are the same. However, queries are encrypted so you need to handle them one by one (online).
Emmm... A typical data structure problem, right?
Certainly I've found it on CodeChef. What? Why TLE???
solutionThe union is difficult to compute but the intersection is easy, so let's apply $$$|S_u \cup S_v| = |S_u| + |S_v| - |S_u \cap S_v|$$$, where $$$S_x$$$ is the set of vertices whose distances to $$$x$$$ are no more than $$$w$$$.
Note that the tree is not changed, so you can precalculate the structure of the tree's centroid decomposition, in order to calculate $$$|S_u|$$$ online.
Since the restricted distances are the same, when $$$S_u$$$ and $$$S_v$$$ have a non-empty intersection, their intersection equals to the set of vertices whose distance to the middle point of the path between them are no more than $$$\left(w - \frac{dist(u, v)}{2}\right)$$$, which can be counted similarly. To avoid many discussions, you can add extra vertices at each edge of the tree.
Time complexity and space complexity are both $$$\mathcal{O}(n \log n)$$$. However, some solution may lose out due to cache-unfriendly behavior. One way to ease that is to instead discuss the situations.
102114E - Everything Has Changed
A geometry problem to ensure you have checked in this contest. Read the statement for more details.
solutionNote that the cutting areas do not intersect, which implies the different cutting areas do not affect each other, so we can reduce the problem into calculating angles of triangles. Cosine theorem would be helpful.
Anyway, you can paste your geometry coding template to solve. It seems there are many problems similar to this one.
noteThough the data range is small, we should be careful calling functions like acos(x)
when $$$x$$$ should be ranged in $$$[-1, 1]$$$, or otherwise NaN
will give you a lesson.
102114F - Fireflies
Consider all the lattice points in a hypercube $$$\lbrace (x_1, x_2, \ldots, x_n) | 1 \leq x_i \leq p_i \rbrace$$$. Find a maximal subset such that there are no two points $$$(x_1, x_2, \ldots, x_n)$$$, $$$(y_1, y_2, \ldots, y_n)$$$ meeting the condition $$$x_i \leq y_i$$$ for all $$$i$$$. Report its size modulo $$$(10^9 + 7)$$$.
solutionThe statement is a bit different from the above, however, they are equivalent by applying Dilworth's theorem.
We may be familiar to the case that $$$p_i = 2$$$ for all $$$i$$$, which is equal to find a maximal subset of the power set of some set $$$S$$$ such that none of the sets is a strict subset of another. Besides, the conclusion, also known as Sperner's theorem, is that the size of the maximal subset is equal to $$${|S| \choose \left \lfloor |S| / 2 \right \rfloor}$$$.
A generalization of Sperner's theorem could show the answer to this problem is the number of points satisfying $$$\sum_{i}{x_i}$$$ is equal to $$$M = \left \lfloor \frac{1}{2} \sum_{i}{(p_i + 1)} \right \rfloor$$$. The proof is similar so we omit it.
Then, as we solve many counting problem in form of $$$\sum_{i}{x_i} = M~(1 \leq x_i \leq p_i)$$$, we can use inclusion-exclusion principle to get the answer
$$$\sum_{I \subseteq J}{(-1)^{|I|}{M - 1 - \sum_{i \in I}{p_i} \choose n - 1}}\text{,}$$$where $$$J = {1, 2, \ldots, n}$$$.
The remaining part is straightforward. We can regard $$${M - 1 - \sum_{i \in I}{p_i} \choose n - 1}$$$ as a low-degree polynomial of $$$\sum_{i \in I}{p_i}$$$ and use the meet-in-the-middle approach to split $$$I$$$ into two two halves almost evenly. Time complexity is $$$\mathcal{O}(2^{n / 2} n^2)$$$.
Here is an old problem with small data range.