BZOJ 1009. [HNOI2008] GT test

Clearly there is a DP equation $ dp [i] [j] $ $ I $ denotes the first bit end has a matching number of programs $ $ J bits.

Violence then on the next enumerate what a place to see the place again and again after the match how much.

Here can be introduced a number of $ f [i] [j] $ array represents the current match is never lucky number $ I $ bits, plus a $ J $ character matches the program bits.

This part can be obtained by kmp.

Then the transfer equation dp is the $ dp [i] [j] = \ sum dp [i - 1] [k] \ times f [k] [j] $

The answer is $ \ sum_ {i = 0} ^ {m - 1} dp [n] [i] $

#include <bits/stdc++.h>

const int N = 22;
int MOD, n, m;
char s[N];
int ne[N];

struct Mat {
    int mat[25][25];
    Mat() {
        memset(mat, 0, sizeof mat);
    }
    Mat operator * (const Mat &rhs) const {
        Mat c;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                for (int k = 0; k < n; k++)
                    (c.mat[i][j] += mat[i][k] * rhs.mat[k][j]) %= MOD;
        return c;
    }
};

Mat qp(Mat ans, Mat a, int b) {
    while (b) {
        if (b & 1) ans = ans * a;
        a = a * a;
        b >>= 1;
    }
    return ans;
}

void kmp() {
    int i = 0, j = ne[0] = -1;
    while (i < n) {
        while (j != -1 && s[i] != s[j]) j = ne[j];
        ne[++i] = ++j;
    }
}

int main() {
    scanf("%d%d%d", &m, &n, &MOD);
    scanf("%s", s);
    kmp();
    Mat b;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 10; j++) {
            int k = i;
            while (k != -1 && s[k] != j + '0') k = ne[k];
            k++;
            if (k < n) b.mat[i][k]++;
        }
    }
    Mat a;
    a.mat[0][0] = 1;
    a = qp(a, b, m);
    int ans = 0;
    for (int i = 0; i < n; i++)
        ans += a.mat[0][i];
    ans %= MOD;
    printf("%d\n", ans);
    return 0;
}
View Code

Guess you like

Origin www.cnblogs.com/Mrzdtz220/p/12230564.html