题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1009

显而易见的动态规划加矩阵快速幂,不过转移方程不怎么好想,dp[i][j]表示长度为i的准考证号后j位与不吉利数字的前j位相同的方案数。则:

转移方程为$dp[i][j]=\sum_{k=0}^{m-1}dp[i-1][k]*g[k][j]$

答案为:$ans=\sum_{i=0}^{m}dp[n][i]$

g[i][j]表示长度为i的后缀变成长度为j的后缀的方案数。

而g数组可以用kmp预处理出来

附上洛谷40分不用矩阵优化的代码

   #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 6e6 + ;
ll Next[];
ll g[][];
ll dp[maxn][];
char s[];
void getN(int n) {
Next[] = -;
int i = , j = -;
while (i < n) {
if (j == - || s[i] == s[j])
Next[++i] = ++j;
else
j = Next[j];
}
}
int main() {
ll n, m, mod;
scanf("%lld%lld%lld", &n, &m, &mod);
scanf("%s", s);
getN(m);
Next[] = ;
for (int i = ; i < m; i++) {
for (int j = ''; j <= ''; j++) {
int t = i;
while (t&& s[t] != j)
t = Next[t];
if (s[t] == j)
t++;
g[i][t]++;
}
}
dp[][] = ;
for (int i = ; i <= n; i++) {
for (int j = ; j < m; j++) {
for (int k = ; k < m; k++) {
dp[i][j] = (dp[i][j] + dp[i - ][k] * g[k][j]) % mod;
}
}
}
ll ans = ;
for (int i = ; i < m; i++)
ans = (ans + dp[n][i]) % mod;
printf("%lld\n", ans);
}

以及正解

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 6e6 + ;
ll Next[];
ll n, m, mod;
ll dp[][];
char s[];
void getN(int n) {
Next[] = -;
int i = , j = -;
while (i < n) {
if (j == - || s[i] == s[j])
Next[++i] = ++j;
else
j = Next[j];
}
}
struct matrix {
ll cnt[][];
matrix() { memset(cnt, , sizeof(cnt)); }
matrix operator *(const matrix a)const {
matrix ans;
for (int i = ; i <= m; i++) {
for (int j = ; j <= m; j++) {
ans.cnt[i][j] = ;
for (int k = ; k <= m; k++)
ans.cnt[i][j] = (ans.cnt[i][j] + cnt[i][k] * a.cnt[k][j]) % mod;
}
}
return ans;
}
};
matrix powM(matrix a, int b) {
matrix ans = matrix();
for (int i = ; i <= m; i++)
ans.cnt[i][i] = ;
while (b) {
if (b & )
ans = ans * a;
a = a * a;
b /= ;
}
return ans;
}
int main() {
matrix g, ans, dp = matrix();
scanf("%lld%lld%lld", &n, &m, &mod);
scanf("%s", s);
getN(m);
Next[] = ;
memset(g.cnt, , sizeof(g.cnt));
for (int i = ; i < m; i++) {
for (int j = ''; j <= ''; j++) {
int t = i;
while (t&& s[t] != j)
t = Next[t];
if (s[t] == j)
t++;
g.cnt[i][t]++;
}
}
dp.cnt[][] = ;
ans = powM(g, n);
ans = dp * ans;
ll sum = ;
for (int i = ; i < m; i++)
sum = (sum + ans.cnt[][i]) % mod;
printf("%lld\n", sum);
}

[Bzoj1009][HNOI2008]GT考试(动态规划)的更多相关文章

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

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

  2. BZOJ1009 [HNOI2008]GT考试 矩阵

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

  3. bzoj1009 [HNOI2008] GT考试 矩阵乘法+dp+kmp

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

  4. [Bzoj1009][HNOI2008]GT考试(KMP)(矩乘优化DP)

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

  5. bzoj1009: [HNOI2008]GT考试(kmp+矩阵乘法)

    1009: [HNOI2008]GT考试 题目:传送门 题解: 看这第一眼是不是瞬间想起组合数学??? 没错...这样想你就GG了! 其实这是一道稍有隐藏的矩阵乘法,好题! 首先我们可以简化一下题意: ...

  6. [bzoj1009](HNOI2008)GT考试 (kmp+矩阵快速幂加速递推)

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

  7. [bzoj1009][HNOI2008]GT考试

    Description 阿申准备报名参加考试,准考证号为位数,他不希望准考证号上出现不吉利的数字. 他的不吉利数学有位,不出现是指中没有恰好一段等于. 可以为. Input 第一行输入.接下来一行输入 ...

  8. [BZOJ1009] [HNOI2008] GT考试 (KMP & dp & 矩阵乘法)

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

  9. bzoj1009: [HNOI2008]GT考试 ac自动机+矩阵快速幂

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

随机推荐

  1. CentOS7 设置电源选项,待机、睡眠、挂起

    设置装有 CentOS7 的笔记本合盖后黑屏进入睡眠模式 systemd 能够处理某些电源相关的 ACPI事件,你可以通过从 /etc/systemd/logind.conf 以下选项进行配置: Ha ...

  2. hdu4348 To the moon (可持久化线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348 题目大意:给定含有n个数的序列,有以下四种操作 1.C l r d:表示对区间[l,r]中的数加 ...

  3. java判断回文数

  4. D0g3_Trash_Pwn_Writeup

    Trash Pwn 下载文件 1 首先使用checksec查看有什么保护 可以发现,有canary保护(Stack),堆栈不可执行(NX),地址随机化没有开启(PIE) 2 使用IDA打开看看 mai ...

  5. [POJ1772] Substract

    问题描述 We are given a sequence of N positive integers a = [a1, a2, ..., aN] on which we can perform co ...

  6. 一波儿networkx 读写edgelist,给节点加attribute的操作

    一波儿networkx 读写edgelist,给节点加attribute的操作 read more: nx official: Reading and writing graphs import nu ...

  7. Python3解leetcode First Bad Version

    问题描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...

  8. Word文档粘贴到帝国CMS

    很多时候我们用一些管理系统的时候,发布新闻.公告等文字类信息时,希望能很快的将word里面的内容直接粘贴到富文本编辑器里面,然后发布出来.减少排版复杂的工作量. 下面是借用百度doc 来快速实现这个w ...

  9. Apache服务器出现Forbidden 403错误提示的解决方法

    默认web目录/var/www/html 改成 /data/www出现403问题解决: vim /etc/apache2/apache2.conf <Directory /data/www/&g ...

  10. 2018 CCPC 吉林站 H Lovers || HDU 6562 (线段树哦)

    http://acm.hdu.edu.cn/showproblem.php?pid=6562 题意: q次操作 1.将第l~r个数的左边和和右边都加上一个数d, 使得这个数变成 dsiddsid的形式 ...