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 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个小朋友有多少种坐法

题目分析

首先长度为1时,只有1种可能,即“M”;

  长度为2时,有2种可能,即“FF”和“MM”;

  长度为3时,有4种可能,即“FFF”、“FFM”、“MFF”和“MMM”;

  长度为4时,有7种可能,即“FFFF”、“FFFM”、“FFMM”、“MFFM”、“MFFF”、“MMFF”、“MMMM”;
当n>4时,我们可以这么想:
  如果第n个人是M,符合条件,这样的情况有f(n-1)个,因为是直接在n-1的情况下在最后加上了一个M

  如果第n个人是F,那么就需要考虑倒数第二个人,如果倒数第二个人是F,这是可以的,那么也就相当于 在n-2的基础上加了一个FF

          但是注意,刚刚我们是在n-2的基础上加了一个FF,也就是说,默认前n-2是合理的,但是也存在不合理的情况 也就是说 前面n-2是以MF结尾的,这时候加上FF也是合理的,也就相当于在n-4的基础上加上了MFFF

综上 我们可以列出来递推方程:
  f(n) = f(n-1)+f(n-2)+f(n-4)

剩下的就只是将大数模板套进去就好了

代码:

#include<bits/stdc++.h>

using namespace std;

int n,i;
string bigadd(string a,string b)
{
    int jin=0,i;
    char ai,bi;
    string anss=a;
    int lena=a.size();
    int lenb=b.size();
    int lenmax=max(lena,lenb);
    int p=lena-1;
    int q=lenb-1;
    for(i=lenmax-1;i>=0;i--)
    {
        if(p<0)
        ai=‘0‘;
        else
        ai=a[p];
        if(q<0)
        bi=‘0‘;
        else
        bi=b[q];
        anss[i]=((ai-‘0‘+bi-‘0‘+jin)%10)+‘0‘;
        jin=(ai-‘0‘+bi-‘0‘+jin)/10;
        p--;
        q--;
    }
    if(jin)
    {
        char x=jin+‘0‘;
        anss=x+anss;
    }
    return anss;
}
 int main()
 {
    string a[1008];
    a[1]="1";
    a[2]="2";
    a[3]="4";
    a[4]="7";
    for(i=5;i<1008;++i)
           a[i]=bigadd(bigadd(a[i-1],a[i-2]),a[i-4]);  //这里需要注意的是,我之前用的是bigadd(bigadd(a[i-4],a[i-2]),a[i-1]),但是WA了,我仔细想了想,这是由于我的大数相加模板导致的,如果后加的数比前面的数位数大,就会出现位数丢失的问题,所以必须先将最大的a[i-1]与a[i-2]相加。
    while(scanf("%d",&n)!=EOF)
    {
        cout<<a[n]<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/11333490.html

时间: 2024-10-01 06:39:15

HDU 1297 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): 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

HDOJ 1297 Children’s Queue

JAVA大数.... Children's Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10390    Accepted Submission(s): 3333 Problem Description There are many students in PHT School. One day, the headmas

uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

题目链接:uva 1478 - Delta Wave 题目大意:对于每个位置来说,可以向上,水平,向下,坐标不能位负,每次上下移动最多为1, 给定n问说有多少种不同的图.结果对10100取模. 解题思路:因为最后都要落回y=0的位置,所以上升的次数和下降的次数是相同的,并且上升下降的关系满足出栈入栈的关系.即卡特兰数. 所以每次枚举i,表示有i个上升,i个下降,用组合数学枚举出位置,然后累加求和. C(2?in)?f(i)=C(2?i?2n)?f(i?1)?(n?2?i+1)?(n?2?i+2)

hdu 4869 Turn the pokers(递推&amp;组合数学&amp;逆元)

Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1279    Accepted Submission(s): 466 Problem Description During summer vacation,Alice stay at home for a long time, with nothing t

hdu 1284 钱币兑换问题 (递推 || DP || 母函数)

钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5069    Accepted Submission(s): 2868 Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input 每行只有一个正整数N,N小于32768. Outpu

HDU Tickets(简单的dp递推)

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 972    Accepted Submission(s): 495 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However,

hdu 2050 折线分割平面 (递推)

折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15709    Accepted Submission(s): 10836 Problem Description 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目.比如,一条折线可以将平面分成两部分,两条折线最多可以将平面

HDU 1250 Hat&#39;s Fibonacci(Java大数相加)+讲解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n -