Rqnoj105 Nuclear Power Plant Problems

Source: Internet
Author: User

Back to my lovely blog ~ I was a little lazy yesterday, and I didn't write any article ~ Make up your resources today.

Next, let's start with the question.

 

 

[Title Description] (rqnoj105) a nuclear power plant has n pitfall containing nuclear substances, which are arranged in a straight line. If a nuclear material is put into M consecutive pits, it will lead to an explosion. Therefore, nuclear material may not be put in some pits. Task: for the given n and M, calculate the total number of solutions for storing nuclear substances without explosion [input format] The input file is only one row, two positive integers n, M (1 <n <50, 2 ≤ m ≤ 5) [output format] the output file has only one positive integer s, indicating the total number of solutions. [Sample input] 4 3 [sample output] 13

 

 

This is a linear recursive dynamic planning. To tell the truth, I did not understand the problem that I was directly reading... Paste the code first:

 

 

  1 program nuclear;
2 var
3 f:array[-6..60]of int64;
4 i,m,n:integer;
5 begin
6 readln(n,m);
7 f[0]:=1;
8 f[-1]:=1;
9 for i:=1 to n do
10 f[i]:=2*f[i-1]-f[i-m-1];
11 writeln(f[n]);
12 end.
 

 

 

This huge short code is really not easy to understand. First, the F array stores the number of possible failures to the F-th pitfall. Many questions on the Internet are discussed in different situations. According to the invincible idea of Jingo, you only need to set the M-bit forward of the array, that is,-m to n.

Another Tangle is the magical dynamic equation. You can analyze it by yourself. If you do not understand it, refer to the following.

Let's first break down the equation, that is, F [I]: = f [I-1] + F [I-1]-f [i-m-1], which is obvious. The two F [I-1] represent the f [I] bit on or not on. However, the condition that f [I] can be placed is that a row can contain a maximum of 1-1 heaps. For example, if I = 4, the status is as follows:

 

 

 

 

 

Because if you put it in (4), (2) it must not be put in (3. So we need to exclude (2) and (3) the situation in F [I-1] (the one put in Pit 3.

From the question, we can see that if (2) (3) both have nuclear substances, (1) certainly does not. So the number of conditions that can reach the graph is only equal to f [0], that is, the value of F [i-m-1. At this time f [I-1]-f [i-m-1] is good to understand.

Another question is why both F [0] and f [-1] are initialized to 1. You can find the answer simply from the above. Because when I = m (i-m-1 =-1) or I = m + 1 (i-m-1 = 0), I, I-1 ,..., I-m + 1 is not consistent with the meaning of the question.

Now please scroll up the scroll wheel and read the code again. This is the charm of this great O (n) DP code.

 

(Saltless original, reprinted please indicate the source)

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.