hdu 2604 - Queuing

来源:互联网 发布:用友软件公司地址 编辑:程序博客网 时间:2024/04/30 06:52

Queuing

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 928 Accepted Submission(s): 404

Problem Description

Queues and Priority Queues are data structures(结构) which are known to most computer scientists. The Queue occurs often in our daily life. There are many people lined up at the lunch time. 


  Now we define(定义) that ‘f’ is short for female and ‘m’ is short for male. If the queue’s length is L, then there are 2L numbers of queues. For example, if L = 2, then they are ff, mm, fm, mf . If there exists a subqueue as fmf or fff, we call it O-queue else it is a E-queue.

Your task is to calculate(计算) the number of E-queues mod M with length L by writing a program.

 

Input

Input(投入) a length L (0 <= L <= 10 6) and M.

 

Output

            Output(输出) K mod M(1 <= M <= 30) where K is the number of E-queues with length L.

 

Sample Input

3 8
4 7
4 8

 

Sample Output

6
2
1

 

 

来自 <http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=3&sectionid=1&problemid=11>

 

大意:

排队是一件非常常见的事情,求在L人排出的队列中不包含fmffff子串的序列个数模M的值.

范围:L [  0  ~10^6 ], M [ 130 ]

分析:

想求出第n个人时符合要求的序列总个数,可以通过第n-1个人后加一个人来求得。

1.最后一位添加m的情况:

由题可知,只要n-1个人符合要求,则添m也一定符合要求,既有Fn-1)种排法

2.最后一位添加f的情况:

要使最后一位加f也符合要求,则前两位不能是fmffn-3之前的是符合要求的,既在n-3符合情况的条件下添加mfmm得到n-1,又只要n-3符合条件,则后两位添加mm的定然也符合条件,有Fn-3)种可能。而在n-3后面添加mf要符合要求,只有n-3的最后一位是m才符合,既n-4位符合要求的后面加一个mFn-4)种情况。

综上:Fn = Fn-1+ Fn-3+ Fn-4)。

接下来只要知道(a+b% n == ( (a%n) + (b%n) ) % n 就可以ac这题了 ^_^

 

 

代码:

 

#include<iostream>
using namespace std;
const int
MAX = 1000001;
char
L[MAX][31];
int main
()
{

    for
(int j = 1; j < 31; j++)
    {

         L[0][j] = 1 % j;
         L[1][j] = 2 % j;
         L[2][j] = 4 % j;
         L[3][j] = 6 % j;
    }

    for
(int i = 4; i < MAX; i++)
    {

        for
(int j = 1; j < 31; j++)
        {

            L[i][j] = (L[i-1][j] + L[i-3][j] + L[i-4][j]) % j;
        }
    }

    int
l,m;
    while
(scanf("%d%d",&l,&m) != EOF)
    {

        printf("%d\n",L[l][m]);
    }

    return
0;
}

0 0