hdoj.1297 Children’s Queue【大数+排列组合】 2015/08/06

来源:互联网 发布:淘宝里食品类目划分 编辑:程序博客网 时间:2024/06/10 18:59

Children’s Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12414    Accepted Submission(s): 4051


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
123
 

Sample Output
124
 

Author
SmallBeer (CML)
 

Source
杭电ACM集训队训练赛(VIII)  

递推公式为:f(n) = f(n-1) + f(n-2) + f(n-4)
注:人越多,组合数越大,需要大数相加
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int ch[1010][1010];int main(){    memset(ch,0,sizeof(ch));    ch[0][0] = ch[1][0] = ch[2][0] = ch[3][0] = 1;    ch[0][1] = 1;    ch[1][1] = 1;    ch[2][1] = 2;    ch[3][1] = 4;    for( int i = 4 ; i <= 1000 ; ++i ){        for( int j = 1 ; j <= ch[i-1][0] ; ++j )            ch[i][j] = ch[i-1][j] + ch[i-2][j] + ch[i-4][j];        for( int j = 1 ; j <= ch[i-1][0] ; ++j )            if( ch[i][j] > 9 ){                ch[i][j+1] += ch[i][j] / 10;                ch[i][j] %= 10;            }        ch[i][0] = ch[i][ch[i-1][0]+1] ? ch[i-1][0] + 1 : ch[i-1][0];    }    int n;    while( scanf("%d",&n)!=EOF ){        for( int i = ch[n][0] ; i > 0 ; --i )            printf("%d",ch[n][i]);        printf("\n");    }    return 0;}


0 0
原创粉丝点击