First, I strongly recommend reading this article. (If you get it, there's no need to come here.)
The Miller-Robin algorithm has two parts: one is the Fermat test, and the other is quadratic probing. These two things sound very black magic (and they really are), so I'll explain them clearly and include the code. The number we are testing is \(p\).
Continue reading →Chinese original · English translation by Claude Opus 5.5
This problem is mainly about greedy. First, we can establish that the following cases definitely have no solution:
The first character is ')'; the last character is '('.
The length of the string is odd.
We notice that in the whole string \(s[1 \dots n]\), the first and last characters must be '(' and ')'. So we only need to care about \(s[2 \dots n-1]\). Count the number of parentheses that need to be filled in: that is, \((n-2)\) minus the number of parentheses already determined, split into \(remL\), the number of left parentheses to fill in, and \(remR\), the number of right parentheses to fill in.
Continue reading →Chinese original · English translation by Claude Opus 5.5
This problem can be seen as a network flow model. We color the board in red and black. Then the source connects to the red points, with the max flow capped at the red point's weight; all the black points connect to the sink, with the max flow capped at the black point's weight. The answer is the total point weight minus the max flow.
Let's try to make sense of it: for the case \(m = 1, n = 2\):
\[[ x, y ]\]
Continue reading →Chinese original · English translation by Claude Opus 5.5
This problem is really just testing how to use the Next array from the KMP algorithm. Restated, the problem is roughly:
Given two 01 strings, construct a string with the same length and the same number of 1s as the first string, such that the second string occurs as many times as possible; occurrences may overlap.
Continue reading →Chinese original · English translation by Claude Opus 5.5
This problem counts as a basic problem for CDQ divide and conquer.
First, the main way CDQ divide and conquer works is to use divide and conquer to fix the operations and queries within an interval, turning them into static subproblems to solve (the premise is that the answer to every query is affected by past operations, and that these operations affect the problem independently; that is, answering a query is the superposition of independent past operations, and the answer is not affected by their order).
Continue reading →Chinese original · English translation by Claude Opus 5.5
where \(up, left, right, down\) denote the numbers of evergreen trees above, below, to the left, and to the right. How do we bring the complexity down?
Continue reading →Chinese original · English translation by Claude Opus 5.5
This problem is still pretty easy to do. First put all the prefix XOR sums into a trie, then in \(O(n)\) enumerate the right endpoint \(r\), look up in the trie the maximum XOR with the prefix XOR sum \([1-r]\) and put it into the heap (when putting it into the heap, mark its rank as \(1\)). After that, take items out of the heap and keep putting in the XOR query values with gradually increasing ranks; just collect the first \(k\).
Continue reading →Chinese original · English translation by Claude Opus 5.5
Given a permutation \(p[1-n]\) and a \([1,n]\)-valued array \(a[]\), answer \(q\) queries: does the subsegment \([l,r]\) of the array contain a cyclic shift of the permutation?
Consider scanning forward: for the number at each position, find the position of the “nearest previous cyclic-shift number” to its left, and at the same time update its own position for the next iterations to use:
Continue reading →Chinese original · English translation by Claude Opus 5.5