HDU 2604 Queuing

本文介绍了一个算法,用于计算给定长度的队列中偶数序列的数量,考虑到队列中元素的性别标识。通过动态规划和矩阵快速幂,解决了这一问题,并提供了AC代码实现。

Queuing
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 

分析
dp
状态转移方程的推导
计算f(n)时,当第 n 位为 m 时,对前面无影响,所以记f1 = f(n-1),当第 n 位为 f 时,观察后两位,不论后两位为 mf 还是 ff 都会对前面产生限制,继续观察后三位,若后三位为 fff 和 fmf 时不满足条件,当结尾为 mmf 时对前面无影响,记 f2 = f(n-3),当结尾为 mff 时,对前面产生限制,继续观察后四位,mmff和fmff中,只有mmff符合条件且不会对前面产生影响,记 f3 = f(n-4)。至此,所有可能的条件都已经找齐,所以 f(n) = f1 + f2 + f3,即 f(n) = f(n-1) + f(n-3) + f(n-4)。

矩阵快速幂
构造的矩阵A为 4*4 的矩阵,如下
0 1 0 0
0 0 1 0
0 0 0 1
1 1 0 1
要求 f(n),只需乘 n-3 次即可。

AC代码如下

#include<cstdio>
#include<cstring>
#define maxn 4
using namespace std;
struct S
{
    int m[4][4];
};
int a[maxn];
int l,m;

S mul(S a,S b)
{
    S c;
    memset(c.m,0,sizeof(c.m));
    for(int i = 0 ; i < 4 ; i++)
        for(int j = 0 ; j < 4 ; j++)
        {
            for(int k = 0 ; k < 4 ; k++)
                c.m[i][j] += a.m[i][k] * b.m[k][j];
            c.m[i][j] %= m;
        }
    return c;
}

S quick_power(S a,int k)
{
    if(k == 1) return a;
    else if(k & 1) return mul(quick_power(a,k-1),a);
    else
    {
        S c = quick_power(a,k >> 1);
        return mul(c,c);
    }
}

int main()
{
    a[0] = 1,a[1] = 2,a[2] = 4,a[3] = 6;
    S c;
    for(int i = 0 ; i < 4 ; i++)
        if(i == 2) c.m[3][i] = 0;
        else c.m[3][i] = 1;
    for(int i = 0 ; i < 3; i++)
        for(int j = 0 ; j < 4; j++)
        {
            if(j == i + 1) c.m[i][j] = 1;
            else c.m[i][j] = 0;
        }
    while(~scanf("%d%d",&l,&m))
    {
        if(l < 4)
        {
            printf("%d\n",a[l]%m);
            continue;
        }
        S b = quick_power(c,l-3);
        int ans = 0;
        for(int i = 0 ; i < 4 ; i++)
            ans += (b.m[3][i] * a[i]) % m;
        printf("%d\n",ans%m);
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值