The recursive problem is easy to implement, but it is really troublesome to get the recursive formula, just like DP. Analysis (partial from hdu ppt): Set: F (n) indicates the legal queue of n people. Then: according to the gender analysis of the last person, he is either male or female, so we can discuss it in 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, there is a total of F (n-1); 2. If the last person in the valid queue of n is a female, the n-1 person in the queue must be a girl. That is to say, limits the last two must be both girls, which can be divided into two situations: (1), if the queue before The N-2 individual is legal queue, then apparently followed by two more girls, it must also 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 the illegal queue, the illegal place must be the tail, that is, the length here is the N-2 of the illegal string form must be "F (n-4) + male + female ", a total of F (n-4) in this case ). 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. [Cpp] # 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 (const int ); // convert a variable of the int type to the BigNum (const char *); // convert a variable of the string type to the BigNum (const BigNum &); // copy the constructor BigNum & operato R = (const BigNum &); // reload the value assignment operator. The value assignment operation between large numbers is friend istream & operator> (istream &, BigNum &); // reload the input operator friend ostream & operator <(ostream &, BigNum &); // reload the output operator BigNum operator + (const BigNum &) const; // reload the addition operator, bigNum operator-(const BigNum &) const; // overload subtraction operator, subtraction between two large numbers BigNum operator * (const BigNum &) const; // overload multiplication operator. BigNum operator/(const int &) const; // overload division operator. A large number is used to divide an integer. Calculate BigNum operator ^ (const int &) const; // nth power operation of large numbers int operator % (const int &) const; // perform the modulo operation bool operator> (const BigNum & T) const on an int type variable in a large number; // compare the size of a large number with that of another large number bool operator> (const int & t) const; // compare the size of a large number with that of an int Type Variable void print (); // output large number}; BigNum: BigNum (const int B) // convert an int type variable to a large number {int c, d = B; len = 0; memset (a, 0, sizeof (a); while (d> MAXN) {c = d-(d/(MAXN + 1) * (MAXN + 1 ); d = d/ (MAXN + 1); a [len ++] = c;} a [len ++] = d;} BigNum: BigNum (const char * s) // convert a variable of the string type to a large number {int t, k, index, l, I; memset (a, 0, sizeof ()); l = strlen (s); len = l/DLEN; if (l % DLEN) len ++; index = 0; for (I = L-1 I> = 0; i-= DLEN) {t = 0; k = I-DLEN + 1; if (k <0) k = 0; for (int j = k; j <= I; j ++) t = t * 10 + s [j]-'0'; a [index ++] = t ;}} BigNum: BigNum (const BigNum & T): len (T. len) // copy the constructor {int I; memset (a, 0, sizeof (a); for (I = 0; I <len; I ++) a [I] = T. a [I];} BigNum & BigNum: operator = (const BigNum & n) // overload the value assignment operator to 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;} istream & operator> (istream & in, BigNum & B) // reload the input operator {char ch [MAXSIZE * 4]; int I =-1; in> ch; int l = strlen (ch); int count = 0, sum = 0; for (I = l-1; I> = 0 ;) {sum = 0; int t = 1; for (int j = 0; j <4 & I> = 0; j ++, I --, t * = 10) {sum + = (ch [I]-'0') * t;} B. a [count] = sum; count ++;} B. len = count ++; return in;} ostream & operator <(ostream & out, BigNum & B) // reload the output operator {int I; cout <B. a [B. len-1]; for (I = B. len-2; I> = 0; I --) {cout. width (DLEN); cout. fill ('0'); cout <B. a [I];} return out;} 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; else t. len = big; return t;} BigNum: operator-(const BigNum & T) const // subtraction between two large numbers {int I, j, big; bool flag; bigNum t1, t2; if (* this> T) {t1 = * this; t2 = T; flag = 0;} else {t1 = T; t2 = * this; flag = 1 ;}big = t1.len; for (I = 0; I <big; I ++) {if (t1.a [I] <t2.a [I]) {j = I + 1; while (t1.a [j] = 0) j ++; t1.a [j --] --; while (j> I) t1.a [j --] + = MAXN; t1.a [I] + = MAXN + 1-t2.a [I];} else t1.a [I]-= t2.a [I];} t1.len = big; while (t1.a [len-1] = 0 & t1.len> 1) {t1.len --; big --;} if (flag) t1.a [big-1] = 0-t1.a [big-1]; return t1;} BigNum :: operator * (const BigNum & T) const // multiplication between two large numbers {BigNum ret; int I, j, up; int temp, temp1; for (I = 0; I <len; I ++) {up = 0; for (j = 0; j <T. len; j ++) {temp = a [I] * T. a [j] + ret. a [I + j] + up; if (temp> MAXN) {temp1 = te Mp-temp/(MAXN + 1) * (MAXN + 1); up = temp/(MAXN + 1); ret. a [I + j] = temp1;} else {up = 0; ret. a [I + j] = temp ;}} if (up! = 0) ret. a [I + j] = up;} ret. len = I + j; while (ret. a [ret. len-1] = 0 & ret. len> 1) ret. len --; return ret;} BigNum: operator/(const int & B) const // perform the division operation on an integer {BigNum ret; int I, down = 0; for (I = len-1; I> = 0; I --) {ret. a [I] = (a [I] + down * (MAXN + 1)/B; down = a [I] + down * (MAXN + 1)-ret. a [I] * B;} ret. len = len; while (ret. a [ret. len-1] = 0 & ret. len> 1) ret. len --; Return ret;} int BigNum: operator % (const int & B) const // a large number of Modulo operations on an int type variable {int I, d = 0; for (I = len-1; I> = 0; I --) {d = (d * (MAXN + 1) % B + a [I]) % B ;} return d;} BigNum: operator ^ (const int & n) const // Npower operation of large numbers {BigNum t, ret (1); int I; if (n <0) exit (-1); if (n = 0) return 1; if (n = 1) return * this; int m = n; while (m> 1) {t = * this; for (I = 1; I <1 <= m; I <= 1) {t = t * t ;} m-= I; ret = ret * t; If (m = 1) ret = ret * (* this);} return ret;} bool BigNum: operator> (const BigNum & T) const // compare the size of a large number with that of another large number {int ln; if (len> T. len) return true; else if (len = T. len) {ln = len-1; while (a [ln] = T. a [ln] & ln> = 0) ln --; if (ln> = 0 & a [ln]> T. a [ln]) return true; else return false;} bool BigNum: operator> (const int & t) const // compare the size of a large number and an int type variable {BigNum B (t); return * t His> B;} 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 ;}