1009: [HNOI2008]GT考试

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 4266  Solved: 2616
[Submit][Status][Discuss]

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位的数。 N<=10^9,M<=20,K<=1000

Output

  阿申想知道不出现不吉利数字的号码有多少种,输出模K取余的结果.

Sample Input

4 3 100
111

Sample Output

81

HINT

析:先用KMP把不能出现的串先匹配出来,然后再对每种状态进行计算,dp[i][j] 表示已经匹配到 i 并转移到 j 有多少种,但是时间复杂度太高,所以要用矩阵快速幂来进行优化。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-3;
const int maxn = 20 + 10;
const int maxm = 3e5 + 10;
const int mod = 100003;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} char s[maxn];
int f[maxn], K; struct Matrix{
int a[maxn][maxn];
int n;
Matrix(){ ms(a, 0); } void toOne(){ FOR(i, 0, n) a[i][i] = 1; }
Matrix operator * (const Matrix &rhs){
Matrix res;
res.n = n;
FOR(i, 0, n) FOR(j, 0, n)
for(int k = 0; k < n; ++k)
res.a[i][j] = (res.a[i][j] + a[i][k] * rhs.a[k][j]) % K;
return res;
}
}; Matrix fast_pow(Matrix a, int n){
Matrix res; res.n = a.n; res.toOne();
while(n){
if(n & 1) res = res * a;
n >>= 1;
a = a * a;
}
return res;
} int main(){
scanf("%d %d %d", &n, &m, &K);
scanf("%s", s);
f[0] = f[1] = 0;
for(int i = 1; i < m; ++i){
int j = f[i];
while(j && s[j] != s[i]) j = f[j];
f[i+1] = s[j] == s[i] ? j+1: 0;
}
Matrix x; x.n = m;
for(int i = 0; i < m; ++i){
for(int t = 0; t < 10; ++t){
int j = i;
while(j && s[j] - '0' != t) j = f[j];
if(s[j] - '0' == t) ++j;
++x.a[i][j];
}
}
Matrix ans = fast_pow(x, n);
int res = 0;
for(int i = 0; i < m; ++i) res = (res + ans.a[0][i]) % K;
printf("%d\n", res);
return 0;
}

  

BZOJ 1009 [HNOI2008]GT考试 (KMP + 矩阵快速幂)的更多相关文章

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

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

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

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

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

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

  4. 题解:BZOJ 1009 HNOI2008 GT考试 KMP + 矩阵

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

  5. bzoj1009 [HNOI2008]GT考试——KMP+矩阵快速幂优化DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 字符串计数DP问题啊...连题解都看了好多好久才明白,别提自己想出来的蒟蒻我... 首 ...

  6. bzoj 1009 [HNOI2008]GT考试——kmp+矩阵优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 首先想到 确保模式串不出现 就是 确保每个位置的后缀不是该模式串. 为了dp,需要记录 ...

  7. BZOJ 1009 [HNOI2008]GT考试 (KMP+矩阵乘法)

    ---恢复内容开始--- 题目大意:给定一个由数字构成的字符串A(len<=20),让你选择一个长度为n(n是给定的)字符串X,一个合法的字符串X被定义为,字符串X中不存在任何一段子串与A完全相 ...

  8. [HNOI2008] GT考试(DP+矩阵快速幂+KMP)

    题目链接:https://www.luogu.org/problemnew/show/P3193#sub 题目描述 阿申准备报名参加 GT 考试,准考证号为 N 位数 X1,X2…Xn(0 <= ...

  9. [HNOI2008][bzoj1009] GT考试 [KMP+矩阵快速幂]

    题面 传送门 思路 首先,如果$n$和$m$没有那么大的话,有一个非常显然的dp做法: 设$dp[i][j]$表示长度为i的字符串,最后j个可以匹配模板串前j位的情况数 那么显然,答案就是$\sum_ ...

随机推荐

  1. 2 python第三章文件操作

    1.三元运算 三元运算又称三目运算,是对简单的条件语句的简写,如: 简单条件语句: if 条件成立: val = 1 else: val = 2 改成三元运算: val = 1 if 条件成立 els ...

  2. vue深入了解组件——插槽

    一.插槽内容 Vue实现了一套内容分发的API,这套API基于当前的Web Components规范草案,将 <slot>  元素作为承载分发的内容的出口. 它允许你像这样合成组件: &l ...

  3. <!-- str.startsWith('胡') 检查一个 字符串中是否有某字符 返回true false -->& vh 属性

    1.<!-- str.startsWith('胡')  检查一个 字符串中是否有某字符 返回true false --> 2. vh 分享到选择其它项   复制本页链接 版本:CSS3 补 ...

  4. request error: Connection aborted.', error(113, 'No route to host')

    from: https://superuser.com/questions/720851/connection-refused-vs-no-route-to-host/720860 "Con ...

  5. RabbitMq install on Centos

    安装服务(root) erlang官方安装说明:https://www.erlang-solutions.com/resources/download.html step 1: 安装erlang的yu ...

  6. 趣味编程:CPS风格代码(C++11, C++14版)

    CPS风格代码(C++11版) #include <iostream> using namespace std; int add(int x, int y){return x + y;} ...

  7. 迷你MVVM框架 avalonjs 学习教程19、avalon历史回顾

    avalon最早发布于2012.09.15,当时还只是mass Framework的一个模块,当时为了解决视图与JS代码的分耦,参考knockout开发出来. 它的依赖收集机制,视图扫描,绑定的命名d ...

  8. C++ 0x 使用 shared_ptr 自动释放, 防止内存泄漏

    最近在研究 cocos2d-x 3.0 ,它在创建类的对象时比如 Layer 时, 并不是直接使用 new , 而是使用一个宏方法  CREATE_FUNC(MyLayer);. 这个宏就是自动的创建 ...

  9. mysql备份和还原命令

    备份和还原数据库都是在未登录的前提下进行命令操作的: 1.备份表: mysqldump -u root -p dbname table1 table2 > D:\sqlback.sql 2.备份 ...

  10. jenkins 自动构建gitlab项目

    安装的plugin: - kubernetes:1.7.1    - workflow-aggregator:2.5    - workflow-job:2.21    - credentials-b ...