zoukankan      html  css  js  c++  java
  • (递推 大整数) 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]);
            }
        }
    }
  • 相关阅读:
    461. Hamming Distance
    342. Power of Four
    326. Power of Three
    368. Largest Divisible Subset java solutions
    95. Unique Binary Search Trees II java solutions
    303. Range Sum Query
    160. Intersection of Two Linked Lists java solutions
    88. Merge Sorted Array java solutions
    67. Add Binary java solutions
    14. Longest Common Prefix java solutions
  • 原文地址:https://www.cnblogs.com/Weixu-Liu/p/9164693.html
Copyright © 2011-2022 走看看