数位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-magicand 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, 8797and 9747.

题意:

d magic number(0<=d<9)的意思就是一个数,从最高位开始奇数位不是d,偶数位是d

题目问,给a,b,m,d(a<=b,m<2000)问,a,b之间有多少个数满足既是d magic number,又可以被m整除

分析:

一道简单的数位dp题,也没什么好说的。就是bit下标是从0开始的,奇偶换一下就可以了。
 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=2005;
const int mod=1e9+7;
int m,d;
char a[N],b[N];
ll f[N][N];
int bit[N];
int len;
ll dfs(int pos,int pre,bool lim)
{
    if(pos==len)return pre==0;
    if(!lim&&f[pos][pre]!=-1)return f[pos][pre];
    ll ans=0;
    int n=lim?bit[pos]:9;
    for(int i=0;i<=n;i++){
        if(pos&1&&i!=d)continue;
        if(!(pos&1)&&i==d)continue;
        ans=(ans+dfs(pos+1,(pre*10+i)%m,lim&&i==n)%mod)%mod;
    }
    if(!lim)f[pos][pre]=ans;
    return ans;
}
bool judge(char *s)
{
    int ok=0;
    for(int i=0;i<len;i++){
        int t=s[i]-'0';
        ok=(ok*10+t)%m;
        if(!(i&1)&&t==d)return false;
        if((i&1)&&t!=d)return false;
    }
    return ok==0;
}
ll solve(char *s)
{
    len=strlen(s);
    for(int i=0;i<len;i++)bit[i]=s[i]-'0';
    return dfs(0,0,1);
}
int main()
{
    scanf("%d%d",&m,&d);
    scanf("%s%s",a,b);
    memset(f,-1,sizeof(f));
    cout<<(solve(b)-solve(a)+judge(a)+mod)%mod<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值