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],让你找到这个区间内满足没有前导零且偶数位都是d,奇数位不出现d,并且这个数能被m整除的数的个数。

思路:用dp[pos][yushu][oushu]表示pos位前面的位形成的数modm后余数为yushu,且当前位是否是偶数的方案数,要注意前导零。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define MOD 1000000007
char s1[2005],s2[2005];
int wei[2005];
ll dp[2005][2005][2];
int m,d;
void add(ll& x,ll y) {
x += y;
if(x>=MOD) x-=MOD;
} ll dfs(int pos,int yushu,int oushu,int flag,int zero)
{
int i,j;
if(pos==-1){
if(zero==1)return 0;
if(yushu==0)return 1;
else return 0;
}
if(!flag && !zero && dp[pos][yushu][oushu]!=-1){
return dp[pos][yushu][oushu];
} int ed=flag?wei[pos]:9;
ll ans=0;
if(zero==1){
add(ans,dfs(pos-1,yushu,oushu,0,1));
for(i=1;i<=ed;i++){
if(i!=d)add(ans,dfs(pos-1,(yushu*10+i)%m,1^oushu,flag&&wei[pos]==i,0) );
}
}
else{
if(oushu){
if(d<=ed)add(ans,dfs(pos-1,(yushu*10+d)%m,1^oushu,flag&&wei[pos]==d,0) );
}
else{
for(i=0;i<=ed;i++){
if(i!=d)add(ans,dfs(pos-1,(yushu*10+i)%m,1^oushu,flag&&wei[pos]==i,0) );
}
}
}
if(!flag && !zero){
dp[pos][yushu][oushu]=ans;
}
return ans;
} ll solve(char s[])
{
int len,i,j;
len=strlen(s);
for(i=len-1;i>=0;i--){
wei[i]=s[i]-'0';
}
return dfs(len-1,0,0,1,1);
} int main()
{
int n,i,j,len1,len2;
while(scanf("%d%d",&m,&d)!=EOF)
{
scanf("%s%s",s1,s2);
len1=strlen(s1);
reverse(s1,s1+len1);
for(i=0;i<len1;i++){
if(s1[i]=='0'){
s1[i]='9';
}
else{
s1[i]--;break;
}
}
if(s1[len1-1]=='0'){
s1[len1-1]='\0';
len1--;
} len2=strlen(s2);
reverse(s2,s2+len2);
memset(dp,-1,sizeof(dp));
ll num1=solve(s1);
ll num2=solve(s2);
printf("%I64d\n",((num2-num1)%MOD+MOD)%MOD ); }
return 0;
}

最新文章

  1. iOS UIView动画效果 学习笔记
  2. 在Gradle中使用jaxb的xjc插件
  3. [No00003E]26个字母暗藏的单词秘密
  4. 使用json格式输出
  5. Python内置的HTTP协议服务器SimpleHTTPServer
  6. shell将输入的参数逆序
  7. shell脚本例子集锦(习题总结)
  8. [转贴]C编译过程概述
  9. SQL Server T-SQL基础
  10. (2012年旧文)纪念史蒂夫乔布斯---IT界的普罗米修斯
  11. 一年四个P(Project)
  12. Delphi动态申请数组内存的方法(不使用SetLength,采用和C相似的方式)
  13. react入门——慕课网笔记
  14. Bootstrap Paginator分页插件+ajax 实现动态无刷新分页
  15. Linux下安装 Python3
  16. javaweb开发.常用的第三方包
  17. 【代码笔记】Web-HTML-颜色
  18. IN_ORDER_PLANNING、IN_BOM_CHANGE
  19. jQuery $(&#39;div&gt;ul&#39;) $(&#39;div ul&#39;
  20. HTML框架标签的使用-&amp;lt;frameset&amp;gt;

热门文章

  1. 《犬夜叉2021》我想通过Binder找到你
  2. 【SpringBoot1.x】SpringBoot1.x 入门
  3. Hdfs手动执行Balance
  4. MySQL select join on 连表查询和自连接查询
  5. 【Oracle】下载11.2.0.4的地址
  6. 一. SpringCloud简介与微服务架构
  7. window10系统安装
  8. Web自动化测试python环境中安装 --selenium安装、火狐和火狐驱动版本、谷歌和谷歌驱动版本、测试
  9. shell命令分隔符 二叉树结构的命令行树
  10. libevent之基于socket的bufferevent