"Matrix Multiply" "KMP" "Hnoi 2008" "Bzoj 1009" GT exam

Source: Internet
Author: User

1009: [HNOI2008]GT Exam
Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2230  Solved: 1364

Description

Ashen ready to enroll in the GT exam, the ticket number is n-digit x1x2....xn (0<=xi<=9), he does not want the ticket number on the appearance of unlucky numbers. 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 Input

4 3 100 111

Sample Output

81

Exercises

First think of DP, and then see the data range, so naturally think of matrix multiplication.
For the transfer location, you can use KMP to speed up (it is said to be violent, but Lcomyn, Rivendell and Fye are all written by AC automata. )
I do not know why the matrix is wrong to write some sadness. Under the guidance of the Great God wrote a strange recursive matrix multiplication.

Code:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <algorithm>using namespace STD;#define NCharS[n];intn,m,p,l,ans=0, Next[n],a[n][n],b[n][n],f[n][n];voidInit () {l=strlen(s) +1; for(inti=l-2; i>=0; i--) s[i+1]=s[i]; s[0]="'; for(intI=2; i<l; i++) {intx=next[i-1]; while(x && s[i]!=s[x+1]) x=next[x];if(s[i]==s[x+1]) next[i]=x+1; } for(intI=0; i<l-1; i++) for(intj=0; j<=9; J + +) {intX=i; while(x && j!=s[x+1]-' 0 ') x=next[x];if(j==s[x+1]-' 0 ') f[i][x+1]++;Elsef[i][0]++; }memcpy(B,f,sizeof(f));voidJZC (intx) {if(x==1)return;if(x>3) Jzc (x>>1);memsetA0,sizeof(a)); for(intI=0; i<m; i++) for(intj=0; j<m; J + +) for(intk=0; k<m; k++) a[i][j]= (a[i][j]+f[i][k]*f[k][j])%p;memcpy(F,a,sizeof(a));if(x&1){memsetA0,sizeof(a)); for(intI=0; i<m; i++) for(intj=0; j<m; J + +) for(intk=0; k<m; k++) a[i][j]= (a[i][j]+f[i][k]*b[k][j])%p;memcpy(F,a,sizeof(f)); }}intMain () {scanf("%d%d%d%s", &n,&m,&p,&s); Init (); JZC (n); for(intI=0; i<m; i++) ans= (ans+f[0][i])%p;printf("%d\n", ans);return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Matrix Multiply" "KMP" "Hnoi 2008" "Bzoj 1009" 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.