WordPress金融网站/重庆镇海seo整站优化价格
GT考试
[Link]([题目详情 - HNOI2008]GT考试 - HydroOJ)
题意
阿申准备报名参加 GTGTGT 考试,准考证号为 nnn 位数 X1X2⋯XnX1X2⋯XnX1X2⋯Xn,他不希望准考证号上出现不吉利的数字。
他的不吉利数字 A1A2⋯AmA1A2⋯AmA1A2⋯Am 有mmm 位,不出现是指 X1X2⋯XnX1X2⋯XnX1X2⋯Xn 中没有恰好一段等于A1A2⋯AmA1A2⋯AmA1A2⋯Am,A1A1A1 和$ X1$ 可以为 000。
请输出不出现不吉利的数的号码有多少种?
题解
要统计方案数,考虑dpdpdp维护。
我们设f[i,j]:表示长度位i且后缀与不吉利数前缀最长匹配长度j的方案数f[i,j]:表示长度位i且后缀与不吉利数前缀最长匹配长度j的方案数f[i,j]:表示长度位i且后缀与不吉利数前缀最长匹配长度j的方案数。
状态转移时我们考虑当前状态能转移到哪些状态去,也就是第i+1i+1i+1位填0∼90\sim90∼9对下一个状态的影响。
状态转移方程:f[i+1][k]=f[i][0]∗a[0][k]+f[i][1]∗a[1][k]+...+f[i][m−1]∗a[m−1][k]f[i+1][k]=f[i][0]*a[0][k]+f[i][1]*a[1][k]+...+f[i][m-1]*a[m-1][k]f[i+1][k]=f[i][0]∗a[0][k]+f[i][1]∗a[1][k]+...+f[i][m−1]∗a[m−1][k]
因为不吉利数是已知的,因此后缀与前缀匹配的数字是什么也是已知的,一次每一种转移对应的系数a[j][k]a[j][k]a[j][k]也是可以求出来的,是个固定值,因此f[i]和f[i+1]f[i]和f[i + 1]f[i]和f[i+1]的关系是线性的,因此可以矩阵乘法来加速,即f[i+1]=f[i]∗Af[i+1]=f[i]*Af[i+1]=f[i]∗A。
转移矩阵AAA中的元素a[j][k]a[j][k]a[j][k]表示原来与不吉利数匹配长度由jjj到kkk的方案数。
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 25, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int a[N][N];
char str[N];
void mul(int c[][N], int a[][N], int b[][N]) {int tmp[N][N] = {0};for (int i = 0; i < m; i ++ )for (int j = 0; j < m; j ++ )for (int t = 0; t < m; t ++ )tmp[i][j] = (tmp[i][j] + a[i][t] * b[t][j]) % k;memcpy(c, tmp, sizeof tmp);
}
int qmi(int b) {int f[N][N] = {1};while (b) {if (b & 1) mul(f, f, a);b >>= 1;mul(a, a, a);}int res = 0;for (int i = 0; i < m; i ++ ) res = (res + f[0][i]) % k;return res;
}
int main() {ios::sync_with_stdio(false), cin.tie(0);cin >> n >> m >> k;cin >> str + 1;for (int i = 2, j = 0; i <= m; i ++ ) {while (j && str[j + 1] != str[i]) j = ne[j];if (str[j + 1] == str[i]) j ++;ne[i] = j;}for (int j = 0; j < m; j ++ )for (char c = '0'; c <= '9'; c ++ ) {int k = j;while (k && str[k + 1] != c) k = ne[k];if (str[k + 1] == c) k ++;if (k < m) a[j][k] ++;}cout << qmi(n) << endl;return 0;
}