# Recurrence of Algorithms and Its Application (establishment of recursive relationships and Application in the competition of informatics)

Source: Internet
Author: User

Definition: a sequence of numbers H0, H1 ,..., HN ,... If an integer N0 exists, when nn0 is used, you can use an equal sign (greater than or less than) to associate HN with some of the preceding HN (0i <n, such a formula is called a recursive relationship.

There are three basic problems in the recurrence relationship: how to establish the recurrence relationship, the nature of the given recurrence relationship, and how to solve the recurrence relationship.

Five Basic recursive relationships:

I. Fibonacci Series

Among all recursive relationships, the Fibonacci series should be most familiar to everyone. In the most basic programming language logo language, there are many such questions. In complex basic, Pascal, and C languages, the question of the Fibonacci series is gradually withdrawn from the competition stage because the solution is relatively easy. However, this does not mean that the Fibonacci series have no research value. On the contrary, some of these questions can still inspire us.

The representative of the Fibonacci series is the "rabbit breeding problem" (also known as the "Fibonacci problem") proposed by the famous Italian mathematician Fibonacci in 1202 ").

The question was raised: There is a pair of female rabbits, assuming that two months will be able to breed a pair of rabbits. How many rabbits are there after N months?

Solution: There are rabbit FX pairs in total for X months, and the number of new rabbits in the month is NX. The number of rabbits left in the X-1 month is set to Ox. Then:

FX = NX + ox

And Ox = Fx-1,

Nx = ox-1 = Fx-2 (that is, all rabbits in the X-2 month have reproductive capacity to the X months)

Border FX = Fx-1 + Fx-2 boundary condition: f0 = 0, F1 = 1

The recurrence relationships above can be obtained in sequence.

F2 = F1 + f0 = 1, F3 = F2 + F1 = 2, F4 = F3 + F2 = 3, F5 = F4 + F3 = 5 ,.......

The fabonacci series often occur in the simple combination counting problem, for example, this method can be used to solve the problem of "bone card coverage" [1] and "Example 1" in the following section. In the optimization method [2], the use of the Fibonacci series has also been well reflected.

Ii. Hanoi Tower Problems

Question: The Hanoi Tower consists of N disks of different sizes and three wooden columns A, B, and C. At the beginning, the N disks are sequentially mounted on the column from large to small, as shown in 1.

The N disks on column A must be moved to column C according to the following rules:

(1) only one disc can be moved at a time;

(2) disks can only be stored on three columns;

(3) During the moving process, small disks cannot be pressed on the dashboard.

How many times do I need to move these N plates from column A to column C?

Solution: Set HN to the number of times the plate needs to be moved from column A to column C. Obviously, when n = 1, you only need to move the plate on column A directly to column C, so H1 = 1. When n = 2, first move the small plate on column A to Column B; then move the large plate from column A to column C; finally, move the small plate on column B to column C, three disks are recorded, so H2 = 3. Similarly, when there are N (N2) plates on column A, n-1-1 plates above are always first moved to Column B with the help of column C, and then the bottom plate of column A is moved to column C; move n-1 plates on column B to column C with the help of column A; move hn-1 + 1 + hn-1 total times.

Limit HN = 2hn-1 + 1 boundary condition: hn-1 = 1

Iii. Plane Segmentation

Question:

There are N closed curves on the plane, and any two closed curves happen to meet at two points, and any three closed curves do not meet at the same point, ask the number of areas in which these closed curves divide the plane.

Solution: Set an to N closed curves to divide the plane into the number of regions. From Figure 2 can be seen: a2-a1 = 2; a3-a2 = 4; a4-a3 = 6. We can see from these formulas that an-1 = 2 (n-1 ). Of course, the formula above is just the conclusion we have drawn after observing the four images. Its correctness is not guaranteed yet. Let's try to prove it. When there are n-1 Curves on the plane that divide the plane into an-1 region, the n-1 curve will add an area for every intersection with the curve, because there are n-1 closed curves on the plane, and the n-th curve exactly exists at two points with each existing closed curve, and it is not at the same point with any two curves, therefore, a total of 2 (n-1) areas are added on the plane, and an-1 + 2 (n-1) areas are added. Therefore, the recursive relationship of this question is an = An-1 + 2 (n-1), and the boundary condition is a1 = 1.

The question of plane segmentation is a type of problem that is frequently encountered in competitions. Due to its flexibility and variety, it is often difficult for contestants. The question of "Example 2" Below is another question of plane segmentation, if you are interested, try to find the recursive relationship first.

Iv. Catalan count

The Catalan number is first obtained by Euler when accurately calculating the number of different diagonal triangles with convex n edges. It often appears in the combination count problem.

Proposal: In a convex n-edge shape, the N-edge is split into several triangles by the diagonal lines that do not interwork in the n-edge shape, the HN table is used for different split numbers. hn is the catalan number. For example, Pentagon has the following five sharding schemes (Figure 6-4), so h5 = 5. Evaluate the HN corresponding to an arbitrary convex n edge.

Solution: Set CN to the total number of N-edge splitting schemes. According to the requirements in the questions, we can see that any side of a convex n-edge must be an edge of a triangle, and the side P1 PN is no exception, then, we can determine a triangle based on the three points that are not in the same line ,......, In the Pn-1, find a point PK (1 <k <n), and P1, PN constitute three vertices of a triangle, the N-edge is divided into three non-Intersecting parts (as shown in 3), which are called area ①, area ②, and area ③ respectively. Area ③ must be a triangle, area ① is a convex K edge shape, area ② is a convex n-k + 1 edge shape, and area ①'s total number of split schemes is CK, the number of split schemes in Area ② is CN-k + 1, so the number of N-edge split schemes containing △p1pkpn is ckcn-k + 1, while the PK can be P2, P3, ......, According to the principle of addition, the total number of triangular split schemes with convex n edges is equal to that with the convenience of calculation, and the agreed boundary condition C2 = 1.

The Catalan number is a complex recursive relationship, especially in competitions, it is difficult for contestants to establish a correct recursive relationship in a short period of time. Of course, you can also use the search method to solve the catalan number problem. However, compared with the recursive relationship method, the search method is not only inefficient, but also increases programming complexity.

V. second-class Stirling Number

Among the five typical recursive relationships, the second type of Stirling is least familiar to everyone. Because of this, it is necessary to explain what the second type of strling number is.

[Definition 2] place n balls in M of the same box without empty boxes. The number of different solutions is represented by S (n, m, it is called the second type of Stirling number.

Let's deduce the recursive relationship with two parameters according to Definition 3-the second type of Stirling number.

Solution: There are n Different balls with b1, b2 ,...... BN representation. Obtain a ball bn from it. There are two kinds of BN placement methods:

BN occupies one box, and the remaining balls can only be placed M-1-1 box. The number of solutions is S2 (N-1 m-1 );

Bn shares a box with other balls. Therefore, B1, B2 ,...... The n-1 balls of the bn-1 are placed in M boxes, and then the ball BN can be placed in one of the boxes. The number of balls is MS2 (n-1, m ).

Based on the above two cases, the second type of Stirling Number Theorem can be obtained:

[Theorem] S2 (n, m) = MS2 (n-1, m) + S2 (N-1 m-1) (n> 1, M1)

The boundary condition can be pushed and exported by definition 2:

S2 (n, 0) = 0; S2 (n, 1) = 1; S2 (n, n) = 1; S2 (n, k) = 0 (k> N ).

The second type of Stirling is rarely used in the competition, but there are also some topics in the competition that are similar or even more complex. Readers may wish to try to establish a recursive relationship on their own.

"Example 1" (the first question of the 1998 Bengbu city competition retries) a trained bee can only crawl to the adjacent beehive on the right, not reverse crawling. Try to find the number of possible routes for the bee to climb from the beehive A to the beehive B.

Solution: this is a typical issue of the Fibonacci series. The recurrence relationship is obvious. Because the "bees can only climb to the right side of the adjacent beehive, not reverse crawling" restriction, decided that the bee to B point path can only be from the B-1 point or B-2 point to arrive, therefore fn = fn-1 + fn-2 (a + 2nb), boundary condition fa = 1, FA + 1 = 1. (With program pro_1.pas)

(Example 2) n (N500) lines in the same plane are known to have P (P2) lines at the same point, then, how many different regions can the n straight lines divide the plane?

Solution: This question is very similar to the plane division problem in the first part. The difference lies in the straight line and whether there is a common line. Due to the particularity of the common point straight line, we decided to first consider the P line at one point, and then consider the remaining N-P lines. First, we can directly find the line where p is located at a point and divide the plane into 2 P regions. Then, we can add a straight line based on K (KP) lines on the plane, A maximum of K straight lines can be intersecting, and an area will be added at each intersection. After the intersection with the last line, an area will be added because the line can be infinitely extended. So fm = fm-1 + M (M> P), the boundary condition has been calculated before, is fp = 2 p. Although this question seems to have two parameters, we can find that this question is a recursive relation with a parameter. (With program pro_2.pas)

The recursive relationships in the two examples above are obvious. I believe no one will question the questions about them.

Next let's look at another question.

"Example 3" (question 2 of the 1999 Jiangsu Provincial team competition question) in an n × m square, M is an odd number, with n × m numbers, 7:

There is a person in the middle of the square. This person can move forward in five directions, but cannot move out of the square. 8:

Each person passing through a square must take the number in this square. It is required to find a path from the bottom to the top so that the sum of the numbers is the maximum. The maximum value of the sum of outputs.

Solution: This question is essentially similar to the first question. It calculates all possible points that can reach a point from a point that can arrive, and then explores their relationships. We use coordinates (x, y) to uniquely identify a point. (m, n) indicates the upper right corner of the graph. The starting point of a person is that it is restricted by the forward direction of the person, points that can reach point (x, y) directly only (x + 2, Y-1), (x + 1, Y-1), (X, Y-1), (x-1, Y-1 ), x-2, y-1 ). The path to point (X, Y) and the largest path must go from (x + 2, Y-1), (x + 1, Y-1), (X, Y-1 ), (x-1, Y-1), (X-2, Y-1) Several paths are generated, since the optimal scheme is required, of course to pick one and the maximum path, the relationship is as follows: FX, y = max {FX + 2, Y-1, FX + 1, Y-1, FX, Y-1, Fx-1, Y-1, Fx-2} + numx, Y, where numx, Y indicates the number at (x, y. The boundary condition is :,. (With program pro_3.pas)

When I see the above question, some people will say that this is not an application of Recursive Relations, it is a dynamic planning; the above relationship is not a recursive relationship, but a dynamic transfer equation. Why? Because there is a max operation in the relational expression, it is a dynamic plan. Yes? In the definition of a recursive relationship, only "associate h (n) with some of the preceding items h (I) (0i <n) with an equal sign (or greater than or less than sign)" is required ", the Association has a wide range of meanings, including the use of the maximum (small) value operator. Therefore, we believe that the above question can still be a recursive relationship. Of course, it also belongs to dynamic planning. So what is the relationship between recursive relationships and dynamic planning? Let's draw a Venn diagram (see figure 9 ). In the mathematical formula, a = {recursive relationship} B = {dynamic planning}, ba. Generally, people understand the recursive relationship as a general recursive relationship, so dynamic planning and recursive relationship are two independent individuals. Next, let's analyze the similarities and differences between General recursive relationships and dynamic planning through the list (see table 1 ).

Although there is a difference between the General recursive relationship and the dynamic planning, in practice, people often confuse them, and always mistake the General recursive relationship as a dynamic planning. This is because the dynamic planning that has been discovered since the 50 or 60 s in the last century has set off a great wave in the competition in informatics. Until today, it is still the major competition in informatics. However, the history of recursive relationships has a long history. Although recursive relationships are not as popular as dynamic planning today, their impact on people cannot be ignored. In the IOI '99 trial, there was a "01 Statistics" [4]. Many people just use the formula to calculate the number of combinations, and then sum the number of combinations, to calculate the number of class A numbers, the program efficiency is not high. In fact, this is a very typical recursive relation question [5]. Due to the fact that contestants usually ignore the General recursive relationship, this leads to a bid loss.

Another type of game problem also utilizes recursive relationships. Let's take a look at this example.

"Example 4" (1995 "student computer world") writes a computer program that allows computers and people to play card games, win the computer, and display the entire game process. The game is: two players (one on the computer and the other on the computer): There are n cards (3 <n <10000), and the two players exchange cards (the computer first takes ), whoever takes the last one is who wins. The rule is as follows: the first contestant takes one to n-1 cards (you can take any card, but you cannot finish it). In the future, you can take one or more cards, however, you must not remove more than twice the number of items just retrieved by the other party (if the other party just removes B sheets, you can take 1 to 2B sheets ).

Solution: this question seems to be a very obvious dynamic planning question. It is divided into stages by the number of remaining cards. The State FP and K indicate the remaining P cards, in addition, if the first person can obtain a maximum of K cards, it is a bid or a bid.

The state transition equation is: FP, k = (PK) or FP, k-1 or not FP-K, 2 k (1pn, 1 k <n)

Then, we can design a card acquisition scheme based on the situation of the obtained status to make the computer win. Of course, if the initial number of cards is a point of failure, the computer can only admit defeat.

This method is more efficient when the number of cards is not too large, but once the number of cards is large (assuming n reaches the limit), the required space is O (105105 ), this method will inevitably lead to insufficient space.

We may wish to draw a table with a small number of cards (see table 2) to see if there are any rules.

Table 2 shows that if the remaining number of cards is 2, 3, 5, or 8, no matter how many cards a player gets first (assuming that he cannot finish all the cards at a time), he will inevitably lose (unless another player does not want to win ); numbers such as numbers 2, 3, 5, and 8 are called "Number of BITs ". When the initial number of cards is not a mandatory number, we need to design a scheme to make the number of cards more than 2x after each computer gets X cards, and is the number of BITs that must be defeated. So what exactly are the numbers that cannot be defeated? From the numbers 2, 3, 5, and 8 in the table, we guess that the numbers starting from 2 in the Fibonacci series are all numbers that must be defeated, and they are proved by mathematics [6]. Then we can design the plan based on the number of bid card numbers. If you want the computer to take X cards at a time, the remaining number of cards is a must-have number, and more than 2x cards, it seems that you cannot do it. For example, the initial number of cards is 20. If you take 7 cards at a time and make the remaining 13 cards, the opponent can take all 13 cards at a time. So we only have to design a solution for 7 more cards to ensure that the computer can get 7th cards, and the number of cards that the computer gets at the last time is less than 13/2, to achieve this step, you only need to nest and use the Fibonacci series (with program pro_4.pas ).

Conclusion: from the above examples, it can be seen that using recursive relationships to solve problems is sometimes easier than dynamic planning. When dynamic planning is difficult to implement, generally, recursive relationships may play an important role, which is usually manifested in two aspects: solving directly or simplifying the dynamic planning process.

Summary

Based on the above description of the establishment of recursive relationships and the application in the competition of informatics, we can see that recursive relationships are not an abstract concept and are specific, therefore, we cannot find a way to establish all recursive relationships. We can only analyze the issues based on the specific conditions. Although there is no fixed pattern to follow for the establishment of recursive relationships, in general, we must first find the important conditions in the question and analyze the relationship between a certain item and several items in the previous section, then, find the boundary condition. Recursive relationships are widely used in competitions. They include dynamic planning for almost every competition. Therefore, learning the establishment of recursive relationships can improve our mathematical quality, it will also be of great benefit to future competitions. Because dynamic planning is more familiar to everyone, This article focuses on the general recursive relationship in the recursive relationship, hoping to give some inspiration to contestants.

[Appendix]

Bone card coverage: the 2 x n Board uses a 1X2 bone card for full coverage.

Optimization Method: Let the Function Y = FX have a single peak on the interval (a, B), which is assumed to be a very big point. The so-called single-peak extreme, that is, there is only one pole, and when X and Z deviate from the larger, the deviation | f (x)-f (e) | the larger. It is required to use several tests to find a certain degree of accuracy. The optimal method is the 0.618 optimization method and the Fibonacci optimization method.

Proof of the combination formula:

Bytes

Renewal certificate is complete.

01 series: Recently, IOI experts are conducting a research on binary numbers, and a statistical problem designed by the research makes them a big headache. The problem is this: for a natural number N, you can convert it into the corresponding binary number, where: N = ak2k + ak-12k-1 + ...... A121 + a0; and AI = 0 or 1 (0i <K), ak = 1. For example, 10 = 1010; 5 = 101. Let's count a0 ~ AK, the number of 0 in k + 1 and the number of 1. If the number of K + 1 is greater than the number of 1, it is called the number of Class. The current task is to evaluate 1 ~ for a given m ~ The number of Class A in M.

For solutions to 01 statistics, see Xu Jing's solution report.

Proof: use mathematical induction to prove:

From table 2, we can know that the number of Fibonacci 2, 3, and 5 is the number of bid cards, and the number of other cards between (1, 5) is the number of bid cards.

Assume that the Fibonacci series are F1, F2 ...... Fi and Fi + 1 are in the range of (F1, FI + 1]. Only the number of Fiber-ACCI cards is the number of bid cards, and the number of other cards is the number of bid cards. Then we can prove that the number of cards in the (F1, FI + 2] interval also satisfies the above nature. The proof is as follows:

Set the remaining fi + 2 cards. This time, the computer takes X cards, and the remaining number is P = Fi + 2-X cards.

If xfi is used, PFI + 1,

∵ Fi = Fi + Fi-1 and Fi-1Fi

Wi-Fi + 12fi

∴ P2x

The publisher can finish all the remaining P cards at a time.

The login computer will definitely lose

If 1x <Fi, P> fi + 1,

∵ Fi + 2-fi = Fi + 1 and Fi + 1 is the number of ononacci

The ghost computer cannot use a card acquisition scheme, so that after a computer gets less than FI + 1/2 cards at a time, there will be more than one card.

When the remaining fi + 1 card is left, it is the computer's turn to take the cards, and the computer cannot finish all the cards at a time.

∵ Fi + 1 is the number of ononacci

The login computer will definitely lose

Again P (FI + 1, FI + 2), that is, when the other P cards are used by people, the people will surely win,

Beats P is a must-win number

Conclusion can be found from 1 and 2. Pass.

Related Keywords:

### Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.