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.
题意:定义一个数为好数同时满足下面三个条件:
1、所有奇数位上的数字都不为d
2、所有偶数位上的数字都为d
3、这个数能整除m
现在给你m,d以及区间[a,b],让你求a,b之间有多少好数。
思路:显然是数位dp,但是比赛的时候我没调出来。先看大佬的总结:
1、因为是大数,所以边界不能直接-1,可以用字符串模拟大数-1,也可以单独判断一下边界。
2、怎样判断除尽,只需要我们每次保留余数就可以了
3、怎样记录状态 首先是位置,然后是余数
为什么是余数呢?
因为当前的余数,_yu_ _,表示的是到当前位所剩下的, 也就是说
当前位取遍所有的0-9之后,后面所有的结果。
它也是一种计数,只是不同的一个标准,但是他们互斥,并且%的
所有的情况就是[0,m) ,这样就可以不重不漏的算完所有的情况
上三条来自 暴力都不会_QAQ
我的总结:
1、仔细读题!!!是所有偶数位必须为d,所有奇数位必须不为d。
2、偶数位奇数位是从最高位开始算的,所以我们dp的时候从最高位开始。因为要整除m,每次把余数*10+当前的数字。
3、用字符串模拟大数字,同样是solve(y)-solve(x-1),这里我们不需要x-1,只需要单独判断一下x这个数是否符合条件就可以了。
4、第二维我直觉是用余数,但详细证明参加上面第三条。。。
代码:
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define N 30
#define inf 1<<29
#define MOD 2007
#define LL long long
using namespace std;
typedef long long ll;
const ll mo=1e9+7;
char s[2010],ss[2010];
ll dp[2010][2010],a[2010];
ll m,d,jo;
int len;
ll dfs(int pos,ll sum,bool limit)
{
if(pos==len) return sum==0;
if(!limit && dp[pos][sum]!=-1) return dp[pos][sum];
int up=limit ? a[pos] : 9;
ll tmp=0;
for(int i=0;i<=up;i++)
{
if(pos&1)
{
if(i==d)
tmp=(tmp+dfs(pos+1,(sum*10+i)%m,limit&&i==a[pos]))%mo;
}
else if(i!=d)
tmp=(tmp+dfs(pos+1,(sum*10+i)%m,limit&&i==a[pos]))%mo;
}
if(!limit) dp[pos][sum]=tmp;
return tmp;
}
int solve(char *s)
{
int pos=strlen(s)-1;
len=0;
while(len<=pos)
{
a[len]=s[len]-'0';
len++;
}
//printf("%s\n",s);
ll as=dfs(0,0,true);
return as;
}
int main()
{
int le,ri;
while(scanf("%lld%lld",&m,&d)!=EOF)
{
memset(dp,-1,sizeof(dp));
scanf("%s%s",s,ss);
int l=strlen(s);
ll spc=1,sm=0;
for(int i=0;i<l;i++)
{
if((i%2==0)&&((s[i]&15)==d)) {spc=0;break;}
else if((i&1)&&((s[i]&15)!=d)) {spc=0;break;}
sm=(sm*10+(s[i]&15))%m;
}
if(spc) spc=(sm==0);
printf("%lld\n",(solve(ss)-solve(s)+spc+mo)%mo);
}
return 0;
}