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_ ...
随机推荐
- Python Env
简介: 记录 CentOS 6.x Python 环境的安装步骤. 一.安装依赖包 shell > yum -y install epel-release shell > yum -y i ...
- gradle 错误
A problem occurred evaluating root project 'clouddriver'.> Failed to apply plugin [class 'com.net ...
- Ansible Playbook Variables
虽然自动化存在使得更容易使事情重复,但所有的系统可能不完全一样. 在某些系统上,您可能需要设置一些与其他操作略有不同的行为或配置. 此外,一些观察到的远程系统的行为或状态可能需要影响如何配置这些系统. ...
- R画散点图、线型图、箱型图、直方图基本知识
1.导入数据 2.散点图 plot(iris[,1]~iris[,4],xlab='Length',ylab='Width',col='red',main='Length VS Width')
- List<?>和List<T>的区别
是java泛型的两种用法:List<T>是泛型方法,List<?>是限制通配符 List<T>一般有两种用途:1.定义一个通用的泛型方法.伪代码: public i ...
- mongodb根据子项中的指标查找最小或最大值
假设students集合中有这样的数据: { "_id" : 1, "name" : "Aurelia Menendez", "s ...
- 百度BAE的一些使用心得
休眠会释放单元资源停止收费,那就不用被百度收费了
- 取消svn add
svn commit之前,add的东西都可以取消. 通过先执行svn cleanup,再执行svn revert --recursive example_folder.
- C++ define与const
C++中不但可以用define定义常量还可以用const定义常量,它们的区别如下: 用#define MAX 255定义的常量是没有类型的,所给出的是一个立即数,编译器只是把所定义的常量值与所定义的常 ...
- [leetcode]269. Alien Dictionary外星字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...