Bzoj3530 [Sdoi2014] Count

Source: Internet
Author: User

Time Limit:10 Sec Memory limit:512 MB
submit:744 solved:394 Description

We call a positive integer n is a lucky number when and only if its decimal representation does not contain any element in the number string set S as its substring. For example, when s= (22,333,0233), 233 is the lucky number, 2333, 20233, 3223 is not the lucky number.
Given N and S, calculate the number of lucky numbers not greater than N.

Input


The first line of the input contains the integer n.
The next line is an integer m, which represents the number of elements in S.
The next m line, a number string per line, represents an element in S.

Output

Outputs an integer that represents the value of the answer modulus 109+7.

Sample Input20
3
2
3
14
Sample Output -HINT

The L in the following table represents the length of N, and L represents the sum of all the string lengths in S.


1 < =l < =1200, 1 < =m < =100, 1 < =l < =1500

Source

Round 1 Day 1

DP on AC automaton

Just a little bit more trouble than the DP on the trie tree.

First build the trie tree, set up a good AC automaton, and then run the digital DP. The case where the length of the number is less than n is processed separately, the maximum bit limit is not considered at this time. After processing a number length equal to n length, when the highest bit is not filled to separate the decision.

See the code specifically:

1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <queue>8 using namespacestd;9 Const intmod=1e9+7;Ten intt[1510][ One],cnt=1; One BOOLend[1510];intfail[1510]; A voidInit () { for(intI=0; i<=9; i++) t[0][i]=1;} - voidInsertChars[]) { -     intrt=1, I,j; the     intlen=strlen (s); -      for(i=0; i<len;i++){ -         if(!t[rt][s[i]-'0']) t[rt][s[i]-'0']=++CNT; -rt=t[rt][s[i]-'0']; +     } -end[rt]=1; +     return; A } at intq[15100],hd,tl; - voidBuild () { -Hd=0, tl=1; -q[++hd]=1; -fail[1]=0; -      while(hd<=TL) { in         intnow=q[hd++]; -end[now]|=End[fail[now]]; to          for(intI=0; i<=9; i++){ +             intv=T[now][i]; -             if(!v) { thet[now][i]=T[fail[now]][i]; *             } $             Else{Panax Notoginseng                 intk=Fail[now]; -                  while(!t[k][i]) k=Fail[k]; thefail[v]=T[k][i]; +q[++tl]=v; A             } the         } +     } -     return; $ } $ intn,m; - Chars[1510],c[1510]; - inta[1510]; the intf[1510][1510][2]; - intans=0;Wuyi intMain () { thescanf"%s", s+1); -     inti,j; Wu     intLen=strlen (s+1); -      for(i=1; i<=len;i++) a[i]=s[i]-'0'; About init (); $scanf"%d",&m); -      for(i=1; i<=m;i++){ -scanf"%s", c); - Insert (c); A     } + Build (); the      for(i=1; i<=9; i++)//First -         if(!end[t[1][i]]) $f[1][t[1][i]][0]++; the      for(i=1; i<len-1; i++)  the          for(j=1; j<=cnt;j++) the              for(intk=0; k<=9; k++){ the                 if(!End[t[j][k]]) { -f[i+1][t[j][k]][0]+=f[i][j][0]; inf[i+1][t[j][k]][0]%=MoD; the                 } the             } About      for(i=1; i<len;i++) the       for(j=1; j<=cnt;j++) ans= (ans+f[i][j][0])%MoD; theMemset (F,0,sizeoff); the      for(i=1; i<=a[1];i++) +         if(!end[t[1][i]]) { -             if(i==a[1]) f[1][t[1][i]][1]++; the             Elsef[1][t[1][i]][0]++;Bayi         } the      for(i=1; i<len;i++) the        for(j=1; j<=cnt;j++) -          for(intk=0; k<=9; k++) -             if(!End[t[j][k]]) { thef[i+1][t[j][k]][0]+=f[i][j][0]; thef[i+1][t[j][k]][0]%=MoD; the                 if(k<a[i+1]){ thef[i+1][t[j][k]][0]+=f[i][j][1]; -f[i+1][t[j][k]][0]%=MoD; the                 } the                 Else if(k==a[i+1]){ thef[i+1][t[j][k]][1]+=f[i][j][1];94f[i+1][t[j][k]][1]%=MoD; the                 } the             } the      for(i=1; i<=cnt;i++){98Ans= (ans+f[len][i][0])%MoD; AboutAns= (ans+f[len][i][1])%MoD; -     }101printf"%d\n", ans);102     return 0;103}

Bzoj3530 [Sdoi2014] Count

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.