We have \(a^{x} \equiv b (mod \; p)\) and want the value of \(x\). Let's work through this equation a bit. First, this \(x \in [0, p]\), and we can let \(x=im-j\), where \(m=\lceil \sqrt{p} \rceil\). Clearly, \(j \in [0, m], i \in [1, m]\). The equation turns into this:
Continue reading →Chinese original · English translation by Claude Opus 5.5
A fairly bare max-flow problem. We create the source \(s=0\), and have the foods take edges from the source to every cow; each cow then gets a copy node (of course the main node and the copy node are connected, so that it only eats one) that connects to the drink nodes, which then connect to the sink. Then just compute the max flow.
Continue reading →Chinese original · English translation by Claude Opus 5.5
A bare 2-SAT model problem; it can be used as a template problem. Let's analyze it: if the husband in couple \(i\) and the wife in couple \(j\) are in conflict, then that means the husband in couple \(i\) can sit together with the husband in couple \(j\), or the wife in couple \(i\) can sit together with the wife in couple \(j\). Both of these can contribute to the answer, so we add edges.
Continue reading →Chinese original · English translation by Claude Opus 5.5
We can solve this problem with bipartite matching. We map the x- and y-coordinates of each asteroid onto a bipartite graph, then just find the maximum matching.
Continue reading →Chinese original · English translation by Claude Opus 5.5
For each marriage, add a directed edge from the man to the woman; for each lover relationship, add a directed edge from the woman to the man. Then just count the strongly connected components of size one.
Continue reading →Chinese original · English translation by Claude Opus 5.5
First pick a vertex as the root, turning the unrooted tree into a rooted one, then recursively branch out (DFS-like). But to cut down the complexity, we want to keep the recursion depth in a tree as small as possible, so the choice of this vertex matters a lot. We pick the centroid to take care of this. With the centroid, none of our subtrees has size above \(n/2\); once the computation is done, delete the root and recurse into the subtrees. So the time complexity is a nice \(O(n\log n)\).
Continue reading →Chinese original · English translation by Claude Opus 5.5
Given a sequence A[] of length n, you need to answer q queries. Each query gives two endpoints and asks for the number of distinct colors in the interval. n, q < 1e5.
Continue reading →Chinese original · English translation by Claude Opus 5.5
In this problem \(w\) is large, and we need two-dimensional sums. One workable approach is a nested data structure; the first idea is a Fenwick tree of dynamically allocated segment trees, with time complexity \(O(log^2 m)\). But the space is still large: you can see that the segment trees take up a huge amount of space, so consider replacing them with balanced BSTs, which shrinks the space enormously while the time complexity stays the same.
Continue reading →Chinese original · English translation by Claude Opus 5.5