It's fairly obvious that this is an interval-expansion problem, expanding monotonically toward both sides.
A normal brute force could do the expansion in \(n^2\), but this problem clearly has monotonicity, so we can try binary search to optimize away one factor of \(n\). Binary search for a range, then use a sparse table or a segment tree or something like that to query the farthest distance within the region, and then we can check.
The code is a bit long and a bit complicated.
Continue reading →Chinese original · English translation by Claude Opus 5.5
Going back over a problem I did a year ago, it really does feel easy now.
Consider writing it as \(d' \in A, d = d' + xP\), then count how many \(x\) make \(d\) land in \(B\). Brute force just enumerates directly. To optimize the brute force, we notice that there must be a cycle here, of length \(\text{lcm}(P, Q)\), which allows a slight optimization.
Let's think of this modulo \(Q\) as a circular strip, then, for starting position \(start_pos\), mark the \(d \in B\) on this paper strip and take prefix sums. To make it easy to tell the order in which prefix sums are added and subtracted, we also need to mark ranks.
Continue reading →Chinese original · English translation by Claude Opus 5.5
There is a fairly obvious brute-force algorithm: consider the two cases of odd and even length, and expanding through same-colored vertices with BFS does the job. But the complexity is high: the BFS effectively takes \(\Theta(n^2 + m^2)\) time.
Continue reading →Chinese original · English translation by Claude Opus 5.5
When I first saw this problem I was completely lost... After looking at it carefully I could roughly figure it out: fix a root, then do a DP to get the counts, then distribute the counts among subtrees of the same shape. (But I couldn't write it.)
Concretely, the way to turn it into a rooted tree is to use the centroid as the root (if there are two centroids, add a virtual root), and after each DFS, sort by hash value, then do the transitions group by group.
When transitioning, you can label each case, then use the combinatorial meaning of prefixes to do the transition. Specifically: \(dp[i] = \sum_{uniqueType \in subtree} {dp[uniqueType] + |subtree| - 1 \choose |subtree|}\).
Continue reading →Chinese original · English translation by Claude Opus 5.5
Consider three forms of shortest path: \(A \times dist, \lfloor \frac{dist}{2} \rfloor B + (dist \bmod 2) A, kB\). The first two just take one BFS. The third has a special requirement on the path: there must be no edge outside the edge set that forms a triangle with some two edges of the path.
Speaking of triangles, we can imitate that square-root method: mark first, then enumerate the middle edge. However, here we can't split the edges into one-way edges, so doing the square-root method directly is bogus.
Continue reading →Chinese original · English translation by Claude Opus 5.5
A pattern-spotting problem. As long as you can spot the property that the range of a valid sequence is at most \(1\), you can just compute it. Directly: