(递推 大整数) 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 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

对于各种排列而言,用f(n)表示其长度为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-1的排列,加一个M是可行的(最好是男孩的情形);对于之前长度n-2的排列,加加上FF是可行的(加MM有可能造成排列重复,这是最后是女孩的情形,并且前n-2个人是可行的排列);另外一种情况是,前n-2个人是不可行的排列,即最后的两个人是“MF”,再加上两个“FF”就变成可行的排列,这种情况也就是在n-4人可行的排列基础上加上“MFFF”。

C++:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int s[1001][200];
int main()
{
    s[1][0]=1;
    s[2][0]=2;
    s[3][0]=4;
    s[4][0]=7;
    int i,j,c,ans=0;
    for(i=5;i<=1000;i++)
    {
        for(j=0,c=0;j<200;j++)
        {
            ans=s[i-1][j]+s[i-2][j]+s[i-4][j]+c;
            c=ans/100000000;
            s[i][j]=ans%100000000;
        }
    }
    int n;
    while(cin>>n)
    {
        j=199;
        while(!s[n][j])
            j--;
        cout<<s[n][j];
        for(i=j-1;i>=0;i--)
            printf("%08d",s[n][i]);
        cout<<endl;
    }
    return 0;
}

用JAVA

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        BigInteger a[]=new BigInteger[1001];
        int n;
        while(in.hasNextInt()) {
            n=in.nextInt();
            a[1]=BigInteger.valueOf(1);
            a[2]=BigInteger.valueOf(2);
            a[3]=BigInteger.valueOf(4);
            a[4]=BigInteger.valueOf(7);
            for(int i=5;i<=n;i++) {
                a[i]=BigInteger.ZERO;
                a[i]=a[i].add(a[i-1]);
                a[i]=a[i].add(a[i-2]);
                a[i]=a[i].add(a[i-4]);
            }
            System.out.println(a[n]);
        }
    }
}

原文地址:https://www.cnblogs.com/Weixu-Liu/p/9164693.html

时间: 2024-08-03 09:15:11

(递推 大整数) Children’s Queue hdu1297的相关文章

POJ 2506 Tiling(递推+大整数加法)

http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. 1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 using namespace std; 6 7 int n; 8 char s[255][255]; 9 10

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

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(递推)

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 n

百度之星-大搬家-递推

问题描述: 近期B厂组织了一次大搬家,所有人都要按照指示换到指定的座位上.指示的内容是坐在位置ii上的人要搬到位置jj上.现在B厂有NN个人,一对一到NN个位置上.搬家之后也是一一对应的,改变的只有位次. 在第一次搬家后,度度熊由于疏忽,又要求大家按照原指示进行了一次搬家.于是,机智的它想到:再按这个指示搬一次家不就可以恢复第一次搬家的样子了.于是,B厂史无前例的进行了连续三次搬家. 虽然我们都知道度度熊的“机智”常常令人堪忧,但是不可思议的是,这回真的应验了.第三次搬家后的结果和第一次的结果完

HDU acm1028 整数划分 递归问题(递推)

我们用递归+记忆化的方法来解决普通整数划分问题:定义 f(n,m)为将整数n划分为一系列整数之和,其中加数 最大不超过m. 得到下面的递推关系式: 当n==1 || m==1 只有一种划分,即 1 或者 1+1+1......+1 当m>n 显然,等价于 f(n,n) 当m==n 此时:我考虑加数包含m与否的两种情况: 1)划分不包含m,即f(n,m-1)---所有m-1的划分 2)划分包含 m,此时只有一种即 m 所以当m==n时,有 f(n,m)=f(n,m-1)+1 当m<n时, 1)包

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

下面通过一些典型实例及其扩展来讨论递推法的应用. [例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[

【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. 在计算机科学中,二叉树是每个结点最多有两个子结点的有序树.通常子结点被称作“左孩子”和“右孩子”.二叉树被用作二叉搜索树和二叉堆.随后他又和他人讨论起了二叉搜索树.什么是二叉搜索树呢?二叉搜索树首先是一棵二叉树.设key[p]表示结点p上的数值.对于其中的每个结点p,若其存在左孩子lch,则key

HRBUST1589(数学递推)

彩桥 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %lld , %llu Java class name: Main [Submit] [Status] [Discuss] 题目链接:http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=75523 Description Cc用了药水之后状态全满,现在出现在他们眼前的是一个悬崖,悬崖之间有