· Olympiad in Informatics

Codeforces 1136D: Nastya Is Buying Lunch — Solution

The main approach to this problem is greedy. We enumerate \(i\) from \(n-1\) down to \(1\). Let \(vis[i]\) denote whether the node numbered \(i\) can be transferred, and \(arr[i]\) the number at position \(i\); in the set of transfer relations, we look for \((i,j), vis[j]=true\) and find those \(j\). Since we transfer from back to front, the positions of the \(j\) we find are all after \(i\), which meets the transfer requirement. Then we check whether the answer transferred so far \(ans\), the number of relations satisfying the condition \(cnt\), and \(i\) sum to \(n\). If they do, that means the transfer meets the condition, and we perform the transfer directly; if not, we put the mark on this node, meaning it can be transferred from now on.

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

· Olympiad in Informatics

Codeforces Round #548 (Div. 2) Problem-Solving Report (CF1139)

Very simple: while scanning, if the current digit is even, just add the current position to the answer.

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

· Olympiad in Informatics

P1450: [HAOI2008] Coin Shopping — Solution

Let’s first consider the unrestricted version, that is, ignoring the coin-count limits in the queries. We can just hack at it with an unbounded knapsack, and the complexity is linear.

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

· Olympiad in Informatics

Difference on Trees

The prerequisites for learning difference on trees are:

  • Lowest common ancestor (LCA)
  • Linear difference arrays

Once you have learned these, we can start learning difference on trees. The idea behind difference on trees is similar to difference arrays: when we have no choice but to take prefix sums, turn range operations into single-point operations to lower the complexity. Difference on trees has appeared twice in NOIp-series contests, so it is well worth learning (I suspect the problem setters didn't dare set heavy-light decomposition, but still wanted to test how familiar contestants are with operations on trees, so they went with this cheap trick of a thing). Let's first learn a basic operation: vertex range add on a path.

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

· Olympiad in Informatics

P1829: [National Training Team] Crash's Number Table — Solution

First, we stipulate that \(N<M\). Then write out the expression that counts the answer:

\[\begin{gathered} \sum_{i=1}^N \sum_{j=1}^M lcm(i,j) \\ \sum_{i=1}^N \sum_{j=1}^M \frac{ij}{\gcd(i,j)} \end{gathered}\]

Then we can try enumerating the greatest common divisor \(gcd(i,j)=d\), and then let \(i,j\) become the only two coprime factors.

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

· Olympiad in Informatics

P3327: [SDOI2015] Sum of Divisor Counts — Solution

This problem is still fairly interesting (ugh, I had to read the solution for ages before I could write it, I’m so weak). The main thing is turning \(d\) into:

\[d(xy) = \sum_{i|x}^x \sum_{j|y}^y [\gcd(i,j) = 1]\]

You can get a hand-wavy intuition for it: every time a divisor is enumerated, it is counted only when the two are coprime, which avoids counting anything twice.

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

· Olympiad in Informatics

P2257: YY's GCD — Solution

First, we can get hold of a formula that counts the answer:

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

· Olympiad in Informatics

LibreOJ 6005: [24 Network Flow Problems] Longest Increasing Subsequence — Solution

This problem is still pretty interesting; the way the graph is built is really clever. First, we solve the DP for the first question in \(O(n \log n)\) time, and along the way get an array \(f[i]\), meaning the length of the longest subsequence ending at \(i\). When building the graph, note that the vertex for each position has to be split into two: this is to keep the answer unique. So whenever \(f[i]=1\) for vertex \(i\), add the edge \((s,i)\); whenever \(f[i]=k\) for vertex \(i\), add the edge \((i+n,t)\). After that, if we add edges for \((u,v), f[v] = f[u] + 1, arr[u] \leq arr[v]\), just run a max flow and the answer comes out.

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

· Things

March Summary & the Start of a New Stage

It's been a long time since I wrote a blog post; I've been busy with regular classes lately, so I haven't updated the blog. This month went pretty well. I studied regular classes for a long time, got crushed in every way by the big shots in my class, and in the end my monthly exam just flat-out blew up. That's a fairly ordinary thing, I suppose. The Lüyin Cup football matches were pretty fun, and the gods in my class played really well.

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

· Things

Crushed by Regular Classes & Taking the New Policy Zen-Style

I've been back for over a week now, and everything has gone pretty smoothly (especially getting out of the winter break homework), except that I came down with a fever over the weekend, and my throat still really hurts. These are all just little episodes, nothing serious.

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