hdoj 1297 Children’s Queue 【高精度】【递推】

题意:有n个人,每一个人可以是男孩也可以是女孩,要求每个女孩不能单独一个,也就是一个女孩的左右紧挨的位置至少要有一个女孩。问这样的队列有几个。

分析:设f(n)是n的排列的数目,这时候来一个人:

一:如果是男孩,那么f(n ) = f(n-1)

二:如果是女孩,如果前n-2是合法的,那么f(n) = f(n-2);如果前n-2不合法的,那么n-2队列的末尾两个同学肯定是男+女,那么再加上后来的女孩,又合法了,此时f(n) = f(n-4);

综上:f(n) = f(n-1)+f(n-2)+f(n-4);

又因为数据范围比较大,要用高精度。

代码:

#include <stdio.h>
#include <string.h>
#define M 1050
int a[M][M];
void table(){
    int i, j;
    memset(a, 0, sizeof(a));
    a[1][0] =1; a[2][0] = 2; a[3][0] = 4; a[4][0] = 7;
    for(i = 5; i < 1050; i ++){
        for(j = 0; j < M; j ++)
            a[i][j] = a[i-1][j]+a[i-2][j]+a[i-4][j];
        for(j = 0; j < M; j ++){
            if(a[i][j] >= 10){
                int temp = a[i][j]/10;
                a[i][j]%=10;
                a[i][j+1] += temp;
            }
        }
    }
}
int main(){
    table();
    int n, i, j;
    while(scanf("%d", &n) == 1){
        i = M-1;
        while(a[n][i] == 0) i --;
        printf("%d", a[n][i]);
        while((--i) >= 0) printf("%d", a[n][i]);
        puts("");
    }
    return 0;
} 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1297

时间: 2024-08-10 23:28:03

hdoj 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): 17918    Accepted Submission(s): 5976 Problem Description There are many students in PHT School. One day, the headmaster whose na

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

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

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

ACdream 1420 High Speed Trains【Java大数高精度 + 递推】

High Speed Trains Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) 链接:http://acdream.info/problem?pid=1420 Problem Description The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was a

[bzoj1002][FJOI2007]轮状病毒-题解[基尔霍夫矩阵][高精度][递推]

Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下图所示 N轮状病毒的产生规律是在一个N轮状基中删去若干条边,使得各原子之间有唯一的信息通道,例如共有16个不同的3轮状病毒,如下图所示 现给定n(N<=100),编程计算有多少个不同的n轮状病毒 Input 第一行有1个正整数n Output 计算出的不同的n轮状病毒数输出 Sample Input

POJ 2506 Tiling(高精度+递推)

高精度模版(bin神的) /* * 高精度,支持乘法和加法 */ struct BigInt { const static int mod = 10000; const static int DLEN = 4; int a[600],len; BigInt() { memset(a,0,sizeof(a)); len = 1; } BigInt(int v) { memset(a,0,sizeof(a)); len = 0; do { a[len++] = v%mod; v /= mod; }w

hdoj 2524 矩形A + B【递推】

矩形A + B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5187    Accepted Submission(s): 4034 Problem Description 给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格. Input 第一行输入一个t, 表示有t组数据,然后每行输入n,m,