题目链接: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. Ubuntu Anaconda3 环境下安装caffe

    安装Python环境 本人环境为Anaconda3 ,可参照 https://blog.csdn.net/ctwy291314/article/details/86571198 完成安装Python2 ...

  2. Docker实战部署应用——MySQL5.7

    MySQL 部署 拉取MySQL镜像 拉取命令: docker pull mysql:5.7 查看镜像 docker images 创建 MySQL 容器 docker run -id --name= ...

  3. 实现 unity MonoBehaviour API5.4 的消息

      顺序(第一次执行.忽略循环) 方法 说明 Editor 1 void Reset() 重置为默认值 ------------------------------------------------ ...

  4. 02tensorflow非线性回归以及分类的简单实用,softmax介绍

    import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # 使用numpy生成200个随机点 x_data ...

  5. php内置函数分析之ucwords()

    PHP_FUNCTION(ucwords) { zend_string *str; char *delims = " \t\r\n\f\v"; register char *r, ...

  6. django之创建子应用

    一:子应用 Django的视图编写是放在子应用中的.类似于flask中的视图. 二:创建子应用 例如:在刚才的dj_study项目中,创建一个名字为user的子应用(目录):注意是第一级的dj_stu ...

  7. count(*),count(1),count(列名)的区别

    count(*)和count(1)无任何差别,永远优于count其他字段只要存在普通索引,count就会使用普通索引,只存在主键时,count(*)和或count(1)会使用主键索引 count(a) ...

  8. django权限之二级菜单

    遗漏知识点 1.构建表结构时,谁被关联谁就是主表,在层级删除的时候,删除子表的时候,主表不会被删除,反之删除主表的话,字表也会被删除, 使用related_name=None   反向查询,起名用的 ...

  9. windows openssh安装

    下载地址:https://github.com/PowerShell/Win32-OpenSSH/releases 解压好后打开目录,执行以下命令: powershell.exe -Execution ...

  10. 【leetcode】1090. Largest Values From Labels

    题目如下: We have a set of items: the i-th item has value values[i] and label labels[i]. Then, we choose ...