HDU 2604 Queuing (递推+矩阵快速幂)

【题目链接】:click here~~

【题目大意】:

n个人排队,f表示女,m表示男,包含子串‘fmf’和‘fff’的序列为O队列,否则为E队列,有多少个序列为E队列。

【思路】:

用f(n)表示n个人满足条件的结果,那么如果最后一个人是m的话,那么前n-1个满足条件即可,就是f(n-1);

如果最后一个是f那么这个还无法推出结果,那么往前再考虑一位:那么后三位可能是:mmf, fmf, mff, fff,其中fff和fmf不满足题意所以我们不考虑,但是如果是

mmf的话那么前n-3可以找满足条件的即:f(n-3);如果是mff的话,再往前考虑一位的话只有mmff满足条件即:f(n-4)

所以f(n)=f(n-1)+f(n-3)+f(n-4),递推会跪,可用矩阵快速幂

构造一个矩阵:

来图一张:

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int siz=4;
int k,mod;
struct mut
{
    int mat[4][4];  // size of matric
    mut()
    {
        memset(mat,0,sizeof(mat));
    }
    void init(int v)
    {
        for(int i=0; i<=4; ++i)
            mat[i][i]=v;
    }
};
mut operator * (mut a,mut b)
{
    mut c;
    for(int i=0; i<4; ++i)
    {
        for(int j=0; j<4; ++j)
        {
            c.mat[i][j]=0;
            for(int k=0; k<4; ++k)
            {
                c.mat[i][j]+=(a.mat[i][k]*b.mat[k][j])%mod;
                c.mat[i][j]%=mod;
            }
        }
    }
    return c;
}
mut operator ^(mut a,LL n)
{
    mut c;
    c.init(1);
    while(n)
    {
        if(n&1) c=a*c;
        a=a*a;
        n>>=1;
    }
    return c;
}
int main()
{
    mut a,b,c;
    a.mat[0][0]=9;
    a.mat[1][0]=6;
    a.mat[2][0]=4;
    a.mat[3][0]=2;
    b.init(0);
    b.mat[0][0]=b.mat[0][2]=b.mat[0][3]=b.mat[1][0]=b.mat[2][1]=b.mat[3][2]=1;
    while(~scanf("%d%d",&k,&mod))
    {
        if(k==0){
            puts("0");
        }
        else if(k<=4){
            printf("%d\n",a.mat[4-k][0]%mod);
        }
        else{
            c=b^(k-4);
            c=c*a;
            printf("%d\n",c.mat[0][0]%mod);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-05 18:46:28

HDU 2604 Queuing (递推+矩阵快速幂)的相关文章

hdu 2604 递推 矩阵快速幂

HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstring> using namespace std; const int N = 5; int msize, Mod; struct Mat { int mat[N][N]; }; M

HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 249    Accepted Submission(s): 140 Problem Description Farmer John likes to play mathematics games with his N cows. Recently, t

2014 BNU 邀请赛E题(递推+矩阵快速幂)

Elegant String 题意:给定一个字符串,由0-k数字组成,要求该串中,子串不包含0-k全排列的方案数 思路:dp[i][j]表示放到i个数字,后面有j个不相同,然后想递推式,大概就是对应每种情况k分别能由那几种状态转移过来,在纸上画画就能构造出矩阵了,由于n很大,所以用快速幂解决 代码: #include <stdio.h> #include <string.h> const long long MOD = 20140518; int t; long long n; i

HDU 2842 (递推+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个环可以取下或放上,cost=1.求最小cost.MOD 200907. 解题思路: 递推公式 题目意思非常无聊,感觉是YY的. 设$dp[i]$为取第i个环时的总cost. $dp[1]=1$,$dp[2]=2$,前两个环取下是没有条件要求的. 从i=3开始,由于条件对最后的环限制最大,所以从最后一

HDU - 6185 Covering(暴搜+递推+矩阵快速幂)

Covering Bob's school has a big playground, boys and girls always play games here after school. To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets. Meanwh

Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)

题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但是N最大能到int的最大值, 直接循环推解不了 所以就得用矩阵快速幂咯 现在就看转移矩阵长什么样了 Mi表示要求的矩阵 转移矩阵用A表示 A * Mi = Mi+1 矩阵Mi里面至少得有 F[i-1] F[i-2] i ^ 4 Mi+1就相应的有 F[i] F[i-1] (i+1)^4 (i+1)^4 =

递推+矩阵快速幂 HDU 2065

1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 7 //矩阵大小上限 8 const int SIZ=100; 9 int MOD=100; 10 11 //矩阵大小为n*m,初始化全部为0 12 struct mat 13 { 14 int n,m; 15 int ar[SIZ][SIZ]; 16 mat() 17 { 18 memset

HDU2604-Queuing(递推+矩阵快速幂)

题目链接 题意:男为f,女为m,求在长度为L的队列中不存在fmf,fff这样子序列的序列的个数. 思路:又是递推题,假设长度为L的队列中存在的序列个数为f(L),那么考虑最后一个放的字母,假设最后一个放m,那么前L-1个可以随意排列,即个数为f(L - 1):如果最后一个放f,那么考虑后两个字母,可能出现的情况为ff,mf,这样比较难判断是否符合题目要求的,所以我们考虑后三个字母,可能出现的情况就为fff,mff,fmf,mmf,显而易见mff,mmf符合题意.当后三个为mmf时,前L-3个可以

2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)

Happy Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1146    Accepted Submission(s): 491 Problem Description Little Q wants to buy a necklace for his girlfriend. Necklaces are single