CodeForces

来源:互联网 发布:微信辅助软件 编辑:程序博客网 时间:2024/06/12 05:02

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.

Example
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.

数位dp不需要什么技巧
不过注意一下范围
这个题的范围比较坑人

#include<iostream>#include<cmath>#include<cstdio>#include<string>#include<cstring>#include<algorithm>using namespace std;long long  dp[7000][2001];long long mo=1e9+7;long long bte[7000];long long m,d;long long dfs(int weishu,int biaoji,int yushu,int qz){    if (weishu == 0)return yushu == 0;    if(biaoji&&dp[weishu][yushu]!=-1)return dp[weishu][yushu];    int bianjie=biaoji?9:bte[weishu];    long long jieg=0;    for(int a=0;a<=bianjie;a++)    {        if(qz%2)        {            if(a==d)continue;            jieg+=dfs(weishu-1,!(a==bianjie&&bianjie==bte[weishu]&&biaoji==0),(yushu*10+a)%m,qz+1);            jieg %= mo;        }        else        {            if(a!=d)continue;            jieg+=dfs(weishu-1,!(a==bianjie&&bianjie==bte[weishu]&&biaoji==0),(yushu*10+a)%m,qz+1);            jieg %= mo;        }    }    if(biaoji)dp[weishu][yushu]= jieg % mo;;    return jieg %= mo;;}int jiejue(string q){    if(q=="0")return 0;    memset(bte,0,sizeof(bte));    bte[0] = q.size();    for (int a = 0; a < bte[0]; a++)    {        bte[a + 1] = q[bte[0] - a-1] - '0';    }    return dfs(bte[0],0,0,1);}int main(){    cin>>m>>d;    string z,y;    cin>>z>>y;    memset(dp,-1,sizeof(dp));    int u=jiejue(y)-jiejue(z);    memset(bte, 0, sizeof(bte));    int yy = u;    bte[0] = z.size();    for (int a = 0; a < bte[0]; a++)bte[a + 1] = z[bte[0] - a-1] - '0';    int jc = 1;    int we = 0;    for (int a = z.size(); a >=1; a--)    {        we *= 10;        we += bte[a];        we %= m;    }    if (we%m)jc = 0;    for (int a = bte[0],b=1; a >= 1; a--,b++)    {        if (b % 2)        {            if (bte[a] == d)jc = 0;        }        else        {            if (bte[a] != d)jc = 0;        }    }    cout << (u + jc + mo) % mo;}
原创粉丝点击