(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

Source: Internet
Author: User

(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 Input
132
 
Sample Output
132
 
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 ;}






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.