BZOJ 1009 GT考试
Description
阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字。他的不吉利数学A1A2...Am(0<=Ai<=9)有M位,不出现是指X1X2...Xn中没有恰好一段等于A1A2...Am. A1和X1可以为0
Input
第一行输入N,M,K.接下来一行输入M位的数。 100%数据N<=10^9,M<=20,K<=1000 40%数据N<=1000 10%数据N<=6
Output
阿申想知道不出现不吉利数字的号码有多少种,输出模K取余的结果.
Sample Input
111
Sample Output
HINT
Source
利用“不吉利数字”构建ac自动机(我好像小题大做了,一个串好像没有必要),构建初始矩阵,接着矩阵乘法即可。
初始矩阵a[i][j]表示走一步从i节点走到j节点的方案数。
code:
#include<vector>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std; #define maxm 25
char buf[maxm];
int n,m,rhl,ans; struct node
{
int a[maxm*maxm][maxm*maxm],n,m;
node() {memset(a,,sizeof(a));n = m = ;} friend node operator *(node x,node y)
{
node z; z.n = x.n; z.m = y.m;
int i,j,k;
for (i = ;i <= z.n;++i)
for (j = ;j <= z.m;++j)
for (k = ;k <= x.m;++k)
(z.a[i][j] += (long long)x.a[i][k]*(long long)y.a[k][j]%rhl)%=rhl;
return z;
} inline node quick(node x,int k)
{
node ret; ret.n = x.n; ret.m = x.m;
for (int i = ;i <= ret.n;++i) ret.a[i][i] = ;
for (;k;k>>=,x = x*x)
if (k & )
ret = ret*x;
return ret;
} inline void calc() { for (int i = ;i <= m;++i) (ans += a[][i])%=rhl; }
}s; struct trie
{
int next[maxm][],fail[maxm],L,root;
bool end[maxm];
inline int newnode()
{
memset(next[L],-,sizeof(next[L]));
return ++L-;
} inline void init() {L = ; root = newnode();} inline void insert()
{
int len = strlen(buf),now = root,i;
for (i = ;i < len;++i)
{
if (next[now][buf[i]-''] == -) next[now][buf[i]-''] = newnode();
now = next[now][buf[i]-''];
}
end[now] = true;
} inline void build()
{
int now = root,i; queue <int> team;
fail[root] = root;
for (i = ;i < ;++i)
if (next[now][i] == -) next[now][i] = root;
else fail[next[now][i]] = root,team.push(next[now][i]);
while (!team.empty())
{
now = team.front(); team.pop();
for (i = ;i < ;++i)
if (next[now][i] == -) next[now][i] = next[fail[now]][i];
else fail[next[now][i]] = next[fail[now]][i],team.push(next[now][i]);
}
} inline void ready()
{
vector <int> son[maxm]; queue <int> team; int i,now,v,nn;
for (i = ;i < L;++i) if (fail[i] != i) son[fail[i]].push_back(i);
team.push(root);
while (!team.empty())
{
now = team.front(); team.pop();
nn = son[now].size();
for (i = ;i < nn;++i)
{
v = son[now][i];
if (end[now]) end[v] = true;
team.push(v);
}
}
} inline void make()
{
int i,j; s.n = s.m = L;
for (i = ;i < L;++i)
{
if (end[i]) continue;
for (j = ;j < ;++j)
if (!end[next[i][j]]) s.a[i+][next[i][j]+]++;
}
}
}ac; int main()
{
freopen("1009.in","r",stdin);
freopen("1009.out","w",stdout);
scanf("%d %d %d\n",&n,&m,&rhl);
ac.init();
scanf("%s",buf); ac.insert();
ac.build(); ac.ready(); ac.make();
s = s.quick(s,n); s.calc();
printf("%d",ans);
fclose(stdin); fclose(stdout);
return ;
}
BZOJ 1009 GT考试的更多相关文章
- BZOJ 1009 GT考试(ac自动机+矩阵DP)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1009 题意:给定一个长度为m的串s.有多少种长度为n的串不包含s? 思路:(1)将s插入 ...
- BZOJ 1009 GT考试 (AC自动机 + 矩阵乘法加速dp)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1009 题意: 准考证号为\(n\)位数\(X_1X_2....X_n(0<=X_ ...
- bzoj 做起走 -- bzoj 1009 GT 考试
现在每次做一道bzoj上的题,整个人都感觉升华了... 先是在网上各种搜题解.要么只有代码,要么有点讲解看不懂,对于从来没有耐心看完别人代码的我,只能一篇一篇的翻..然后终于在某2011级同学的某段话 ...
- [BZOJ 1009] [HNOI2008] GT考试 【AC自动机 + 矩阵乘法优化DP】
题目链接:BZOJ - 1009 题目分析 题目要求求出不包含给定字符串的长度为 n 的字符串的数量. 既然这样,应该就是 KMP + DP ,用 f[i][j] 表示长度为 i ,匹配到模式串第 j ...
- BZOJ 1009: [HNOI2008]GT考试( dp + 矩阵快速幂 + kmp )
写了一个早上...就因为把长度为m的也算进去了... dp(i, j)表示准考证号前i个字符匹配了不吉利数字前j个的方案数. kmp预处理, 然后对于j进行枚举, 对数字0~9也枚举算出f(i, j) ...
- BZOJ 1009 HNOI 2008 GT考试 递推+矩乘
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3679 Solved: 2254[Submit][Statu ...
- BZOJ 1009 [HNOI2008]GT考试 (KMP + 矩阵快速幂)
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 4266 Solved: 2616[Submit][Statu ...
- bzoj 1009: [HNOI2008]GT考试 -- KMP+矩阵
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MB Description 阿申准备报名参加GT考试,准考证号为N位数X1X2.. ...
- GT考试(bzoj 1009)
Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学A1A2...Am(0< ...
随机推荐
- [置顶] 获取激活码,激活myeclipse
myeclipse10.0 正式版下载地址: http://downloads.myeclipseide.com/downloads/products/eworkbench/indigo/instal ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结 我们从第一节搭建框架开始直到二十七节,权限管理已经告一段落,相信很多有跟上来的园友,已经 ...
- Latex插入图片 分类: LaTex 2014-11-18 20:07 261人阅读 评论(0) 收藏
在Latex中插入图片的方式很多,我这里只介绍自己常用的一种方式,欢迎大家指导. 我习惯于使用graphicx宏包来插入图片,有时候会配合上subfigure宏包来同时插入多幅图片组合. 首先,需要在 ...
- [PWA] Enable Push Notification in your web app
1. Clone the project: git clone https://github.com/GoogleChrome/push-notifications.git 2. install th ...
- qt 学习之路 :QML 语法
前面我们已经见识过 QML 文档.一个 QML 文档分为 import 和对象声明两部分.如果你要使用 Qt Quick,就需要 import QtQuick 2.QML 是一种声明语言,用于描述程序 ...
- [转] 学习使用:before和:after伪元素
http://www.w3cplus.com/css3/learning-to-use-the-before-and-after-pseudo-elements-in-css.html 如果你一直密切 ...
- Android read-only file system解决方法
adb shell su - mount -o rw,remount /system
- Java web 文件下载
/** * 下载文件 * @param msg */ public boolean printOutFile(String fileFullName,String fileName) { if (fi ...
- 零基础学习云计算及大数据DBA集群架构师【Linux系统环境及权限管理12.21-12.25】
从这周开始Linux的学习,老师是一位女老师,这到给了更多的信心,老师讲得很快,如果说只谈记命令的话是不多,但是要真正去理解,其实内容还是挺多的,我都是以老师讲的内容为主线,然后自己再看鸟哥的书做加深 ...
- Android Camera开发:使用TextureView和SurfaceTexture预览Camera 基础拍照demo
Google自Android4.0出了TextureView,为什么推出呢?就是为了弥补Surfaceview的不足,另外一方面也是为了平衡GlSurfaceView,当然这是本人揣度的.关于Text ...