Question: bzoj 1009 hnoi2008 gt kmp + Matrix

Source: Internet
Author: User

Original question description:

A Shen is going to sign up for the GT test. the admission ticket number is n digits x1x2... Xn (0 <= xi <= 9). He does not want an unlucky number to appear on the admission ticket number. His unlucky math a1a2... am (0 <= ai & lt; = 9) has m-bit, does not appear refers to x1x2... there is not exactly one section in XN that is equal to a1a2... am. a1 and X1 can be 0

Analysis:

Sputation: I almost fell victim to the details of this question.

At first, I thought about DP, but the status transfer was disgusting.

Let's change the way we use KMP to construct an automatic machine of.

Then this question is converted into the number of paths that run and run on the automatic machine, and n edges are not run to the final state (AM.

In this way, we will transform this question into a classic problem: On a directed graph, the number of paths passing through n edges from S to T.

You can solve the problem by using the matrix's quick power.

If you do not have a classic problem, please refer to the following explanation:

/*

We can solve this problem using DP. The equation is:

DP [I] [J] [k] = DP [I] [p] [k-1] * DP [p] [J] [k-1];

Then, apparently, we can use the scrolling array.

DP [I] [J] = DP [I] [p] * DP [p] [J];

Then, what did you find?

Isn't this matrix multiplication!

*/

Specific implementation process:

1. Obtain the next array of.

2. Construct the matrix M: If I can be transferred to J, M [I] [J] ++;

(Because am is the final state, it can be optimized without the am edge)

3. c = m ^ N;

4. The answer is for (INT I = 0; I <m; I ++) ans + = C [0] [I];

Accode:

# Include <cstdio> # include <cstring> using namespace STD; int n, m, K; const int maxm = 30; struct matrix {int A [maxm] [maxm], n; matrix (int n, int X): n (n) {for (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) A [I] [J] = I = J? X: 0;} Matrix Operator * (const Matrix & B) {matrix C (n, 0); For (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) for (int K = 0; k <n; k ++) (C. A [I] [J] + = (A [I] [k] * B. A [k] [J]) % K) % = K; return C ;}}; matrix pow_mod (Matrix & A, int B) {matrix C (. n, 1); For (; B; B >>= 1) {If (B & 1) C = C * A; A = A * A;} return C ;} int A [maxm], F [maxm]; matrix m (maxm, 0); void getnext () {for (INT I = 1; I <m; I ++) {int J = f [I]; while (J & A [I]! = A [J]) J = f [J]; F [I + 1] = A [I] = A [J]? J + 1: 0 ;}} char STR [maxm]; int main () {scanf ("% d \ n % s", & N, & M, & K, STR); For (INT I = 0; I <m; I ++) A [I] = STR [I]-'0'; getnext (); m. N = m; For (INT I = 0; I <m; I ++) for (Int J = 0; j <10; j ++) {int K = I; if (A [I] = J) M. A [I] [I + 1] ++; else {While (K & A [k]! = J) k = f [k]; if (a [k] = J) K ++; M. A [I] [k] ++ ;}} matrix C = pow_mod (m, n); int ans = 0; For (INT I = 0; I <m; I ++) (ANS + = C. A [0] [I]) % = K; printf ("% d \ n", ANS); Return 0 ;}

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.