(Hdu step 3.1.2) the bone board is paved with squares (simple recursion: calculate the number of solutions to overlay 2 * n grids with a 2*1 bone board), hdu3.1.2
Make an advertisement for yourself before writing a question ~.. Sorry, I hope you can support my CSDN video courses at the following address:
Http://edu.csdn.net/course/detail/209
Question:
Bone plate shop Square |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission (s): 744 Accepted Submission (s): 478 |
|
Problem Description in a rectangle square of 2 × n, fill the square with a 1 × 2 bone card, input n, and output the total number of layout solutions. For example, if n = 3, it is a 2 × 3 square. There are three methods for laying the bone card, for example: |
The Input data is composed of multiple rows. Each row contains an integer n, indicating that the rectangular square of the test instance is 2 x n (0 <n <= 50 ). |
Output For each test instance, output the total number of placement schemes, and each instance outputs one row. |
Sample Input132 |
Sample Output132 |
Authorlcy |
Source recurrence solving topic exercise (For Beginner) |
Recommendlcy |
Question Analysis:
Simple recursion. Assume that dp [I] is the number of solutions that are filled with 2 * n grids. Then dp [I] = dp [I-1] + dp [I-2]. The dp [I-1] is the number of solutions that are full of 2 * (n-1) grids (since the first 2 * (n-1) mesh is full, then the last one can only be placed vertically ). Dp [I-2] Number of Solutions for a full 2 * (n-2) mesh (if the front 2 * (n-2) mesh is already full, then the last one can only be placed horizontally, otherwise it will be repeated ). in fact, after independently thinking about the recursive formula, you can actually include the input sample to verify it. note that dp [50] has reached more than 20 billion. In this case, long is required.
The Code is as follows:
/** B. cpp ** Created on: February 5, 2015 * Author: Administrator */# include <iostream> # include <cstdio> using namespace std; const int maxn = 52; long dp [maxn]; void prepare () {dp [1] = 1; dp [2] = 2; int I; for (I = 3; I <maxn; ++ I) {dp [I] = dp [I-1] + dp [I-2] ;}} int main () {prepare (); int n; while (scanf ("% d ", & n )! = EOF) {printf ("% lld \ n", dp [n]);} return 0 ;}