# Educational Codeforces Round 8 D. Magic Numbers （数位DP）

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 100100
#define MOD 1000000007

long long dp[2020][2020];
int bit[2020];
int m,d;
int len;

long long dfs(int pos,int st,int zero,int flag,int odd)
{
//printf("%d %d %d %d %d\n",pos,st,zero,flag,odd);
if(pos == len)
return st == 0;
if(flag && dp[pos][st] != -1)
return dp[pos][st];
int u = flag?9:bit[pos];
long long ans = 0;
if(!odd)
{
for(int i = 0; i <= u; i++)
if(i != d)
ans += dfs(pos+1,(st*10+i)%m,zero||i,flag||i<u,odd^1);
}
else
{
if(flag)
ans += dfs(pos+1,(st*10+d)%m,zero||d,flag||d<u,odd^1);
else if(d <= u)
ans += dfs(pos+1,(st*10+d)%m,zero||d,flag||d<u,odd^1);
}
ans %= MOD;
if(flag)
dp[pos][st] = ans;
return ans;
}

long long solve(char s[])
{
len = strlen(s);
for(int i = 0; i < len; i++)
bit[i] = s[i] - '0';
return dfs(0,0,0,0,0);
}

int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,C = 1;
//scanf("%d",&t);
while(scanf("%d%d",&m,&d) != EOF)
{
memset(dp,-1,sizeof(dp));
char s1[2020],s2[2020];
scanf("%s%s",s1,s2);
int len = strlen(s1);
int flag = 1,u = 0,cnt = 0;
for(int i = 0; i < len; i++)
{
u ^= 1;
if(u & 1 && s1[i] == '0'+d)
flag = 0;
if(!(u&1) && s1[i] != '0'+d)
flag = 0;
cnt = cnt * 10 + s1[i] - '0';
cnt %= m;
}
if(cnt)
flag = 0;
//printf("%lld %lld\n",solve(s1),solve(s2));
printf("%lld\n",(solve(s2)-solve(s1)+flag+MOD)%MOD);
}
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：Educational Codeforces Round 8 D. Magic Numbers （数位DP） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)