Question: Click to open the link
The recursive problem is easy to implement, but it is really troublesome to get the recursive formula, just like DP.
Analysis (some ppt files from HDU ):
Set: F (n) indicates the valid queue of n persons, then:
According to the gender analysis of the last person, he is either male or female, so he can be divided into two categories:
1. If the last person in the valid queue of N people is male, there is no restriction on the queue of n-1 people. He only needs to stand at the end, in this case, F (n-1) is used );
2. If the last person in the valid queue of n persons is a female, the n-1 person in the queue must be a girl. That is to say, the last two persons must be both girls, this can be divided into two situations:
(1), if the queue of the former N-2 individual is legal queue, then obviously followed by two girls, it must be legal, this situation has F (n-2 );
(2) the difficulty is that, even if the previous N-2 individual is not legal queue, plus two girls may also be legal, of course, this length for the N-2 of illegal queue, the invalid part must be the tail, that is to say, the length here is the form of the illegal string of N-2 must be "F (n-4) + male + female ", this case has a total of F (n-4 ).
So the recursive formula is F (n) = f (n-1) + f (n-2) + f (n-4 ).
In addition, it should be noted that the recursive data range is 1000, and Int or even long can no longer carry the maximum boundary. Therefore, I used the large number class posted two days ago.
# Include <iostream> # include <string> # include <iomanip> # include <algorithm> using namespace STD; # define maxn 9999 # define maxsize 10 # define dlen 4 class bignum {PRIVATE: int A [500]; // The number of digits that can be controlled for a large number int Len; // the length of a large number is public: bignum () {Len = 1; memset (A, 0, sizeof (a);} // constructor bignum & operator = (const bignum &); // reload the value assignment operator. bignum operator + (const bignum &) const; // reload the addition operator. Void print () is an addition operation between two large numbers (); // output large number}; Bignum & bignum: Operator = (const bignum & N) // overload the value assignment operator. assign values between large numbers {int I; Len = n. len; memset (A, 0, sizeof (a); for (I = 0; I <Len; I ++) A [I] = n. A [I]; return * This;} bignum: Operator + (const bignum & T) const // The addition operation between two large numbers {bignum T (* This ); int I, big; // The number of digits big = T. len> Len? T. len: Len; for (I = 0; I <big; I ++) {T. A [I] + = T. A [I]; If (T. A [I]> maxn) {T. A [I + 1] ++; T. A [I]-= maxn + 1 ;}} if (T. A [Big]! = 0) T. len = big + 1; elset. len = big; return t;} void bignum: Print () // output large number {int I; cout <A [Len-1]; for (I = len-2; I> = 0; I --) {cout. width (dlen); cout. fill ('0'); cout <A [I];} cout <Endl;} bignum que [1202]; // The above is not the main function .. Int main () {que [0] = 1; que [1] = 1; que [2] = 2; que [3] = 4; for (INT I = 4; I <= 1000; I ++) {que [I] = que [I-1] + que [I-2] + que [I-4];} int tar; while (CIN> TAR) {que [tar]. print ();} return 0 ;}