HDU 2604-Queuing(矩阵快速幂)

address : http://acm.hdu.edu.cn/showproblem.php?pid=2604
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

矩阵快速幂,只看题,我也不知道为什么用矩阵快速幂,也许做多了就知道了吧。
题目翻译:
有一个长度为L的队列,由男女组成,分别用字符m和f表示,如果这串字符含有fmf或者含有fff,则该串叫做O-queue,否则该串叫做E-queue.
先给出一个数L为字符串长度,求有m,f组成的E-queue型字符串的个数。
难点:
这是一个和斐波那契数列相似的递推问题,而且公式不好推导。
设F[n]为字符串长度为n时,由m,f组成的D-queue型字符串的个数。

首先先分析一下图形
我们可以倒着考虑 他在第n位有两种情况 f或m 如下图
这里写图片描述
第一种情况 当第n位为f时, 为了满足情况n-1 可以为f或者m 两种情况,这两种情况会有不同的结果,如果n-1 为f 那么我们就可以确定n-2, n-3一定为 m 那么第n-4位就可以随便填了 都不会影响结果 后4位就是唯一确定的。前n-4位个数就为 f(n-4),
同理, 我们看 当n-1位为m的情况,为了满足条件, n-2必须为m 那么n-3位 也可以随便填 后三位确定 那么f(n) 个数再加上 f(n-3)

再看第二种情况。 当第n位为m时 第n-1位 不管填什么都满足条件, 所以f(n)得个数还要加上f(n-1) 的个数可能性

因此得到递推公式 f(n) = f(n-1)+f(n-3)+f(n-4)

我们可以画图推出矩阵关系
这里写图片描述

还有需要注意的 就是 f(4) 可不能 由公式推出来 需要自己算, f(0),f(1),f(2),f(3),f(4) 口算一下就好 最后乘一下 ,剩下 就是快速幂了 ,

code:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

struct node
{
    int m[5][5];
}ans, a;
int l, m;
node mul(node ans, node a)
{
    node p;
    memset(p.m, 0, sizeof(p.m));
    for(int i = 0; i < 4; i++)  ///i行
        for(int j = 0; j < 4; j++)  ///j列
            for(int k = 0; k < 4; k++)
                p.m[i][j] = (p.m[i][j]+ans.m[i][k]*a.m[k][j])%m;
    return p;
}
void solve(int l)
{
    memset(ans.m, 0 ,sizeof(ans.m));
    for (int i=0; i<4; i++)
        ans.m[i][i] = 1;
    while (l)
    {
        if (l&1)
            ans = mul(ans, a);
        a = mul(a, a);
        l>>=1;
    }
}
int main()
{
    int v[5];
    v[0] = 9;
    v[1] = 6;
    v[2] = 4;
    v[3] = 2;
    while (~scanf("%d%d",&l,&m))
    {
        if (l==0)
            cout<<"0"<<endl;
        else if (l<=4)
        {
            cout<<v[4-l]%m<<endl;
        }
        else
        {
            memset(a.m, 0, sizeof(a.m));
            a.m[0][0] = 1;
            a.m[0][2] = 1;
            a.m[0][3] = 1;
            for (int i=1; i<4; i++)
                a.m[i][i-1] = 1;
            solve(l-4);
            int show = 0;
            for (int i=0; i<4; i++)
            {
                show = (show+ans.m[0][i]*v[i])%m;
            }
            cout<<show<<endl;
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值