HDU children's queue

Source: Internet
Author: User
Children's queue

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)Total submission (s): 160 accepted submission (s): 102 

Problem description There are too students in PHT School. one day, the headmaster whose name is pigheader wanted all students stand in a line. he prescribed that girl can not be in single. in other words, either no girl in the queue or more than one girl stands side by side. the case n = 4 (n is the number of children) is like
FFFF, fffm, mfff, ffmm, mffm, mmff, Mmmm
Here F stands for a girl and M stands for a boy. the total number of queue satisfied the headmaster's needs is 7. can you make a program to find the total number of queue with N children?  

 

Input There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer N means the number of children (1 <=n <= 1000)  

 

Output For each test case, there is only one integer means the number of queue satisfied the headmaster's needs.  

 

Sample Input
 
123
  Sample output
 
124
  Note: 1. Recursive Formula 1) m

2) MFF

3) mfff

A: After the security sequence is followed by FF or m, the result is still safe.

B: Adding FF after the unsafe sequence can ensure its security. Although MF and F can also obtain the safe sequence, it is repeated in case.

Therefore: formula A [n] = A [n-1] + A [N-2] + A [n-4];

2. Large numbers and high precision. You can use a two-dimensional array to simulate large numbers. Output result when n = 1000:

12748494904808148294446671041721884239818005733501580815621713101333980596197474

74433619974245291299822523591089179822154130383839594330018972951428262366519975

47955743099808702532134666561848656816661065088789701201682837073071502397487823

19037

 # Include <stdio. h>  Int A [ 1001  ] [  101  ] = {  0  };  Void Add (  Int N ){  Int K =  0  , J;  For  ( J =  1  ; J <  101  ; J ++ ){ K + = A [ N - 1  ] [ J ] + A [ N -  2  ] [ J ] + A [ N -  4  ] [ J ]; A[ N ] [ J ] = K %  10000  ; K = K /  10000  ;  // Printf ("% d", k );  }  While ( K ){ A [ N ] [ J ++] = K %  10000  ; K = K /  10000  ;}} Int  Main  (){ A [  1  ] [  1  ] =  1  ; A [  2  ] [  1 ] =  2  ; A [  3  ] [  1  ] =  4  ; A [  4  ] [  1  ] = 7  ;  Int N , I ;  For  ( I =  5  ; I <  1001  ; I++ ){ Add ( I );}  While  ( Scanf (  "% D"  ,& N )! = EOF ){  For  ( I=  100  ; I >  0  ; I --){  If  ( A [ N ] [ I ]! =  0 )  Break  ;} Printf (  "% D"  , A [ N ] [ I ]);  For  ( I = I-  1  ; I >  0  ; I --){ Printf (  "% 04d"  , A [ N ] [ I ]);} Printf (  "\ 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.