· Olympiad in Informatics

BZOJ1013: [JSOI2008] Sphere Space Generator — Solution

This problem is actually just a textbook problem for Gaussian elimination. Following the statement, we can write equations of the following form:

\[\sum_{i = 1}^n (x_a [i] - x_0 [i])^2 = c\]

where \(c\) is the radius of the sphere. There are \(n+1\) such equations in total; we can subtract consecutive ones to cancel the constant, which builds a system of \(n\) equations. The final system:

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

· Olympiad in Informatics

Educational DP Contest: O – Matching — Solution

Looking at the constraints of this problem, you can think of bitmask \(DP\). Consider designing a state: \(dp[S]\) represents a set of women, where the size of the set \(|S|\) provides another piece of information. In other words, \(dp[S]\) represents the number of transitions for the set of women corresponding to the first \(|S|\) men. It is not hard to derive the following formula:

\[dp[S] = \sum_{(i, |S|) \in compatible} dp[S'], S' \cup i = S\]

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

· Olympiad in Informatics

P2564: [SCOI2009] Birthday Gift — Solution

A fake monotonic queue, heh.

Consider recording the indices of each category's elements, then first put the smallest-numbered element of each category into a multiset, and then update the answer: each time, take out the maximum and the minimum and update with their difference, then update the minimum's index and toss it back into the multiset. If all the categories in the multiset have been fully enumerated, just break out of the loop and output.

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

· Olympiad in Informatics

BZOJ4361: isn — Solution

This problem is really interesting. First, whip up a \(DP\) to count the non-decreasing subsequences of each length; the equation is:

\[f[i, j] = \sum_{k < i, a_k \leq a_i} f[k, j - 1]\]

We notice that it can be optimized with a Fenwick tree, so the complexity here is \(O(n^2 \log n)\). Let \(g[i] = \sum_{i = 1} f[i, n]\), and consider the contribution to the answer. For a given \(i\), there are \(g[i] * (n - i)!\) ways to carry out the transformation, but note that these \((n - i)!\) include the cases where the transformation was already finished early, that is, the cases where we deleted valid elements from a valid sequence, and that definitely won't do. Consider inclusion–exclusion: the contribution is \(g[i] (n-i)! - g[i+1] (n - i - 1)!(i + 1)\), which represents the ways of choosing \(i+1\) valid elements among these.

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

· Olympiad in Informatics

BZOJ4665: Little w's Wedding Candy — Solution

This problem is DP + inclusion–exclusion, which happens to be exactly the type I'm not very good at. Let the state \(f[i][j]\) be “the number of ways in which, after considering the first \(i\) states, \(j\) colors are the same as the original (note that here, a different color is not what makes things essentially distinct)”. We can derive the formula:

\[dp[i][j] = \sum_{k = 0}^j dp[i - 1][j - k] {cnt[i] \choose k} \frac{cnt[i]!}{(cnt[i] - k)!}\]

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

· Olympiad in Informatics

Cayley's Theorem & the Extended Cayley Theorem

Conclusion The number of labeled unrooted trees with \(n\) nodes is \(n^{n-2}\).

This result shows up in many counting problems. To prove it, you first need to know about Prufer sequences. The proof is given below.

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

· Olympiad in Informatics

Codeforces Round #539 (Div. 2) Problem-Solving Report (CF1113)

Actually, this problem is a total freebie. We know the condition is \(x_l \text{ xor } x_{l+1} \dots x_{mid} = x_{mid+1} \text{ xor } x_{mid+2} \dots x_{r}\), but we can move terms straight across in this operation: because \(xor\) is its own inverse. Then, to make sure \(r-l+1\) is even, we just need to maintain odd and even counts at the ends of the Trie.

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

· Olympiad in Informatics

Educational Codeforces Round 59 Problem-Solving Report (CF1107)

True to its name, it's pretty brute-force. Just brute-force sort the values of every contiguous segment, then count them up.

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

· Olympiad in Informatics

P1600: Love Running Every Day — Solution

Let me translate:

You are given a tree with vertex weights and a bunch of paths; for each vertex \(i\), how many paths satisfy \(w(s,i) = weight(i)\)?

Sounds pretty nasty, right? Following the standard trick for this kind of tree problem, consider splitting every path into \((s \to LCA(s,t))\) and \((t \to LCA(s,t))\) and handling them separately. Let's first deal with the \((s \to LCA(s, t))\) kind: suppose we now want to know the contribution of the path \((s \to LCA(s, t))\) to vertex \(i\); then it must satisfy:

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

· Olympiad in Informatics

Monotonic Queue Study Notes

Example problem: CH1201

Given an integer sequence of length n, find a contiguous subsequence of length at most m in it, so that the sum of the whole sequence is maximized.

A solution I wrote earlier: CH1201: Maximum Subsequence Sum — Solution

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