BZOJ 1009 [HNOI2008]GT考试 (KMP + 矩阵快速幂)
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
111
Sample Output
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 + 矩阵快速幂)的更多相关文章
- BZOJ 1009: [HNOI2008]GT考试( dp + 矩阵快速幂 + kmp )
写了一个早上...就因为把长度为m的也算进去了... dp(i, j)表示准考证号前i个字符匹配了不吉利数字前j个的方案数. kmp预处理, 然后对于j进行枚举, 对数字0~9也枚举算出f(i, j) ...
- [bzoj1009](HNOI2008)GT考试 (kmp+矩阵快速幂加速递推)
Description 阿 申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学 A1A2...Am(0&l ...
- bzoj 1009: [HNOI2008]GT考试 -- KMP+矩阵
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MB Description 阿申准备报名参加GT考试,准考证号为N位数X1X2.. ...
- 题解:BZOJ 1009 HNOI2008 GT考试 KMP + 矩阵
原题描述: 阿申准备报名参加GT考试,准考证号为N位数 X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学A1A2...Am(0<=Ai&a ...
- bzoj1009 [HNOI2008]GT考试——KMP+矩阵快速幂优化DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 字符串计数DP问题啊...连题解都看了好多好久才明白,别提自己想出来的蒟蒻我... 首 ...
- bzoj 1009 [HNOI2008]GT考试——kmp+矩阵优化dp
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 首先想到 确保模式串不出现 就是 确保每个位置的后缀不是该模式串. 为了dp,需要记录 ...
- BZOJ 1009 [HNOI2008]GT考试 (KMP+矩阵乘法)
---恢复内容开始--- 题目大意:给定一个由数字构成的字符串A(len<=20),让你选择一个长度为n(n是给定的)字符串X,一个合法的字符串X被定义为,字符串X中不存在任何一段子串与A完全相 ...
- [HNOI2008] GT考试(DP+矩阵快速幂+KMP)
题目链接:https://www.luogu.org/problemnew/show/P3193#sub 题目描述 阿申准备报名参加 GT 考试,准考证号为 N 位数 X1,X2…Xn(0 <= ...
- [HNOI2008][bzoj1009] GT考试 [KMP+矩阵快速幂]
题面 传送门 思路 首先,如果$n$和$m$没有那么大的话,有一个非常显然的dp做法: 设$dp[i][j]$表示长度为i的字符串,最后j个可以匹配模板串前j位的情况数 那么显然,答案就是$\sum_ ...
随机推荐
- H5-BLOB
BLOB 对象为h5的产物.普遍用于传输或者存储数据. <a 标签的新属性 download 表明 此a标签点击后,不是href跳转而是要下载.download的内容表示下载文件名.但是目前部分 ...
- ABAP-关于隐式与显式的DB Commit
转载:https://www.cnblogs.com/liaojunbo/archive/2011/07/11/2103491.html 1.显式的DB Commit 显式的DB Commit并没有对 ...
- oc 中的id类型与类型转换
id是oc语言中一个独特的数据类型.一种通用对象类型.可以转换为任何数据类型,即id类型的变量可以存放任何数据类型的对象. 使用示例: Animal * dog = [[Dog alloc]init] ...
- nginx的配置文件解析
worker_processes ;#工作进程的个数,一般与计算机的cpu核数一致 events { worker_connections ;#单个进程最大连接数(最大连接数=连接数*进程数) } h ...
- .net数据库连接防注入参数查询 命令执行 读取 备份 导出导入转化XML格式
ADO.NET是一组类库,让我们通过程序的方式访问数据库.SYSTEM.DATA这个类提供了统一的接口访问Oracle MSSQL Access.像SYSTEM.IO类操作文件一样. **connec ...
- html5中的SessionStorage 和localStorage
html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. sessionStorage用于本地存储一个会话(session)中的数据,这些数据只 ...
- 用JDBC如何调用存储过程
package com.huawei.interview.lym; import java.sql.CallableStatement; import java.sql.Connection; imp ...
- kubectl 获取信息
获取pod所在节点的ip kubectlget po tiller-deploy-8694f8fddc-c2rql -n kube-system -o jsonpath='{.status.hostI ...
- cubic与spline插值点处的区别
cubic与spline都是Matlab的三次样条插值法,但是它们在插值点处仍然有着很微妙的区别,这个区别说明不了两种方法的好坏,只能根据实际情况进行合理筛选.以一维插值为例: clc clear % ...
- Python bool() 函数
Python bool() 函数 Python 内置函数 描述 bool() 函数用于将给定参数转换为布尔类型,如果没有参数,返回 False. bool 是 int 的子类. 语法 以下是 boo ...