This page briefly introduces construction problems as a problem type.
Introduction
Construction problems are a common type of problem in contests.
Formally, the answer to a problem often has some regularity, so that even when the problem size grows rapidly, there is still a chance to obtain the answer relatively easily.
This requires considering the impact of the growth of the problem size on the answer and whether this impact can be generalized. For example, when designing a dynamic programming method, one must consider what effect transitioning from one state to a successor state will have.
Characteristics
A very notable characteristic of construction problems is their high degree of freedom: there may be many ways to construct a solution, but one relatively simple construction will satisfy the requirements. This seems to relax the requirements and make the problem easier, but often this high degree of freedom is precisely what leaves the problem without a clear direction and makes it difficult to begin.
Another characteristic is their flexible form and diverse variations. There is no general solution or standard approach that can solve all construction problems, and it is even difficult to identify commonalities among their solution ideas.
Examples
The following examples illustrate some of the ideas behind construction problems and provide inspiration. It is recommended to think deeply before viewing the solutions; readers are also welcome to share interesting construction problems.
Example 1
Codeforces Round #384 (Div. 2) C. Vladik and fractions
Construct a set of such that, for a given , .
Solution idea
The construction method can be seen from the second sample.
Clearly, is a valid solution. In the special case , there is no solution, because and are equal.
As for how the construction idea arises, it is probably just observing the samples together with a little number sense. This problem is not difficult for people with strong mathematical intuition.
Example 2
Luogu P3599 Koishi Loves Construction
Task 1: Determine whether it is possible to construct, and construct, a permutation of of length whose prefix sums are pairwise distinct modulo .
Task 2: Determine whether it is possible to construct, and construct, a permutation of of length whose prefix products are pairwise distinct modulo .
Solution idea
For task 1:
When is odd, no valid solution can be constructed.
When is even, a sequence of the form can be constructed.
First, we can see that must appear in the first position of the sequence. Otherwise, the two prefix sums immediately before and after appears inevitably become equal modulo arithmetic.
Next, consider how to construct the entire sequence.
We can obtain the original sequence by constructing its prefix-sum sequence. The differences between every pair of prefix sums cannot be equal modulo arithmetic, because the difference sequence of the prefix-sum sequence corresponds to the original permutation.
Therefore, we try to construct the sequence so that its prefix-sum sequence modulo arithmetic has the form
It is not difficult to see that this perfectly satisfies all the constraints.
For task 2:
When is composite but not , no valid solution can be constructed.
When is prime or equal to , a sequence of the form can be constructed.
First consider when a solution exists.
Clearly, there is no solution when is composite. For a composite number, there are two smaller numbers such that , for example . Once have both appeared, all subsequent prefix products remain , so there is no solution for composite values. In the special case , we can see that there are no such , so a valid solution exists.
Now consider how to construct this sequence.
Following the same idea as in task 1, we find that must appear in the first position; otherwise, the two prefix products immediately before and after appears would be equal. Likewise, must appear in the last position, because all prefix products after the position of are . After analyzing the several samples in the statement, we find that every sample has a valid solution whose prefix products are . Therefore, we can construct the sequence described above to satisfy this condition. It remains only to prove that these numbers are pairwise distinct.
We find that all these numbers are the inverses of , so they are distinct, and the problem is solved.
Example 3
AtCoder Grand Contest 032 B
Given an integer , construct an undirected graph with vertices. Let the vertex labels be ; it must satisfy:
- It is a simple connected graph.
- There is an integer such that, for every vertex, the sum of the labels of its adjacent vertices is .
The input is guaranteed to have a solution.
Solution idea
By analyzing the cases , we can find a construction idea.
Construct a complete -partite graph whose part sums are equal. Then every vertex has the same , namely .
If is even, pair the numbers from the two ends, namely .
If is odd, place alone in one group and pair the remaining numbers, namely .
The connectivity of the graph constructed this way for is easy to prove, so we omit the details.
The problem is solved.
Example 4
BZOJ 4971 “Lydsy1708 Monthly Contest” Knapsack in Memory
After a hard day's work, Q fell asleep. He recalled studying 0/1 knapsack when he had just entered university. At that time, as a freshman, Q solved a simple 0/1 knapsack problem. The problem was:
Given items with volumes , calculate the number of ways to choose some items (possibly none) so that the total volume is exactly . Since the answer may be very large, output only the answer modulo .
Because he had stayed up late solving problems for a long time, he could see only and in the sample input and that the sample output was ; he could not see how many items there were or the volume of each item. Even after waking up, Q could not see and . Write a program to help Q recall the sample input from long ago.
Solution idea
This is one of the construction problems with the highest degree of freedom. This results in a situation where one has no idea how to begin.
First, it is not difficult to see that the modulus is a decoy. Since we can construct the data freely, we can certainly make the number of solutions no greater than the modulus.
In a somewhat unusual way, we realize that we can construct small items with cost and several large items with cost greater than .
Since at most one large item can be selected, the contribution of each large item with cost to the number of solutions is .
Let denote the minimum number of large items when there are items of cost and the number of solutions is .
Precompute with dp. Calculation shows that it is sufficient to precompute all values for .
The problem is solved.