BZOJ1009 HNOI2008 GT考试

本文介绍了一种用于生成不包含不吉利数字的GT考试准考证号的算法,通过KMP算法优化DP过程,实现高效求解。适用于准考证号长度范围广泛,不吉利数字个数有限的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1009: [HNOI2008]GT考试

Time Limit: 1 Sec   Memory Limit: 162 MB
Submit: 2269   Solved: 1384
[ 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位的数。 100%数据N<=10^9,M<=20,K<=1000 40%数据N<=1000 10%数据N<=6

Output

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

Sample Input

4 3 100
111

Sample Output

81

F[i][j]表示当前为第i位匹配了j位且前i位未出现匹配的考号的方案数。因为F[i]只与F[i - 1]有关所以考虑用滚动数组。又发现该DP方程可以写成一次齐次递推式,所以可以用矩阵快速幂加速。
DP方程由KMP的Next数组得到。
代码如下:
/**************************************************************
    Problem: 1009
    User: duyixian
    Language: C++
    Result: Accepted
    Time:60 ms
    Memory:1292 kb
****************************************************************/
 
/* 
* @Author: duyixian
* @Date:   2015-09-10 11:16:06
* @Last Modified by:   duyixian
* @Last Modified time: 2015-09-15 17:13:20
*/
 
#include "cstdio"
#include "cstdlib"
#include "iostream"
#include "algorithm"
#include "cstring"
#include "queue"
 
using namespace std;
 
#define MAX_SIZE 50
#define INF 0x3F3F3F3F
#define Eps
#define Mod K
 
inline int Get_Int()
{
    int Num = 0, Flag = 1;
    char ch;
    do
    {
        ch = getchar();
        if(ch == '-')
            Flag *= -1;
    }
    while(ch < '0' || ch > '9');
    do
    {
        Num = Num * 10 + ch - '0';
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9');
    return Num * Flag;
}
 
int Next[MAX_SIZE], X[MAX_SIZE];
int N, K, M, Ans;
 
struct Matrix
{
    int A[MAX_SIZE][MAX_SIZE];
    int n, m;
 
    inline Matrix operator * (Matrix const &a) const
    {
        Matrix temp;
        memset(&temp, 0, sizeof(Matrix));
        temp.n = n;
        temp.m = a.m;
        for(int i = 0; i <= temp.n; ++i)
            for(int j = 0; j <= temp.m; ++j)
                for(int k = 0; k <= m; ++k)
                    temp.A[i][j] = (temp.A[i][j] + A[i][k] * a.A[k][j]) % Mod;
        return temp;
    }
 
    inline void operator *= (Matrix const &a)
    {
        *this = (*this) * a;
    }
 
    inline Matrix operator ^ (const int k)
    {
        Matrix ans, K1;
        memset(&ans, 0, sizeof(Matrix));
        K1 = *this;
        ans.n = ans.m = n;
        for(int i = 0; i <= n; ++i)
            ans.A[i][i] = 1;
        int temp = k;
        while(temp)
        {
            if(temp & 1)
                ans *= K1;
            temp >>= 1;
            K1 *= K1;
        }
        return ans;
    }
}a, DP;
 
int main()
{
    cin >> N >> M >> K;
    char ch = getchar();
    while(ch < '0' || ch > '9')
        ch = getchar();
    for(int i = 1; i <= M; ++i)
        X[i] = ch - '0', ch = getchar();
    X[M + 1] = 10;
    int temp = 0;
    for(int i = 2; i <= M; ++i)
    {
        while(temp && X[i] != X[temp + 1])
            temp = Next[temp];
        if(X[i] == X[temp + 1])
            Next[i] = ++temp;
    }
    a.n = a.m = M - 1;
    for(int i = 0; i < M; ++i)
    {
        for(int j = 0; j < 10; ++j)
        {
            int temp = i;
            while(temp && X[temp + 1] != j)
                temp = Next[temp];
            if(X[temp + 1] == j)
                ++temp;
            ++a.A[i][temp];
        }
    }
    DP.A[0][0] = 1;
    DP.n = 0;
    DP.m = M - 1;
    DP *= (a ^ N);
    for(int i = 0; i < M; ++i)
        Ans = (Ans + DP.A[0][i]) % Mod;
    cout << Ans << endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值