题目链接

给一个长度为n的字符串, 每个字符可以使f或m。 问你不包含子串fmf以及fff的字符串数量有多少。

令0表示mm结尾, 1表示mf, 2表示ff, 3表示fm。

那么

f(n+1, 0) = f(n, 0) + f(n, 3)

f(n+1, 1) = f(n, 0)

f(n+1, 2) = f(n, 1)

f(n+1, 3) = f(n, 1) + f(n, 2)

所以构造出矩阵

{1, 0, 0, 1}

{1, 0, 0, 0}

{0, 1, 0, 0}

{0, 1, 1, 0}

然后快速幂, 最后的答案就是矩阵第一列的值相加。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int a[4][4] = {
{1, 0, 0, 1},
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 1, 1, 0},
};
int mod;
struct Matrix
{
int f[4][4];
Matrix() {
mem(f);
}
}m;
Matrix operator * (Matrix a, Matrix b) {
Matrix c;
for(int k = 0; k<4; k++) {
for(int j = 0; j<4; j++) {
for(int i = 0; i<4; i++) {
c.f[i][j] += a.f[i][k]*b.f[k][j];
c.f[i][j] %= mod;
}
}
}
return c;
}
Matrix operator ^(Matrix a, ll b) {
Matrix tmp;
for(int i = 0; i<4; i++)
tmp.f[i][i] = 1;
while(b) {
if(b&1)
tmp = tmp*a;
a = a*a;
b>>=1;
}
return tmp;
}
int main()
{
int n;
while(cin>>n>>mod) {
memcpy(m.f, a, sizeof(a));
m = m^n;
int ans = 0;
for(int i = 0; i < 4; i++) {
ans = (ans + m.f[i][0])%mod;
}
cout<<ans<<endl;
}
return 0;
}

hdu 2604Queuing dp+ 矩阵快速幂的更多相关文章

  1. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

  2. bnuoj 34985 Elegant String DP+矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...

  3. 【BZOJ】2004: [Hnoi2010]Bus 公交线路 状压DP+矩阵快速幂

    [题意]n个点等距排列在长度为n-1的直线上,初始点1~k都有一辆公车,每辆公车都需要一些停靠点,每个点至多只能被一辆公车停靠,且每辆公车相邻两个停靠点的距离至多为p,所有公车最后会停在n-k+1~n ...

  4. 【BZOJ】4861: [Beijing2017]魔法咒语 AC自动机+DP+矩阵快速幂

    [题意]给定n个原串和m个禁忌串,要求用原串集合能拼出的不含禁忌串且长度为L的串的数量.(60%)n,m<=50,L<=100.(40%)原串长度为1或2,L<=10^18. [算法 ...

  5. BZOJ5298 CQOI2018 交错序列 【DP+矩阵快速幂优化】*

    BZOJ5298 CQOI2018 交错序列 [DP+矩阵快速幂优化] Description 我们称一个仅由0.1构成的序列为"交错序列",当且仅当序列中没有相邻的1(可以有相邻 ...

  6. HDU.2640 Queuing (矩阵快速幂)

    HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...

  7. Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】

    题意: 有b个blocks,每个blocks都有n个相同的0~9的数字,如果从第一个block选1,从第二个block选2,那么就构成12,问对于给定的n,b有多少种构成方案使最后模x的余数为k. 分 ...

  8. codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)

    题目链接:http://codeforces.com/contest/821/problem/E 题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y- ...

  9. [BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂)

    [BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂) 题面 阿申准备报名参加GT考试,准考证号为N位数X1X2-.Xn,他不希望准考证号上出现不吉利的数字.他的不吉利数学A ...

随机推荐

  1. 卸载cloudera manager

    卸载Cloudera-Manager sudo /usr/share/cmf/uninstall-cloudera-manager.sh 一直选择Yes 就好 卸载完成后,它会问你是否要将databa ...

  2. 阿牛的EOF牛肉串

    #include <iostream>using namespace std;long long s0,s1,s2,s3;int main(){ int i,n; while(cin> ...

  3. WARNING [Project: :app] To shrink resources you must also enable ProGuard

    新版本的Android Gradle plugin中,对于resource有了更加一步的管理,可以把unused resource移除,不仅是自己工程,并且library里面也可以没有用到的,也可以移 ...

  4. lex 和 yacc 的区别与联系

    lex负责词法解析,而yacc负责语法解析,其实说白了就是lex负责根据指定的正则表达式,将输入的字符串匹配成一个一个的token,同时允许用户将当前匹配到的字符串进行处理,并且允许返回一个标识当前t ...

  5. VS 对于LINK fatal Error 问题 解决方案

    方案1:    点击“项目”-->“属性” --> “清单工具”,    然后选择"输入和输出’ --> ‘嵌入清单’,将后面的‘是’改成‘否’就可以了 方案2: 在VS安 ...

  6. meteor学习

    meteor学习 描述:是一套完整的用于开发现代化跨平台实时应用的整体解决方案 不是IDE(集成开发环境) 不是API接口 不是前端框架 不是后端框架 包含 命令行工具 meteor command ...

  7. IC卡

    本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . IC卡 (Integrated Circuit Card,集成电路卡),也称智能卡(Smart card).智慧卡(Intelligent ...

  8. Tomcat Remote Debug操作和原理

    操作篇 这部分主要讲,如何开启tomcat远程调试,并佐以实例.本文方式适用于windows和linux. 假设有两台机器,A是tomcat服务器所在机器,B是IDE安装机器.A和B可以是同一台机器, ...

  9. cnn softmax regression bp求导

    内容来自ufldl,代码参考自tornadomeet的cnnCost.m 1.Forward Propagation convolvedFeatures = cnnConvolve(filterDim ...

  10. Struts2五、Struts1与Struts2的区别

    Struts1和Struts2的区别和对比: Action 类:  • Struts1要求Action类继承一个抽象基类.Struts1的一个普遍问题是使用抽象类编程而不是接口,而struts2的Ac ...