Hdoj/hdu 1297 Children ' s Queue (derivation ~ large number)

Source: Internet
Author: User

Problem Description
There is many students in PHT School. One day, the headmaster whose name was Pigheader wanted all students stand in a line. He prescribed that girl can is in a single. In other words, either no girl in the queue or more than one girl stands side by side. The case n=4 (n was 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 the queue satisfied the headmaster ' s needs is 7. Can do a program to find the total number of the queue with n children?

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

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

Sample Input
1
2
3

Sample Output
1
2
4

Test instructions
Just n people, stand in a row.
There is a requirement that (F) girls cannot stand alone between boys.
There can be no girls.

The output of the number of station method;
(regardless of the difference between people, only the location and the difference between men and women)
(If a row ends in MF is not legal)

Analysis:
If n person's station law is db[n];
Deduced from the previous db[n].
Db[n-1] Add a m to the end, it is certain to be possible.
Db[n-2] At the end of the add FF, it is also sure to.
Add MF can not, add mm is also possible (but this case and db[n-1], add FM also and Db[n-1]+m repeat.

After the sequence can not be added FF (MF can not, plus FF), become legal,
So db[n-4] back +mfff can, in fact, add an F can also constitute a legal, but this situation is contained in db[n-2] (quite and +ff) inside;

So the recursive equation Db[n] =db[n-1] + db[n-2] + db[n-4];

Db[i] is the number of legal sequences saved.

Java large number of seconds a~~~

Import Java.math.biginteger;import Java.util.Scanner; Public classmain{StaticBigInteger db[] =Newbiginteger[1001]; Public Static void Main(string[] args)        {Dabiao (); Scanner sc =NewScanner (System.inch); while(Sc.hasnext ()) {intn =sc.nextint (); System. out. println (Db[n]); }    }Private Static void Dabiao() {db[0]=NewBigInteger ("1"); db[1]=NewBigInteger ("1"); db[2]=NewBigInteger ("2"); db[3]=NewBigInteger ("4"); db[4]=NewBigInteger ("7"); for(intI=5; i<db.length;i++) {db[i]=db[i-1].add (db[i-2]). Add (db[i-4]); }    }}

Hdoj/hdu 1297 Children ' s Queue (derivation ~ large number)

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.