HDU1297 Girl can't Walk Alone (DP)

Source: Internet
Author: User

Children ' s Queue

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 13333 Accepted Submission (s): 4364


Problem Descriptionthere 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?

Inputthere 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)

Outputfor Each test case, there are only one integer means the number of the queue satisfied the headmaster ' s needs.

Sample Input123

Sample Output124

Authorsmallbeer (CML)

SOURCE Hangzhou Electric ACM Training Team Training Tournament (VIII)

Recommendlcy | We have carefully selected several similar problems for you:2044 2045 2050 2046 1290

A[I-1]: Legal + male
A[I-2}: Legal + female
A[I-4}: Legal + unisex (i.e. not legal) + female


Here are three different situations that don't cross each other!


Explain:
Set: F (n) represents the legal queue for n individuals:

According to the last person's gender analysis, he is either male or female,
So there are two main types of discussions:

1. If the last person in the legal queue of n person is a male,
The front n-1 personal queue as long as it is a legitimate queue,
The last guy just needs to stand at the end, so this is a total of f (n-1);

2. If the last person in the legal queue of n person is a female,
The n-1 person in the queue must also be a schoolgirl, which means
Limit the last two people must be girls, this can be divided into two kinds of situations:

2.1. If the front n-2 person of the queue is a legitimate queue,
It is obvious that two more girls will be added to the rear, and it must be legal, this situation has f (n-2);
2.2, however, even if the front n-2 individual is not a legal queue, plus two girls may also be legal,
Of course, this is the n-2 of the length of the illegal queue, the illegal place must be the tail,
That is, the length of the n-2 is the illegal string of the form must be f (n-4) + male + female,
There is a total of f (n-4) in this case.
Source

HDU1297

Ideas are as follows:

A queue of length n can be seen as an additional 1 children in a n-1 queue, the child may only be:

A. Boys, the addition of 1 boys to any legal queue of n-1 is necessarily legal and the number of cases is f[n-1];

B. Girls, the addition of 1 girls in front of the queue after the end of the n-1 to the girl is also legal, we can convert the 2-bit girls into the n-2 queue;

One case is the addition of 2 girls in the legal queue of n-2, the number of cases is f[n-2];

But we notice the difficulty of the subject, perhaps the former n-2 bit with the girl as the end of the illegal queue (that is, the mere 1 girls end),
It is also possible to append 2 girls to the legal queue, while this n-2 illegal queue is bound by the structure of n-4 legal queue + 1 boys +1 girls,
That is, the number of cases is f[n-4].


If you feel the subject is more difficult, you can first review the article: [Acm_hdu_2045]lele's RPG puzzle, the idea is similar to the subject, but relatively simple.
A recursive formula is obtained as follows:
A[I]=A[I-1]+A[I-2]+A[I-4];

#include <iostream>#include<stdlib.h>#include<stdio.h>using namespacestd;Long Longa[10005][ the]={0};intMain () {intI,j,n; a[1][0]=1; a[2][0]=2; a[3][0]=4; a[4][0]=7; a[5][0]= A;  for(i=6; i<= +; i++)    {         for(j=0; j<= the; j + +) {A[i][j]+=a[i-1][j]+a[i-2][j]+a[i-4][j]; A[i][j+1]+=a[i][j]/100000000; A[I][J]%=100000000; }    }     while(cin>>N) { for(j= the; j>=0; j--)        if(a[n][j]!=0)         Break; cout<<A[n][j];  for(j=j-1; j>=0; j--) printf ("%08d", A[n][j]);//It's not right to cout a number of times.cout<<Endl; }    return 0;}

HDU1297 Girl can't Walk Alone (DP)

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.