D. Magic Numbers 数为DP入门

D. Magic Numbers

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

2 6
10
99

Output

Copy

8

Input

Copy

2 0
1
9

Output

Copy

4

Input

Copy

19 7
1000
9999

Output

Copy

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.

#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int m,d;
ll dp[2003][2003][2];
int mod=1e9+7;
char a[2004],b[2004];
int len;
ll dfs(int pos,int num,int limit,char x[])
{
	if(pos==len)
	{
		if(num==0) return 1;
		else return 0;
	}
	if(dp[pos][num][limit]!=-1) return dp[pos][num][limit];//记忆化 
	int up=limit ? x[pos] :9;
	ll ans=0;
	for(int i=0;i<=up;i++)//枚举各个数位上的有效数字范围 
	{
		if(pos%2&&i!=d) continue;
		if(pos%2==0&&i==d) continue;
		int num2=i+num*10;
		num2%=m;
		ans=(ans+dfs(pos+1,num2,limit&&(i==up),x))%mod;
		ans%=mod;
	}
	return dp[pos][num][limit]=ans;
	
	
}
int main()
{
	scanf("%d%d",&m,&d);
	scanf("%s",a);
	scanf("%s",b);
	len=strlen(a);
	for(int i=0;i<len;i++)
	{
		a[i]-='0';
		b[i]-='0';
	}
	memset(dp,-1,sizeof(dp));
	ll ans=dfs(0,0,1,b);
	memset(dp,-1,sizeof(dp));
	ans-=dfs(0,0,1,a);
	int num=0;
	int i=0;
	for(i=0;i<len;i++)//a本身对答案的贡献  
	{
		if(i%2&&a[i]!=d) break;
		if(i%2==0&&a[i]==d) break;
		num=num*10+a[i];
		num%=m;
	}
	if(i==len&&num==0) ans++;
	ans+=mod;//可能溢出 
	printf("%lld\n",ans%mod);
}
发布了44 篇原创文章 · 获赞 6 · 访问量 1171

猜你喜欢

转载自blog.csdn.net/qq_43868883/article/details/104132817