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

4 3 100
111

Sample Output

81

HINT

 

Source

利用“不吉利数字”构建ac自动机(我好像小题大做了,一个串好像没有必要),构建初始矩阵,接着矩阵乘法即可。

初始矩阵a[i][j]表示走一步从i节点走到j节点的方案数。

code:

  1. #include<vector>
  2. #include<queue>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<cstdlib>
  6. using namespace std;
  7.  
  8. #define maxm 25
  9. char buf[maxm];
  10. int n,m,rhl,ans;
  11.  
  12. struct node
  13. {
  14. int a[maxm*maxm][maxm*maxm],n,m;
  15. node() {memset(a,,sizeof(a));n = m = ;}
  16.  
  17. friend node operator *(node x,node y)
  18. {
  19. node z; z.n = x.n; z.m = y.m;
  20. int i,j,k;
  21. for (i = ;i <= z.n;++i)
  22. for (j = ;j <= z.m;++j)
  23. for (k = ;k <= x.m;++k)
  24. (z.a[i][j] += (long long)x.a[i][k]*(long long)y.a[k][j]%rhl)%=rhl;
  25. return z;
  26. }
  27.  
  28. inline node quick(node x,int k)
  29. {
  30. node ret; ret.n = x.n; ret.m = x.m;
  31. for (int i = ;i <= ret.n;++i) ret.a[i][i] = ;
  32. for (;k;k>>=,x = x*x)
  33. if (k & )
  34. ret = ret*x;
  35. return ret;
  36. }
  37.  
  38. inline void calc() { for (int i = ;i <= m;++i) (ans += a[][i])%=rhl; }
  39. }s;
  40.  
  41. struct trie
  42. {
  43. int next[maxm][],fail[maxm],L,root;
  44. bool end[maxm];
  45. inline int newnode()
  46. {
  47. memset(next[L],-,sizeof(next[L]));
  48. return ++L-;
  49. }
  50.  
  51. inline void init() {L = ; root = newnode();}
  52.  
  53. inline void insert()
  54. {
  55. int len = strlen(buf),now = root,i;
  56. for (i = ;i < len;++i)
  57. {
  58. if (next[now][buf[i]-''] == -) next[now][buf[i]-''] = newnode();
  59. now = next[now][buf[i]-''];
  60. }
  61. end[now] = true;
  62. }
  63.  
  64. inline void build()
  65. {
  66. int now = root,i; queue <int> team;
  67. fail[root] = root;
  68. for (i = ;i < ;++i)
  69. if (next[now][i] == -) next[now][i] = root;
  70. else fail[next[now][i]] = root,team.push(next[now][i]);
  71. while (!team.empty())
  72. {
  73. now = team.front(); team.pop();
  74. for (i = ;i < ;++i)
  75. if (next[now][i] == -) next[now][i] = next[fail[now]][i];
  76. else fail[next[now][i]] = next[fail[now]][i],team.push(next[now][i]);
  77. }
  78. }
  79.  
  80. inline void ready()
  81. {
  82. vector <int> son[maxm]; queue <int> team; int i,now,v,nn;
  83. for (i = ;i < L;++i) if (fail[i] != i) son[fail[i]].push_back(i);
  84. team.push(root);
  85. while (!team.empty())
  86. {
  87. now = team.front(); team.pop();
  88. nn = son[now].size();
  89. for (i = ;i < nn;++i)
  90. {
  91. v = son[now][i];
  92. if (end[now]) end[v] = true;
  93. team.push(v);
  94. }
  95. }
  96. }
  97.  
  98. inline void make()
  99. {
  100. int i,j; s.n = s.m = L;
  101. for (i = ;i < L;++i)
  102. {
  103. if (end[i]) continue;
  104. for (j = ;j < ;++j)
  105. if (!end[next[i][j]]) s.a[i+][next[i][j]+]++;
  106. }
  107. }
  108. }ac;
  109.  
  110. int main()
  111. {
  112. freopen("1009.in","r",stdin);
  113. freopen("1009.out","w",stdout);
  114. scanf("%d %d %d\n",&n,&m,&rhl);
  115. ac.init();
  116. scanf("%s",buf); ac.insert();
  117. ac.build(); ac.ready(); ac.make();
  118. s = s.quick(s,n); s.calc();
  119. printf("%d",ans);
  120. fclose(stdin); fclose(stdout);
  121. return ;
  122. }

BZOJ 1009 GT考试的更多相关文章

  1. BZOJ 1009 GT考试(ac自动机+矩阵DP)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1009 题意:给定一个长度为m的串s.有多少种长度为n的串不包含s? 思路:(1)将s插入 ...

  2. BZOJ 1009 GT考试 (AC自动机 + 矩阵乘法加速dp)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1009 题意: 准考证号为\(n\)位数\(X_1X_2....X_n(0<=X_ ...

  3. bzoj 做起走 -- bzoj 1009 GT 考试

    现在每次做一道bzoj上的题,整个人都感觉升华了... 先是在网上各种搜题解.要么只有代码,要么有点讲解看不懂,对于从来没有耐心看完别人代码的我,只能一篇一篇的翻..然后终于在某2011级同学的某段话 ...

  4. [BZOJ 1009] [HNOI2008] GT考试 【AC自动机 + 矩阵乘法优化DP】

    题目链接:BZOJ - 1009 题目分析 题目要求求出不包含给定字符串的长度为 n 的字符串的数量. 既然这样,应该就是 KMP + DP ,用 f[i][j] 表示长度为 i ,匹配到模式串第 j ...

  5. BZOJ 1009: [HNOI2008]GT考试( dp + 矩阵快速幂 + kmp )

    写了一个早上...就因为把长度为m的也算进去了... dp(i, j)表示准考证号前i个字符匹配了不吉利数字前j个的方案数. kmp预处理, 然后对于j进行枚举, 对数字0~9也枚举算出f(i, j) ...

  6. BZOJ 1009 HNOI 2008 GT考试 递推+矩乘

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3679  Solved: 2254[Submit][Statu ...

  7. BZOJ 1009 [HNOI2008]GT考试 (KMP + 矩阵快速幂)

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4266  Solved: 2616[Submit][Statu ...

  8. bzoj 1009: [HNOI2008]GT考试 -- KMP+矩阵

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MB Description 阿申准备报名参加GT考试,准考证号为N位数X1X2.. ...

  9. GT考试(bzoj 1009)

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

随机推荐

  1. 尝鲜basic开发android

    做过android开发的同学都知道,很大精力都需要去面对界面编程,这个是非常没效率非常痛苦的一件事.偶然得知basic老树发新芽,居然还可以做android开发,决定试试效果如何. 首先上:http: ...

  2. [RxJS] Transformation operator: scan

    All of the combination operators take two or more observables as input. These operators may also be ...

  3. [RxJS] Filtering operators: skipWhile and skipUntil

    After takeUntil() and takeWhile() function, let's have a look on skipWhile() and skilUntil() functio ...

  4. Web classPath

    classpath,看名字,类路径,这样比如,对于java程序,就是告诉java程序哪里去找类.(java虚拟机都是通过类装载器的)想myeclipse中struts,spring,hibernate ...

  5. android开发之Notification学习笔记

    今天总结了一下Notification的使用,与大家分享一下. MainActivity.java: 本文参考:http://www.jb51.net/article/36567.htm,http:/ ...

  6. eclipse4.3 kepler中安装maven

    1.软件准备 a:Eclipse 4.3 http://www.eclipse.org/downloads/ b:maven http://maven.apache.org/download.cgi ...

  7. Junit简介和常用API

    测试几个的概念 白盒测试——把测试对象看作一个打开的盒子,程序内部的逻辑结构和其他信息对测试人员是公开的. 回归测试——软件或环境的修复或更正后的“再测试”,自动测试工具对这类测试尤其有用. 单元测试 ...

  8. ZOJ 3905 Cake(贪心+dp)

    动态规划题:dp[i][j]表示有i个Cake,给了Alice j个,先按照b排序,这样的话,能保证每次都能成功给Alice Cake,因为b从大到小排序,所以Alice选了j个之后,Bob最少选了j ...

  9. gamit10.6问题汇总

    1.在处理精密星历时,提示:old version of file not supported (name svnav.dat) 解决办法:在gamit10.5中不会出现这个问题,10.6中的官方文档 ...

  10. SQLite 入门教程(四)增删改查,有讲究

    增删改查操作,其中增删改操作被称为数据操作语言 DML,相对来说简单一点. 查操作相对来说复杂一点,涉及到很多子句,所以这篇先讲增删改操作,以例子为主,后面再讲查操作. 一.插入数据 INSERT I ...