HDU:2604 Queuing


Queuing

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5620    Accepted Submission(s): 2439


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 2 L 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
 


题目翻译:

有一个长度为L的队列,由男女组成,分别用字符m和f表示,如果这串字符含有fmf或者含有fff,则该串叫做O-queue,否则该串叫做E-queue.

先给出一个数L为字符串长度,求有m,f组成的E-queue型字符串的个数。

难点:

这是一个和斐波那契数列相似的递推问题,而且公式不好推导。

设F[n]为字符串长度为n时,由m,f组成的D-queue型字符串的个数。

现在来分析:




而且可知,f[0]等于0,f[1]=2,f[2] =4,f[3]=6,f[4]=9;

注意f[4]并不是由,f[0],f[1],f[3]推导的,是长度大于4的时候满足上述推导的规律,之前我以为4就满足规律,然后一直没对。


#include<stdio.h>
#include<iostream>
#include<string.h>

using namespace std;

const int maxn = 6;
int L,M;    ///L为队列长度,M为取模
int f[10];
struct Matrix
{
    int m[maxn][maxn];
}ans,res;
Matrix Mul(Matrix a,Matrix b)
{
    Matrix tmp;
    for(int i = 1; i <= 4; i++)
        for(int j = 1; j <= 4; j++)
            tmp.m[i][j] = 0;
    for(int i = 1; i <= 4; i++)
        for(int j = 1; j <= 4; j++)
            for(int k = 1; k <= 4; k++)
                tmp.m[i][j] = (tmp.m[i][j]+a.m[i][k]*b.m[k][j])%M;
    return tmp;
}
void QuickPower(int N)
{
    ///将ans初始化为单位阵
    for(int i = 1; i <= 4; i++)
        for(int j = 1; j <= 4; j++)
        {
            if(i == j) ans.m[i][j] = 1;
            else ans.m[i][j] = 0;
        }
    while(N)
    {
        if(N&1)
            ans = Mul(ans,res);
        res = Mul(res,res);
        N = N>>1;
    }
}
int main()
{
    f[0] = 0;
    f[1] = 2;
    f[2] = 4;
    f[3] = 6;
    f[4] = 9;
    while(~scanf("%d%d",&L,&M))
    {
        if(L <= 4)
            printf("%d\n",f[L]%M);
        else
        {
            memset(res.m,0,sizeof(res.m));
            res.m[1][1] = 1;
            res.m[1][2] = 0;
            res.m[1][3] = 1;
            res.m[1][4] = 1;
            for(int i = 2; i <= 4; i++)
                res.m[i][i-1] = 1;
            QuickPower(L-4);
            int _ans = 0;
            for(int i = 1; i <= 4; i++)
            {
                _ans = (_ans + ans.m[1][i]*f[4-i+1])%M;
            }
            printf("%d\n",_ans);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值