Codeforces 628D Magic Numbers 數位DP

Consider the decimal presentation of an integer. Let’s call a number d-magic if digit d appears in decimal presentation of the number on even positions and nowhere else.

For example, the numbers 1727374, 17, 1 are 7-magic but 77, 7, 123, 34, 71 are not 7-magic. On the other hand the number 7 is 0-magic, 123 is 2-magic, 34 is 4-magic and 71 is 1-magic.

Find the number of d-magic numbers in the segment [a, b] that are multiple of m. Because the answer can be very huge you should only find its value modulo 109 + 7 (so you should find the remainder after dividing by 109 + 7).

Input

The first line contains two integers m, d (1 ≤ m ≤ 2000, 0 ≤ d ≤ 9) — the parameters from the problem statement.

The second line contains positive integer a in decimal presentation (without leading zeroes).

The third line contains positive integer b in decimal presentation (without leading zeroes).

It is guaranteed that a ≤ b, the number of digits in a and b are the same and don’t exceed 2000.

Output

Print the only integer a — the remainder after dividing by 109 + 7 of the number of d-magic numbers in segment [a, b] that are multiple of m.

Examples

input

2 6
10
99

output

8

input

2 0
1
9

output

4

input

19 7
1000
9999

output

6

Note

The numbers from the answer of the first example are 16, 26, 36, 46, 56, 76, 86 and 96.

The numbers from the answer of the second example are 2, 4, 6 and 8.

The numbers from the answer of the third example are 1767, 2717, 5757, 6707, 8797 and 9747.

在[a,b]範圍內求有幾個數,滿足:1.是m的倍數,2.從左往右數的偶數位一定是數字k,奇數位一定不是k。
取模運算可加,高位的餘數*10+低位再取模不影響結果,所以數位DP遞歸到最後一位就可以得到這個數是否是m的整數倍,轉移的狀態是上一位取模剩下的餘數。
注意數位DP一般是求(a,b],此題要額外檢驗a是否滿足條件,注意變量開longlong,且a<1e9+7,b>1e9+7時由於取模b

#include <iostream>
#include <stdio.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
typedef long long ll;
int mo[4][2]={0,1,1,0,0,-1,-1,0};
const int MAXN=1000000007;
const int sz=2005;
char s[sz];
ll dp[sz][sz][2],bit[sz];
int m,k,len;

ll dfs(int pos,int rem,int up){
    if(pos==len) return rem==0;
    if(dp[pos][rem][up]!=-1) return dp[pos][rem][up];
    int end;
    if(up) end=bit[pos];
    else end=9;
    ll ans=0;
    for(int i=0;i<=end;i++){
        if(pos%2==0&&i==k) continue;
        if(pos%2==1&&i!=k) continue;
        ans+=dfs(pos+1,((rem*10)+i)%m,up&&i==end);
        ans%=MAXN;
    }
    dp[pos][rem][up]=ans;
    return ans;
}

ll get(){
    len=strlen(s);
    for(int i=0;i<len;i++){
        bit[i]=s[i]-'0';
    }
    return dfs(0,0,1);
}

int check(){
    int t=0;
    for(int i=0;i<len;i++){
        if(i%2==0&&bit[i]==k) return 0;
        if(i%2==1&&bit[i]!=k) return 0;
        t=t*10+bit[i];
        t%=m;
    }
    if(t==0) return 1;
    else return 0;
}

int main()
{
    //freopen("r.txt","r",stdin);
    while(scanf("%d%d",&m,&k)!=EOF){
        ll ans=0;
        memset(dp,-1,sizeof(dp));
        scanf("%s",&s);
        ll a=get();
        ans+=check();
        memset(dp,-1,sizeof(dp));
        scanf("%s",&s);
        ll b=get();
        ans+=b-a;
        ans=(ans+MAXN)%MAXN;
        cout<<ans<<endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章