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;
}

codeforces628D. Magic Numbers (数位dp)的更多相关文章

  1. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

  2. CodeForces 628 D Magic Numbers 数位DP

    Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...

  3. 【CF628D】Magic Numbers 数位DP

    [CF628D]Magic Numbers 题意:求[a,b]中,偶数位的数字都是d,其余为数字都不是d,且能被m整除的数的个数(这里的偶数位是的是从高位往低位数的偶数位).$a,b<10^{2 ...

  4. CodeForces 628D Magic Numbers (数位dp)

    题意:找到[a, b]符合下列要求的数的个数. 1.该数字能被m整除 2.该数字奇数位全不为d,偶数位全为d 分析: 1.dp[当前的位数][截止到当前位所形成的数对m取余的结果][当前数位上的数字是 ...

  5. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  6. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  7. Codeforces Beta Round #51 D. Beautiful numbers 数位dp

    D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...

  8. poj 3252 Round Numbers(数位dp 处理前导零)

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  9. uva 10712 - Count the Numbers(数位dp)

    题目链接:uva 10712 - Count the Numbers 题目大意:给出n,a.b.问说在a到b之间有多少个n. 解题思路:数位dp.dp[i][j][x][y]表示第i位为j的时候.x是 ...

随机推荐

  1. LeetCode703 流中第k大的元素

    前言: 我们已经介绍了二叉搜索树的相关特性,以及如何在二叉搜索树中实现一些基本操作,比如搜索.插入和删除.熟悉了这些基本概念之后,相信你已经能够成功运用它们来解决二叉搜索树问题. 二叉搜索树的有优点是 ...

  2. 【Problem】前端项目运行:Module build failed:Error Node Sass does not yet support my current environmen

    我在运行renren-fast-vue前端项目时,安装完依赖cnpm install 启动服务npm run dev 出现问题. Module build failed: Error: Node Sa ...

  3. 【高级排序算法】1、归并排序法 - Merge Sort

    归并排序法 - Merge Sort 文章目录 归并排序法 - Merge Sort nlogn 比 n^2 快多少? 归并排序设计思想 时间.空间复杂度 归并排序图解 归并排序描述 归并排序小结 参 ...

  4. 如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢

    如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢

  5. P1967 货车运输(倍增LCA,生成树)

    题目链接: https://www.luogu.org/problemnew/show/P1967 题目描述 A国有n座城市,编号从 1到n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制, ...

  6. Xctf攻防世界—crypto—Normal_RSA

    下载压缩包后打开,看到两个文件flag.enc和pubkey.pem,根据文件名我们知道应该是密文及公钥 这里我们使用一款工具进行解密 工具链接:https://github.com/3summer/ ...

  7. EnvironmentPostProcessor怎么做单元测试?阿里P7解答

    简介 从Spring Boot 1.3开始,我们可以在应用程序上下文刷新之前使用EnvironmentPostProcessor来自定义应用程序的Environment.Environment表示当前 ...

  8. DSL是什么?Elasticsearch的Query DSL又是什么?

    1.DSL简介 DSL 其实是 Domain Specific Language 的缩写,中文翻译为领域特定语言.而与 DSL 相对的就是 GPL,这里的 GPL 并不是我们知道的开源许可证(备注:G ...

  9. 记一次压测问题定位:connection reset by peer,TCP三次握手后服务端发送RST_网络_c359719435的专栏-CSDN博客 https://blog.csdn.net/c359719435/article/details/80300433

    记一次压测问题定位:connection reset by peer,TCP三次握手后服务端发送RST_网络_c359719435的专栏-CSDN博客 https://blog.csdn.net/c3 ...

  10. LeetCode上并发题目无Go版本:台湾同胞试水 — 交替打印FooBar

    https://mp.weixin.qq.com/s/I5va3PI1oGIj8R_n3Nw2yw