题目链接: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. Python Paramiko模块使用

    1 执行远程命令 #!/usr/bin/python import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_polic ...

  2. c# 反射获取属性值 TypeUtils

    using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...

  3. 去除重复嵌套的html标签函数

    去除重复嵌套的html标签 function strip_multi_tags($str, $tag = 'div'){ preg_match_all('/<'.$tag.'>|<\ ...

  4. [luogu] P3809 【模板】后缀排序 (SA)

    板子,照着题解打的倍增版. #include <iostream> #include <cstdio> #include <cstring> using names ...

  5. OC + RAC (八) 查看信号状态和跳过信号

    -(void)_test9{ /// RACCommand又叫命令 是用来收发数据的 监听按钮点击,网络请求.... RACCommand * command = [[RACCommand alloc ...

  6. 使用tinymce编辑器从word保持原格式复制粘贴的办法

    官网地址http://ueditor.baidu.com Git 地址 https://github.com/fex-team/ueditor 参考博客地址 http://blog.ncmem.com ...

  7. Xcode之增加环境变量(多种环境区分)

    序言: Xcode默认有DEBUG何RELEASE模式,如果我们在项目中想增加预发布环境或者再增加多个环境呢?如果在项目中用if else 弄个全局变量来控制,每次打包之前去手动修改,这样不仅繁琐,而 ...

  8. Gym - 101194H Great Cells

    Problem H. Great Cells 题目链接:https://codeforces.com/gym/101194/attachments Input file: Standard Input ...

  9. ProxyImpl 类

    package com.test.mvp.mvpdemo.mvp.v7.proxy; import com.test.mvp.mvpdemo.mvp.v7.basemvp.BasePresenter; ...

  10. Oracle命令行模式,批量执行SQL脚本

    由于项目不同,使用的数据库也不一样,通常MySQL 比较方便简介,相对而言Oracle比较繁琐一点,尤其是堡垒机的连接的时候, 通过堡垒机登陆,数据库服务器,通过下面的脚本执行进入到命令行模式执行SQ ...