Children’s Queue(递推)

Children’s Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 12101    Accepted Submission(s): 3953

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

题意:有n个位置,男孩女孩排队,要求女孩至少要2个在一起。

思路:设f[n]表示,n个人的情况。情况一、在f[n-1]的情况后面加一个男孩;情况二、在f[n-2]的情况后面加两个女孩;情况三、在f[n-3]最后是男孩(等价于在f[n-4]个个数)的后面加三个女孩;

所以:f[n]=f[n-1]+f[n-2]+f[n-4];由于数据比较大,所以采用大数加法就可以了。

转载请注明出处:寻找&星空の孩子  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1297

#include<stdio.h>
#include<string.h>
int f[1005][105];
void init()
{
    memset(f,0,sizeof(f));
    f[0][1]=1;
    f[1][1]=1;
    f[2][1]=2;
    f[3][1]=4;

    for(int i=4;i<=1000;i++)
    {
        int add=0;
        for(int j=1;j<=100;j++)
        {
            f[i][j]=f[i-1][j]+f[i-2][j]+f[i-4][j]+add;
            add=f[i][j]/10000;
            f[i][j]%=10000;
            if(add==0&&f[i][j]==0)break;
        }
    }
}
int main()
{
    int n;
    init();
    while(scanf("%d",&n)!=EOF)
    {
        int k=100;
        while(!f[n][k])k--;
        printf("%d",f[n][k--]);
        for(;k>0;k--)
        {
            printf("%04d",f[n][k]);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-08-03 09:15:06

Children’s Queue(递推)的相关文章

HDU 1297 Children’s Queue (递推、大数相加)

Children’s Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17918    Accepted Submission(s): 5976 Problem Description There are many students in PHT School. One day, the headmaster whose na

Children’s Queue(hdu1297+递推)

Children’s Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12101 Accepted Submission(s): 3953 Problem Description There are many students in PHT School. One day, the headmaster whose name is

(递推 大整数) Children’s Queue hdu1297

Children's Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16006    Accepted Submission(s): 5337 Problem Description There are many students in PHT School. One day, the headmaster whose n

递推趣题

在网上看到一个关于递推求解的课件,感觉里面的问题很经典有趣,层层递进,因此在这里记录一下. 1.在一个平面上有一个圆和n条直线,这些直线中的每一条在圆内同其他直线相交,假设没有三条直线相交于一点,试问这些直线将圆分成多少区域. 2.平面上有n条折线,问这些折线最多能将平面分割成多少块?(已知1—>2,2—>7) 3.设有n条封闭曲线画在平面上,而任何两条封闭曲线恰好交于两点,且任何三条封闭曲线不相交于同一点,问这些封闭曲线能把平面分割成的区域个数. 4.在2*n的长方形方格中,用n个1*2的骨

hdu 1297 Children’s Queue

Children's Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10540    Accepted Submission(s): 3390 Problem Description There are many students in PHT School. One day, the headmaster whose n

HDOJ/HDU 1297 Children’s Queue(推导~大数)

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

递推(二):递推法的应用

下面通过一些典型实例及其扩展来讨论递推法的应用. [例2]骨牌铺方格 在2×n的一个长方形方格中,用一种2×1的骨牌铺满方格.输入n(n<=40),输出铺放方案的总数. 例如n=3时,为2×3方格,骨牌的铺放方案有三种,如下图1所示. 图1  2×3方格的骨牌铺放方案 (1)编程思路. 设f[i]为铺满2*n方格的方案数,则有    f[i]=f[i-1]+f[i-2]. 其中,f[i-1]为铺满2*(n-1)方格的方案数(既然前面的2*(n-1)的方格已经铺满,那么最后一个只能是竖着放).f[

NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推 ||卡特兰数(转化成01字符串))

Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m.但是大家来的时间不同,随机次序来机房,带电脑的人直接准备比赛而没带电脑的人需要向带电脑并还没和别人公用的人求助(当然会答应).但是,如果不存在带电脑并还没和别人公用的人,那他就要等了,等是很让人头疼的,这就不和谐了,当然假如没有这样的情况发生比赛是很和谐的. Input 输入多组数据,每组数据只有一行m(

矩阵经典题目七:Warcraft III 守望者的烦恼(矩阵加速递推)

https://www.vijos.org/p/1067 很容易推出递推式f[n] = f[n-1]+f[n-2]+......+f[n-k]. 构造矩阵的方法:构造一个k*k的矩阵,其中右上角的(k-1)*(k-1)的矩阵是单位矩阵,第k行的每个数分别对应f[n-1],f[n-2],,f[n-k]的系数.然后构造一个k*1的矩阵,它的第i行代表f[i],是经过直接递推得到的.设ans[][]是第一个矩阵的n-k次幂乘上第二个矩阵,f[n]就是ans[k][1]. 注意:用__int64 #in