_bzoj1009 [HNOI2008]GT Exam "Matrix acceleration DP"

Source: Internet
Author: User
Tags mul

Portal: http://www.lydsy.com/JudgeOnline/problem.php?id=1009

A good question, so that f (i, j) to indicate that the test number matches to I bit, the post J bit is an unlucky string of the first J-bit, then for each state, is a linear combination of the previous state, so you can use the matrix to accelerate.

#include <cstdio> #include <cstring>const int maxn = 1000000005, Maxm = 25;int n, m, p, MTX[MAXM][MAXM], trie[m AXM][10], FAIL[MAXM], TRANS[MAXM][MAXM], TEM[MAXM][MAXM], Ans;char unf[maxm];inline void mul (int aa[maxm][maxm], int ss[  MAXM][MAXM]) {memset ((void*) TEM, 0, sizeof tem); for (int i = 0; i < m; ++i) {for (int j = 0; j < m; ++j) {for (int k = 0; K < M; ++K) {Tem[i][j] = (Tem[i][j] + aa[i][k] * ss[k][j])% p;}} memcpy ((void*) AA, (void*) TEM, sizeof tem);} inline void poww (int mi) {int i;for (i = n; mi >> I & 1 ^ 1; i); memcpy ((void*) trans, (void*) MTX, sizeof MTX); for (~i; i) {mul (trans, trans), if (Mi >> I & 1) {Mul (trans, MTX);}} int main (void) {//freopen ("In.txt", "R", stdin), scanf ("%d%d%d", &n, &m, &p), scanf ("%s", UNF + 1); for (int i = 0; I < m; ++i) {trie[i][unf[i + 1]-' 0 '] = i + 1;} for (int i = 1, i <= m; ++i) {for (int j = 0; J <; ++j) {if (Trie[i][j]) {Fail[trie[i][j]] = trie[fail[i]][j];} else {Trie[i][j] = Trie[fail[i]][j];}}} for (int i = 0, i < m; ++i) {for (int j = 0; J < ++j) {++mtx[i][trie[i][j]];}} POWW (n); for (int i = 0; i < m; ++i) {ans = (ans + trans[0][i])% p;} printf ("%d\n", ans); return 0;}

  

_bzoj1009 [HNOI2008]GT Exam "Matrix acceleration DP"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.