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).
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.
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.
2 6 10 99
8
2 0 1 9
4
19 7 1000 9999
6
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.
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=2010;
int m,d,L;
ll dp[maxn][maxn];
int bit[maxn];
/*
坑点:因为是大数,所以边界不能直接-1,可以用字符串模拟大数-1,也可以单独判断一下边界。
1.怎样判断除尽 只需要我们每次保留余数就可以了
2.怎样记录状态 首先是位置,然后是余数
为什么是余数呢?
因为当前的余数,_yu_ _,表示的是到当前位所剩下的, 也就是说
当前位取遍所有的0-9之后,后面所有的结果。
它也是一种计数,只是不同的一个标准,但是他们互斥,并且%的
所有的情况就是[0,m) ,这样就可以不重不漏的算完所有的情况
*/
void add(ll& ans,ll b){
ans=(ans+b)%mod;
}
ll dfs(int pos,int pre,int limit){
if(pos==L)
return pre==0;//草,要判断是不是除尽
if(!limit&&dp[pos][pre]!=-1) return dp[pos][pre];
ll ans=0;
int up=limit?bit[pos]:9;
for(int i=0;i<=up;i++){
if((pos&1)&&i!=d) continue;
if((!(pos&1))&&i==d) continue;
add(ans,dfs(pos+1,(pre*10+i)%m,limit&&i==up)); //取余要注意,加法的地方处处取余
}
if(!limit) dp[pos][pre]=ans;
return ans;
}
int check(string s){
for(int i=0;i<L;i++)
bit[i]=s[i]-'0';
int pre=0;
for(int i=0;i<L;i++){
if((i&1)&&bit[i]!=d) return 0;
if((!(i&1))&&bit[i]==d) return 0;
pre=(pre*10+bit[i])%m;
}
return pre==0;
}
ll count(string s){
for(int i=0;i<L;i++)
bit[i]=s[i]-'0';
return dfs(0,0,1);
}
int main(){
ios::sync_with_stdio(false);
memset(dp,-1,sizeof(dp));
cin>>m>>d;
string a,b;
cin>>a>>b;
L=a.length();
ll ans=count(b)-count(a)+check(a);
ans=(ans+mod)%mod;
cout<<ans<<endl;
return 0;
}