Construction

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 x,y,zx,y,z such that, for a given nn, 1x+1y+1z=2n\dfrac{1}{x}+\dfrac{1}{y}+\dfrac{1}{z}=\dfrac{2}{n}.

Solution idea

The construction method can be seen from the second sample.

Clearly, n,n+1,n(n+1)n,n+1,n(n+1) is a valid solution. In the special case n=1n=1, there is no solution, because n+1n+1 and n(n+1)n(n+1) 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 1…n1\dots n of length nn whose nn prefix sums are pairwise distinct modulo nn.

Task 2: Determine whether it is possible to construct, and construct, a permutation of 1…n1\dots n of length nn whose nn prefix products are pairwise distinct modulo nn.

Solution idea

For task 1:

When nn is odd, no valid solution can be constructed.

When nn is even, a sequence of the form n,1,n−2,3,⋯n,1,n-2,3,\cdots can be constructed.

First, we can see that nn must appear in the first position of the sequence. Otherwise, the two prefix sums immediately before and after nn 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

0,1,−1,2,−2,⋯0,1,-1,2,-2,\cdots

It is not difficult to see that this perfectly satisfies all the constraints.

For task 2:

When nn is composite but not 44, no valid solution can be constructed.

When nn is prime or equal to 44, a sequence of the form 1,21,32,⋯ ,n−1n−2,n1,\dfrac{2}{1},\dfrac{3}{2},\cdots,\dfrac{n-1}{n-2},n can be constructed.

First consider when a solution exists.

Clearly, there is no solution when nn is composite. For a composite number, there are two smaller numbers p,qp,q such that p×q≡0(modn)p\times q \equiv 0 \pmod n, for example (3×6)%9=0(3\times6)\%9=0. Once p,qp,q have both appeared, all subsequent prefix products remain 00, so there is no solution for composite values. In the special case 4=2×24=2\times 2, we can see that there are no such p,qp,q, so a valid solution exists.

Now consider how to construct this sequence.

Following the same idea as in task 1, we find that 11 must appear in the first position; otherwise, the two prefix products immediately before and after 11 appears would be equal. Likewise, nn must appear in the last position, because all prefix products after the position of nn are 00. After analyzing the several samples in the statement, we find that every sample has a valid solution whose prefix products are 1,2,3,⋯ ,n1,2,3,\cdots,n. Therefore, we can construct the sequence described above to satisfy this condition. It remains only to prove that these nn numbers are pairwise distinct.

We find that all these numbers are the inverses of 1⋯n−21 \cdots n-2 +1+1, so they are distinct, and the problem is solved.

Example 3

AtCoder Grand Contest 032 B

Given an integer NN, construct an undirected graph with NN vertices. Let the vertex labels be 1…N1\ldots N; it must satisfy:

  • It is a simple connected graph.
  • There is an integer SS such that, for every vertex, the sum of the labels of its adjacent vertices is SS.

The input is guaranteed to have a solution.

Solution idea

By analyzing the cases n=3,4,5n=3,4,5, we can find a construction idea.

Construct a complete kk-partite graph whose kk part sums are equal. Then every vertex has the same SS, namely (k−1)∑i=1nik\dfrac{(k-1)\sum_{i=1}^{n}i}{k}.

If nn is even, pair the numbers from the two ends, namely {1,n},{2,n−1}⋯\{1,n\},\{2,n-1\}\cdots.

If nn is odd, place nn alone in one group and pair the remaining n−1n-1 numbers, namely {n},{1,n−1},{2,n−2}⋯\{n\},\{1,n-1\},\{2,n-2\}\cdots.

The connectivity of the graph constructed this way for n≥3n\ge 3 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 nn items with volumes v1,v2,…,vnv_1,v_2,…,v_n, calculate the number of ways to choose some items (possibly none) so that the total volume is exactly ww. Since the answer may be very large, output only the answer modulo PP.

Because he had stayed up late solving problems for a long time, he could see only ww and PP in the sample input and that the sample output was kk; he could not see how many items there were or the volume of each item. Even after waking up, Q could not see nn and vv. 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 nn small items with cost 11 and several large items with cost greater than w2\dfrac{w}{2}.

Since at most one large item can be selected, the contribution of each large item with cost xx to the number of solutions is (nw−x)\dbinom{n}{w-x}.

Let fi,jf_{i,j} denote the minimum number of large items when there are ii items of cost 11 and the number of solutions is jj.

Precompute ff with dp. Calculation shows that it is sufficient to precompute all values for i≤20i\le 20.

The problem is solved.

This content comes from OI Wiki: Construction. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.