· Olympiad in Informatics

LibreOJ 3096: [SNOI2019] Number Theory — Solution

First, within the residue system of \(Q\) we enumerate numbers \(x_0\), and write each of them as \(x = (x_0 + k * P) \mod Q\). Thinking about it a bit, we find that these numbers change with \(k\), and they show a periodic pattern: that is, these numbers lie on a cycle. We can consider recording, for every number in the residue system of \(Q\), its contribution in \(P\), recording it as prefix sums, and when computing the answer, compare ranks to determine the direction around the cycle (the long arc or the short arc).

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

· Olympiad in Informatics

P1447: [NOI2010] Energy Harvesting — Solution

This is a textbook problem that tests knowledge of Möbius inversion and Dirichlet convolution.

Here we stipulate \(n<m\); swapping the two does not affect the answer. Then let's observe the counting pattern: we find that each point contributes at least \(1\) to the answer, and the extra contribution is actually twice the number of lattice points (points with integer coordinates) on the current segment other than the point itself. In other words:

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

· Olympiad in Informatics

P3172: [CQOI2015] Choosing Numbers — Solution

At first I had no clue at all; after reading the solution it all suddenly clicked (duh, doesn't everything click once you've read the solution?). The problem asks us to compute:

\[ans = \underbrace{ \sum_{a_1 = L}^R \dots \sum_{a_n = L}^R }_{n \text{ of them}} [\gcd_{i = 1}^n (a_i) = k]\]

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

· Olympiad in Informatics

P3768: A Simple Math Problem — Solution

The problem simply asks for:

\[( \sum_{i = 1}^n \sum_{j = 1}^n ij gcd(i, j) ) \mod p\]

The concise statement is very pleasant. With \(gcd\) showing up, we can think in the direction of inversion. First, the usual standard trick: enumerate the \(gcd\). For convenience of writing, I'll drop the modulo here.

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

· Olympiad in Informatics

Möbius Inversion and Du's Sieve

Math problems in OI contests at the provincial team selection level and above are much harder than NOIp math problems. In this post I'll record some of what I took away from learning Möbius inversion and Du's sieve. Here is the outline of this post:

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

· Olympiad in Informatics

P3312: [SDOI2014] Number Table — Solution

Good problem.

First, let's write out the brute-force solution to this problem:

\[answer = \sum_{i = 1}^n \sum_{j = 1}^m \sigma_1 (gcd(i, j)) [\sigma_1 (gcd(i, j)) \leq limit]\]

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

· Olympiad in Informatics

P3830: [SHOI2012] Random Tree — Solution

The first question is fairly easy. Let the state \(f[i]\) be the expected depth when there are \(i\) leaf nodes. Considering that a split from the previous state contributes an extra \(f[i-1]+2\) to the total depth, we know:

\[f[i] = \frac{f[i-1] * (i-1) + f[i-1] + 2}{i} = f[i-1] + \frac{2}{i}\]

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

· Olympiad in Informatics

P3239: [HNOI2015] King Arthur — Solution

A probability and expectation DP problem.

Our state won't take the current round into account: that would be too hard to design, and I'm too weak to manage it. Let's design a global state: \(f[i][j]\) is the probability that, considering the first \(i\) cards, only \(j\) cards have been activated by the time the game ends. Then the answer is:

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

· Olympiad in Informatics

[Codeforces 113D] Museum — Solution

This problem is still pretty godlike: the formula is fairly easy to derive, but the Gaussian elimination part is a bit of a pain.

First we enumerate the destination \(t\), and let the state \(f[i][j]\) be the probability that the people at \(i,j\) transition to \(t\). Consider the following formula:

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

· Olympiad in Informatics

BZOJ2298: [HAOI2011] problem a — Solution

This DP problem is very well set: it tests a contestant's ability to transform the model.

Person \(i\) says: “There are \(ai\) people who scored higher than me, and \(bi\) people who scored lower than me.”

Each statement can be seen as a segment \([a_i+1,n-b_i]\) on a one-dimensional coordinate line, meaning the people on this segment have equal scores. Clearly, in every valid case these segments do not intersect (complete overlap is allowed). So sort the segments by left endpoint, and this \(dp\) equation, keyed by coordinate, should be written as:

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