"Bzoj" "1009" "HNOI2008" GT exam

Source: Internet
Author: User

dp/kmp/matrix multiplication

Good God's problem ... Kneel down on your knees

What the hell is $n \leq 10^9$ ... Let's just not think about the ghost animal thing. >_>

Using a similar digital DP approach, we can think of a DP equation: $f [i][j]$ represents the first I-digit number, its last J-bit matches the number of unlucky strings, apparently $ans=\sum_{i=0}^x f[n][i]$

Then there is the problem of the transfer = = then still follow the idea of digital DP (in fact, it is hard to pull it ...). How to understand can be, the focus is to understand the transfer equation) can be thought: transfer from I to i+1, there are 10 kinds of scenarios, one of which will make the matching length +1, that is $f[i+1][j+1]+=f[i][j]$ so other solutions? Will not make the matching length of 0, think of this you probably already thought of, right! It's kmp!. The fail pointer when the string matches! Then the way of transfer is these three species = =:1. match length to 0;2. Match length +1;3. Match length to fail[j]+1.

Then the transfer method has been determined ~ then it is time to consider the $n\leq 10^9$ this egg pain range ...

Obviously this range O (n) is not possible = = must be accelerated! So let's look at this shift: we can think of $f_i$ as a vector: $$\begin{bmatrix} f_0 f_1 \cdots f_{m-1} \end{bmatrix} (i.e. 0\leq J\leq m-1) $$ from $f_i$ to $f_ {i+1}$, in fact, can be represented by a matrix: $$ \begin{bmatrix} f_0& f_1 &\cdots &f_{m-1} \end{bmatrix}_{i} * \begin{bmatrix} a_{0 ,0}& a_{0,1}& \cdots &a_{0,m-1} \ a_{1,0}& a_{1,1}& \cdots &a_{1,m-1} \ \ddots& \ddots& \vdots &\ddots \ a_{m-1,0}& a_{m-1,1}& \cdots &a_{m-1,m-1} \end{bmatrix} = \begin{bmatrix} f_0 &f_1 &\cdots &f_{m-1} \end{bmatrix}_{i+1} $$

Well, the expression on the left is $f[i][j]$, the right side is $f[i+1][j]$, so what's a matrix? This is a transfer matrix: $a [i][j]$ represents the number of scenarios from matching the I-character transfer to the J-character matching! Obviously when the $j>0$ F[i][j] can only be 0 or 1, here may need to understand carefully, anyway $f[i+1]$ and $f[i]$ is linearly related! So we can accelerate with matrix multiplication directly!

Well, I wrote this question because there is a "matching length of 0" in this state ... And then kmp a long time not to write ... Resolute kneeling Ah, this problem seems to need to change a little on the original KMP, because I kmp understand is not very good so the egg hurts for a long time ... Finally, I saw the hzwer.

1 /**************************************************************2 problem:10093 User:tunix4 language:c++5 result:accepted6 time:56 Ms7 memory:812 KB8 ****************************************************************/9  Ten //Bzoj 1009 One#include <cstdio> A #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) - Const intn= -, inf=~0u>>2; the /******************tamplate*********************/ - intn,m,p; - structmatrix{ -     intX[n][n]; +     int*operator[] (intA) {returnx[a];} -Matrix (intA=0){ + Rep (I,n) Rep (j,n) A             if(I==J) x[i][j]=A; at             Elsex[i][j]=0; -     } - }; -Matrixoperator*(Matrix A,matrix b) { - Matrix C; - Rep (I,m) Rep (j,m) Rep (k,m) inC[i][j]= (C[i][j]+a[i][k]*b[k][j])%P; -     returnC; to } +Matrix Pow (Matrix A,intb) { -Matrix R (1); the      for(; b;b>>=1, a=a*a)if(b&1) r=r*A; *     returnR; $ }Panax Notoginseng /*******************matrix**********************/ - Chars[ -]; the Matrix f,a; + intnext[ -]; A voidKMP () { the     intj=0; +F (I,2, M) { -          while(J && s[i]!=s[j+1]) j=Next[j]; $         if(s[j+1]==s[i]) J + +; $next[i]=J; -     } - Rep (i,m) theRep (J,Ten){ -             intx=i;Wuyi              while(x && s[x+1]-'0'!=J) x=Next[x]; the             if(j==s[x+1]-'0') a[i][x+1]++; -             Elsea[i][0]++; Wu         } - } About intMain () { $scanf"%d%d%d",&n,&m,&P); -scanf"%s", s+1); - KMP (); -f=Pow (a,n); A     intans=0; +Rep (i,m) ans+=f[0][i],ans%=P; theprintf"%d\n", ans); -     return 0; $}
View Code 1009: [HNOI2008]GT exam Time limit:1 Sec Memory limit:162 MB
submit:2015 solved:1233
[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 OutputBayiHintsource [Submit] [Status] [Discuss]

"Bzoj" "1009" "HNOI2008" GT exam

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.