【解题思路】

  先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. 微信小程序分享功能的path路径

    表示被微信小程序坑惨了 糟心的开始写,老板说先不上太复杂,就先显示了两个页面,然后开开心心的发布,测试了几遍,没啥问题呀.结果,一上上去,就被老板批了! 啥呀! 这分享怎么这个鬼样子!明明我看文档都是 ...

  2. 封装加减乘除函数 解决JS 浮点数计算 Bug

    计算机内部的信息都是由二进制方式表示的,即0和1组成的各种编码,但由于某些浮点数没办法用二进制准确的表示出来,也就带来了一系列精度问题.当然这也不是JS独有的问题. 例如, 我们在用JS做浮点运算会遇 ...

  3. CentOS7.6 部署asp.net core2.2 应用

    1.安装.net Core SDK 在安装.NET之前,您需要注册Microsoft密钥,注册产品存储库并安装所需的依赖项.这只需要每台机器完成一次. 打开终端并运行以下命令: sudo rpm -U ...

  4. APICloud框架--sublime使用自定义loader

    官方的apploader调试器,只是有官方的一些模块,如果我们使用非官方的模块就要使用自定义loader进行调试.接下来就走一边sublime设置自定义loader的步骤 修改config.xml 打 ...

  5. input限制字符长度 - composition

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. NOIp2018集训test-10-22 (联考六day2)

    中间值 两个log肯定会被卡.我用的第一种做法,就是要各种特判要在两个序列都要二分比较麻烦. //Achen #include<bits/stdc++.h> #define For(i,a ...

  7. js面向对象编程(第2版)——js继承多种方式

    附带书籍地址: js面向对象编程(第2版)

  8. php开发面试题---Mysql常用命令行大全

    php开发面试题---Mysql常用命令行大全 一.总结 一句话总结: 常见关键词:create,use,drop,insert,update,select,where ,from.inner joi ...

  9. 多图上传控制器及模型代码(2)thinkphp5+layui实现多图上传保存到数据库,可以实现图片自由排序,自由删除。

    公共css代码 <style> .layui-upload-img { width: 90px; height: 90px; margin: 0; } .pic-more { width: ...

  10. 一道腾讯面试题:如何快速判断某 URL 是否在 20 亿的网址 URL 集合中?布隆过滤器

    何为布隆过滤器 还是以上面的例子为例: 判断逻辑: 多次哈希: Guava的BloomFilter 创建BloomFilter 最终还是调用: 使用: 算法特点 使用场景 假设遇到这样一个问题:一个网 ...