hdu1297 - Children’s Queue (遞推求解 + 高精度)

Children’s Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10272    Accepted Submission(s): 3289


Problem Description
There are many 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
1 2 3
 

Sample Output
1 2 4
 

Author
SmallBeer (CML)
 

Source
 

Recommend
lcy
 
 
 
                    題意:男女站一排,不能有單個女生自己站的情況,
                            例如:n = 4  則站排情況如下:(F表示女,M表示男)FFFF, FFFM, MFFF, FFMM, MFFM, MMFF, MMMM               結果爲7
                                       n = 3  .......................FFM,   FFF,    MMM,    MFF              結果爲4
                                       n = 2.........................MM   ,FF                                             結果爲2
                                       n = 1.........................M                                                        結果爲1
 
                  題解:
                            F(n)表示n個人的合法隊列, 根據最後一個人是男是女分爲兩類:
                            1)最後一人是男人,則對前面n-1個人的隊列武任何限制,他站在最後,這種情況共有F(n -1)種
                            2)最後一人是女人,則倒數第二人必須是女生,男生情況則不合法,這種情況又分兩種情況:
                                    2.1)如果前F(n - 2)人合法,則加兩個女生也合法,所以這種情況共有F(n - 2)種;
                                    2.2)   如果前F(n - 2)人不合法,加上2個女生也可能成爲合法隊列,這種情況只可能爲
                                             F(n - 4) + 男 + 女 這種情況 ,所以這種情況共有F(n - 4)種
                          綜上所述,F(n) = F(n - 1) + F(n - 2) + F(n - 4) ,隨後就是n<=3情況的特殊除了
                          需要說明的是F(0) = 1根據題意也是合法的,特殊情況
                           
                            
 
 超時的遞歸代碼:現在做這種題如果不打表或是找規律,基本就是超時
 
/******************************
*
*	acm:   hdu-1297
*
*	title: Children’s Queue
*
*	time:  2014.5.18
*
******************************/

//考察遞推求解

#include <stdio.h>
#include <stdlib.h>

int fun(int n)
{
	int res;

	if (n == 0)  //特殊處理
	{
		res = 1;
	}
	else if (n == 1)
	{
		res = 1;
	}
	else if (n == 2)
	{
		res = 2;
	}
	else if (n == 3)
	{
		res = 4;
	}
	else
	{
		res = fun(n-1) + fun(n-2) + fun(n-4);
	}

	return res;
}

int main()
{
	int n;

    while (~scanf("%d", &n))
	{
		printf("%d\n", fun(n));
	}

    return 0;
}
 
 
//我用打表方法做,但是還是錯誤,原來是高精度問題,當值爲1000時,值大到 __int64 也會溢出
/******************************
*
*	acm:   hdu-1297
*
*	title: Children’s Queue
*
*	time:  2014.5.18
*
******************************/

//考察遞推求解
//本題打表求解


#include <stdio.h>
#include <stdlib.h>

#define ll __int64

ll Array[1000];

void fun(int n)
{
	int i;

	Array[0] = 1;
	Array[1] = 1;
	Array[2] = 2;
	Array[3] = 4;

	for (i = 4; i < n; i++)
	{
		Array[i] = Array[i-1] + Array[i-2] + Array[i-4];
	}
}

int main()
{
	int n;

	fun(1001);

    while (~scanf("%d", &n))
	{
		printf("%I64d\n", Array[n]);
	}

    return 0;
}
 
//AC代碼 ,遞推+ 高精度
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章