(Hdu step 3.1.6) statistical problem (number of solutions that continuously walk left and right and go up n steps), hdu3.1.6

Source: Internet
Author: User

(Hdu step 3.1.6) statistical problem (number of solutions that continuously walk left and right and go up n steps), hdu3.1.6

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:

Statistical problems
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 732 Accepted Submission (s): 466
 
Problem Description in an infinitely large two-dimensional plane, we make the following assumptions:
1. Only one cell can be moved at a time;
2. You cannot go backward (If your destination is "up", you can go left, right, or up, but not down );
3. The passing grid collapsed immediately and could not go for the second time;

Calculate the number of different solutions in n steps (two steps are considered different if one step is different ).
 
Input first gives a positive integer C, indicating that there are group C Test Data
In row C, each row contains an integer n (n <= 20), which indicates that n steps are required.
 
For the Output, Please program the total number of different solutions that take n steps;
The output of each group occupies one row.
 
Sample Input
212
 
Sample Output
37
 
Authoryifenfei
Source Shaoxing top school of Information Technology-Program Design Competition of the second Computer Culture Festival
Recommendyifenfei


Question Analysis:

Number of n-grams f (n) = 2 * f (n-1) + f (n-2 ). How did this happen. Use a (n) to represent the number of solutions in step n and B (n) to represent

Number of solutions taken around step n. So a (n) = a (n-1) + B (n-1) (here, Why can't B (n-1) be multiplied by 2? Because the left-to-right direction cannot be used at the same time ). B (n) = 2 * a (n-1) + B (n-1) (a (n-1) * 2 is because, in step n, you can choose to go left or right. These two solutions are also available. For B (n-1), because if you go in the direction of step n-1, you can only go in the direction of step n)


The Code is as follows:

/** F. cpp ** Created on: February 5, 2015 * Author: Administrator */# include <iostream> # include <cstdio> using namespace std; const int maxn = 22; long dp [maxn]; void prepare () {dp [1] = 3; dp [2] = 7; int I; for (I = 3; I <maxn; ++ I) {dp [I] = 2 * dp [I-1] + dp [I-2] ;}} int main () {prepare (); int t; scanf ("% d ", & t); while (t --) {int n; scanf ("% d", & n); 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.