Bzoj3530 [SDOI2014] Count

Source: Internet
Author: User

Bzoj3530 [SDOI2014] Count

 

3530: [Sdoi2014] Count Description

We call a positive integer N as the lucky number. If and only when its decimal representation does not contain any element in the number string set S, it serves as its sub-string. For example, when S = (22,333,023 3), 233 is the lucky number. 2333, 20233, and 3223 are not the lucky number.
Given N and S, calculate the number of lucky winners not greater than N.

Input


The first line of the input contains an integer N.
The next row contains an integer M, indicating the number of elements in S.
In the next M row, each row has a number string, indicating an element in S.

Output

Output an integer in a row, indicating the value of the modulo 109 + 7.

Sample Input20
3
2
3
14 Sample Output14HINT

In the following table, l represents the length of N, and L represents the sum of the lengths of all strings in S.


1 <= l <= 1200, 1 <= M <= 100, 1 <= L <= 1500

Source

Round 1 day 1

AC automation + Dynamic Planning, great ideas

First, add the mismatched edge to the Trie tree to form a new graph.

Then, dynamic planning is performed in two cases, namely, the number of digits is less than l and the number is equal to l. When the number of digits is less than l, there is no limit, and it can be transferred directly. When the number of digits is equal to l, you need to add one dimension to indicate whether the number of first I bits is equal to the first I bits of n. (For details about transfer, refer to the Code)

Note that the lucky number does not have a leading zero, so the first step in the figure cannot go through t [1] [0].

#include
 
  #include
  
   #include
   
    #include
    
     #include
     
      #include#include
      
       #define F(i,j,n) for(int i=j;i<=n;i++)#define D(i,j,n) for(int i=j;i>=n;i--)#define ll long long#define pa pair
       
        #define maxn 1510#define mod 1000000007using namespace std;int t[maxn][10],go[maxn],f[1210][maxn][2],a[maxn];int tot=1,n,l,ans=0;char s[maxn];bool v[maxn];queue
        
          q;inline void insert(){scanf("%s",s);int len=strlen(s),now=1;F(i,0,len-1){int x=s[i]-'0';if (!t[now][x]) t[now][x]=++tot;now=t[now][x];}v[now]=1;}inline void bfs(){q.push(1);while (!q.empty()){int x=q.front(),y,j;q.pop();v[x]|=v[go[x]];F(i,0,9){j=go[x];while (j&&!t[j][i]) j=go[j];if (t[x][i]){go[y=t[x][i]]=j?t[j][i]:1;q.push(y);}else t[x][i]=j?t[j][i]:1;}}}int main(){scanf("%s",s);l=strlen(s);F(i,0,l-1) a[i]=s[i]-'0';scanf("%d",&n);F(i,1,n) insert();bfs();memset(f,0,sizeof(f));F(i,1,9) if (!v[t[1][i]]) f[1][t[1][i]][0]+=1;F(i,1,l-2) F(j,1,tot) F(x,0,9) if (!v[t[j][x]])(f[i+1][t[j][x]][0]+=f[i][j][0])%=mod;F(i,1,l-1) F(j,1,tot) (ans+=f[i][j][0])%=mod;memset(f,0,sizeof(f));F(i,1,a[0]) if (!v[t[1][i]]) f[1][t[1][i]][i==a[0]]+=1;F(i,1,l-1) F(j,1,tot) F(x,0,9) if (!v[t[j][x]]){(f[i+1][t[j][x]][0]+=f[i][j][0])%=mod;if (x
         

 

 

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.