【解题思路】

  先KMP出fail数组,再用fail数组求出M[i][j],表示上一次匹配到第i位,这次可以遇到多少种不同的字符,使之转而匹配到第j位。

  设集合S=[1,m]∩N

  又设f[i][j]表示共读入了i个字符,当前匹配到了第j位时,有多少种情况。有转移方程f[i][j]=Σf[i-1][k]*M[k][j](k∈S),边界f[0][i]=[i=0](i∈S)。

  上述转移方程等价于行向量f[i]=f[i-1]*M,故f[n]=f[0]*Mn,又f[0]=[1,0,...,0],故f[n]=Mn。答案即为∑f[n][i](i∈S)。复杂度O(m2log2n)。

【参考代码】

 #pragma GCC optimize(2)
#include <cstdio>
#include <cstring>
#define REP(i,low,high) for(register int i=(low);i<=(high);++i)
using namespace std; static int n,m,AwD; char jiry[]; int fail[]={}; struct matrix
{
int mat[][]; matrix() {memset(mat,,sizeof mat);}
matrix(const int&thr)
{
memset(mat,,sizeof mat); REP(i,,m-) mat[i][i]=thr;
}
int&operator()(const int&x,const int&y) {return mat[x][y];}
matrix&operator=(const matrix&thr)
{
return memcpy(mat,thr.mat,sizeof thr.mat),*this;
}
matrix&operator=(const int&thr)
{
memset(mat,,sizeof mat); REP(i,,m-) mat[i][i]=thr; return *this;
}
matrix operator*(const matrix&thr)
{
matrix ret; REP(i,,m-) REP(j,,m-) REP(k,,m-)
{
if((ret.mat[i][j]+=mat[i][k]*thr.mat[k][j]%AwD)>=AwD)
{
ret.mat[i][j]-=AwD;
}
}
return ret;
}
matrix&operator*=(const matrix&thr)
{
matrix ret; REP(i,,m-) REP(j,,m-) REP(k,,m-)
{
if((ret.mat[i][j]+=mat[i][k]*thr.mat[k][j]%AwD)>=AwD)
{
ret.mat[i][j]-=AwD;
}
}
return memcpy(mat,ret.mat,sizeof ret.mat),*this;
}
matrix operator^(const int&thr)
{
matrix bas(*this),ret();
for(register int i=thr;i;i>>=,bas*=bas) if(i&) ret*=bas;
return ret;
}
matrix&operator^=(const int&thr)
{
matrix bas(*this),ret();
for(register int i=thr;i;i>>=,bas*=bas) if(i&) ret*=bas;
return memcpy(mat,ret.mat,sizeof ret.mat),*this;
}
}M; int main()
{
scanf("%d%d%d%s",&n,&m,&AwD,jiry+),fail[]=;
REP(i,,m)
{
int idx=fail[i-]; for(;idx&&jiry[idx+]!=jiry[i];idx=fail[idx]);
fail[i]=idx+(jiry[idx+]==jiry[i]);
}
REP(i,,m-) REP(j,'','')
{
int idx=i; for(;idx&&jiry[idx+]!=j;idx=fail[idx]);
idx+=jiry[idx+]==j; if(idx<m&&++M(idx,i)==AwD) M(idx,i)=;
}
M^=n; int ans=; REP(i,,m-) if((ans+=M(i,))>=AwD) ans-=AwD;
return printf("%d\n",ans),;
}

bzoj1009题解的更多相关文章

  1. BZOJ1009:[HNOI2008]GT考试——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1009 Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0&l ...

  2. 【BZOJ1009】GT考试(KMP算法,矩阵快速幂,动态规划)

    [BZOJ1009]GT考试(KMP算法,矩阵快速幂,动态规划) 题面 BZOJ 题解 看到这个题目 化简一下题意 长度为\(n\)的,由\(0-9\)组成的字符串中 不含串\(s\)的串的数量有几个 ...

  3. BZOJ1009 [HNOI2008]GT考试 矩阵

    去博客园看该题解 题目 [bzoj1009][HNOI2008]GT考试 Description 阿申准备报名参加GT考试,准考证号为N位数X1X2….Xn(0<=Xi<=9),他不希望准 ...

  4. 【BZOJ1009】[HNOI2008]GT考试 next数组+矩阵乘法

    [BZOJ1009][HNOI2008]GT考试 Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的 ...

  5. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  6. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  7. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  8. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  9. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

随机推荐

  1. 使用Gradle发布项目到JCenter仓库 (转载)

    原文:使用Gradle发布项目到JCenter仓库 这篇文章介绍通过Gradle把开源项目发布到公共仓库JCenter中,方便你我他的事情,我们都是很懒的嘛.JCenter现在是Android Stu ...

  2. Dubbox服务的消费方开发

    开发步骤: (1)创建Maven工程(WAR)dubboxdemo-web ,在pom.xml引入依赖 ,同“dubboxdemo-service”工程.区别就是把tomcat插件的运行端口改为808 ...

  3. nodejs模块——fs模块 使用fs.read读文件

    使用fs.read读文件 fs.read() 先介绍fs.open. fs.open(path,flags,[mode],callback)方法用于打开文件,以便fs.read()读取. 参数说明: ...

  4. Python的历史及介绍

    Python的诞生 Python的创始人吉多·范罗苏姆(Guido van Rossum),在1989年12月的圣诞节期间,为了打发时间,决定开发一种新的脚本解释程序,作为ABC语言的继承. 现在,p ...

  5. Image另存为其他格式

    string imgPath = @"C:\ZHM01000001400001-01 青龙 白虎.tif"; Image img = Image.FromFile(imgPath) ...

  6. Dart编程实例 - 类型测试操作符 is!

    Dart编程实例 - 类型测试操作符 is! void main() { double n = 2.20; var num = n is! int; print(num); } 本文转自:http:/ ...

  7. Eclipse中安装SVN插件的艰难旅程

    我们写Java程序的人都知道Eclipse,也装过一些插件,比如Android开发的使用需要安装ADT等,如果代码提交的话我们可能需要安装git和svn的插件,但是这个插件我以前听过,但是一直没有安装 ...

  8. SpringBoot整合MongoDB,在多数据源下实现事务回滚。

    项目中用到了MongoDB,准备用来存储业务数据,前提是要实现事务,保证数据一致性,MongoDB从4.0开始支持事务,提供了面向复制集的多文档事务特性.能满足在多个操作,文档,集合,数据库之间的事务 ...

  9. (转)openfire插件开发(一)

    转:http://blog.csdn.net/lovexieyuan520/article/details/37774909 1. 在上一篇博文中,我介绍了Openfire3.9.3源代码导入ecli ...

  10. Spring定时器的使用方法

    Spring定时器主要通过Quartz Cron表达式来实现定时任务,注解用法如下: # 每月的最后1天 @Scheduled(cron = "0 0 18 28–31 * ?") ...