"BZOJ-1009" GT test kmp+dp+ Matrix multiplication + fast Power

Source: Internet
Author: User
Tags mul

1009: [HNOI2008]GT exam Time limit:1 Sec Memory limit:162 MB
submit:2745 solved:1694
[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 think
0

Input

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

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 OutputBayiHintsourcesolution

This is a very good question.

Start looking at the range, $10^{9}$ obviously O (n) can not do ah, but also like the digital DP, so it must be optimized to optimize to the O (n) only the moment multiplication fast Power optimization dp

Actually and the digital DP very exhausted, F[I][J] represents the number of bits I, and finally matched the number of J-bit scheme, so the answer is obviously $\sum_{i=1}^{n}f[n][i]$

Consider the next array of KMP, sort the discussion, get on the matrix, and then quickly power up the mess.

Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespacestd;intn,m,p,next[ the],an;Chars[ the];structmatrixnode{intda[ -][ -]; Matrixnode () {memset (DA,0,sizeof(DA));}} A    Matrixnode Mul (Matrixnode a,matrixnode B) {Matrixnode C;  for(intI=0; i<m; i++)         for(intj=0; j<m; J + +)             for(intk=0; k<m; k++) C.da[i][j]= (C.da[i][j]+a.da[i][k]*b.da[k][j])%p; returnC;} Matrixnode Pow (Matrixnode A,intx)    {Matrixnode re;  for(intI=0; i<m; i++) re.da[i][i]=1;  for(intI=x; I i>>=1, a=Mul (a,a))if(i&1) re=Mul (re,a); returnre;}voidkmp_prework () { for(intj=0, i=2; i<=m; i++)        {             while(J && s[i]!=s[j+1]) j=Next[j]; if(s[j+1]==s[i]) J + +; next[i]=J; }     for(intI=0; i<m; i++)         for(intx,j=0; j<Ten; J + +) {x=i;  while(x && s[x+1]-'0'!=J) x=Next[x]; if(j==s[x+1]-'0') a.da[i][x+1]++;Elsea.da[i][0]++; }}intMain () {scanf ("%d%d%d\n", &n,&m,&p); scanf"%s", s+1);    Kmp_prework (); Matrixnode ans; Ans=Pow (a,n);  for(intI=0; i<m; i++) an= (an+ans.da[0][i])%p; printf ("%d\n", an); return 0;}

Matrixnode write why so long, make the code wind ugly dead

"BZOJ-1009" GT test kmp+dp+ Matrix multiplication + fast Power

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.