· Olympiad in Informatics

P3846: [TJOI2007] Cute Prime — Solution

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

· Olympiad in Informatics

P2891: [USACO2007OPEN] Dining — Solution

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

· Olympiad in Informatics

HDU3062: Party — Solution

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

· Olympiad in Informatics

Graph Theory Study Notes

  1. In a directed graph, each vertex belongs to exactly one strongly connected component.
  2. Strongly connected components are transitive.
  3. A maximal strongly connected component means a strongly connected component that cannot be enlarged any further.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

POJ3041: [USACO2006NOV] Asteriod — Solution

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

· Olympiad in Informatics

BZOJ2140: Stable Marriage — Solution

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

· Olympiad in Informatics

P4149: [IOI2011] Race — Solution

For this problem we can just do centroid decomposition directly. We need to implement the following operations:

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

Divide and Conquer & Sqrt Decomposition Study Notes

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

· Olympiad in Informatics

Counting Colors — Solution

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

· Olympiad in Informatics

BZOJ1176: [Balkan2007] Mokia — Solution

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