Bzoj 1009: [HNOI2008]GT Exam (DP + matrix fast Power + KMP)

Source: Internet
Author: User

Wrote a morning ... Because the length of the M is also counted in the ...

DP (i, j) indicates that the ticket number before I character matches the number of scenarios for the first J of the unlucky number. KMP preprocessing, and then for J enumeration, the number 0~9 is also enumerated to calculate that f (i, j) represents DP (X-1, J) Contribution to DP (x, i). Then use the matrix to quickly power. Time complexity O (M3Logn + m)

-------------------------------------------------------------------

#include <bits/stdc++.h>using namespace std;const int MAXN =;int FAIL[MAXN], S[MAXN], N, M, MOD; struct Matrix {int n, m;int A[MAXN][MAXN];matrix (int _n = 0, int _m = 0): N (_n), M (_m) {memset (A, 0, sizeof a);}void Unit () {for (int i = 0; i < n; i++)A[i][i] = 1;}matrix operator * (Matrix O) {matrix ret (n, O.M);for (int i = 0; i < n; i++)for (int k = 0; k < m; k++)For (int j = 0; J < o.m; J + +)Ret.a[i][j] = (Ret.a[i][j] + a[i][k] * o.a[k][j])% MOD;return ret;}matrix operator = (Matrix O) {for (int i = 0; i < n; i++)For (int j = 0; J < N; j + +)A[i][j] = o.a[i][j];return *this;}matrix operator ^ (int k) {matrix ret (n, m), t = *this; Ret.unit ();For (; k; k >>= 1) {if (K & 1) ret = ret * t;t = t * t;}return ret;}};void Kmp () {fail[0] = fail[1] = 0;for (int i = 1; i < M; i++) {int p = fail[i];while (P && s[i] = s[p]) p = fail[p];fail[i + 1] = s[i] = = s[p]? p + 1:0;}}int main () {scanf ("%d%d%d", &n, &m, &mod);for (int i = 0; i < M; i++) {char C = getchar ();For (;!isdigit (c); c = GetChar ());S[i] = C-' 0 ';}KMP ();Matrix Q (M, m);for (int i = 0; i < M; i++)For (int j = 0; J <; J + +) {int p = i;while (P && s[p]! = j) p = fail[p];if (s[p] = = j) p++;q.a[p][i]++;}matrix ans (M, 1);ans.a[0][0] = 1;ans = (Q ^ N) * ans;int tot = 0;for (int i = 0; i < M; i++)if (tot + = ans.a[i][0]) >= mod) tot-= mod;printf ("%d\n", tot);return 0;}

-------------------------------------------------------------------  

1009: [HNOI2008]GT exam Time limit:1 Sec Memory limit:162 MB
submit:2236 solved:1368
[Submit] [Status] [Discuss] Description

Ashen ready to enroll in the GT exam, the ticket number is n-digit x1x2 .... Xn (0<=xi<=9), he does not want to appear on the ticket number of unlucky figures. His unlucky math a1a2 ... AM (0<=ai<=9) has M-bit, does not appear to refer to x1x2 ... The xn does not have exactly a paragraph equal to A1A2 ... Am. A1 and X1 can be 0

Input

The first line enters N,m,k. The next line is to enter the number of M bits. 100% data n<=10^9,m<=20,k<=1000 40% data n<=1000 10% data n<=6

Output

Ashen want to know how many kinds of numbers do not appear unlucky numbers, the result of output modulus K redundancy.

Sample Input4 3 100
111Sample OutputBayiHINT

Source

Bzoj 1009: [HNOI2008]GT Exam (DP + matrix fast Power + KMP)

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.