Codeforces 628D Magic Numbers 数位DP

在[m, n]区间内,找到所有满足指定条件的d-magic数字(偶数位为d,奇数位不为d)且是m的倍数的数字数量,最后返回对10^9 + 7取模的结果。数位动态规划是解决此类问题的关键,通过转移状态计算每个位置的贡献,确保边界条件和取模操作正确处理。" 94744660,8195203,参数化3D人脸建模技术:Morphable Model解析,"['三维建模', '图像处理', '计算机视觉', '人工智能', '人脸识别']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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]范围内求有几个数,满足:1.是m的倍数,2.从左往右数的偶数位一定是数字k,奇数位一定不是k。
取模运算可加,高位的余数*10+低位再取模不影响结果,所以数位DP递归到最后一位就可以得到这个数是否是m的整数倍,转移的状态是上一位取模剩下的余数。
注意数位DP一般是求(a,b],此题要额外检验a是否满足条件,注意变量开longlong,且a<1e9+7,b>1e9+7时由于取模b

#include <iostream>
#include <stdio.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
typedef long long ll;
int mo[4][2]={0,1,1,0,0,-1,-1,0};
const int MAXN=1000000007;
const int sz=2005;
char s[sz];
ll dp[sz][sz][2],bit[sz];
int m,k,len;

ll dfs(int pos,int rem,int up){
    if(pos==len) return rem==0;
    if(dp[pos][rem][up]!=-1) return dp[pos][rem][up];
    int end;
    if(up) end=bit[pos];
    else end=9;
    ll ans=0;
    for(int i=0;i<=end;i++){
        if(pos%2==0&&i==k) continue;
        if(pos%2==1&&i!=k) continue;
        ans+=dfs(pos+1,((rem*10)+i)%m,up&&i==end);
        ans%=MAXN;
    }
    dp[pos][rem][up]=ans;
    return ans;
}

ll get(){
    len=strlen(s);
    for(int i=0;i<len;i++){
        bit[i]=s[i]-'0';
    }
    return dfs(0,0,1);
}

int check(){
    int t=0;
    for(int i=0;i<len;i++){
        if(i%2==0&&bit[i]==k) return 0;
        if(i%2==1&&bit[i]!=k) return 0;
        t=t*10+bit[i];
        t%=m;
    }
    if(t==0) return 1;
    else return 0;
}

int main()
{
    //freopen("r.txt","r",stdin);
    while(scanf("%d%d",&m,&k)!=EOF){
        ll ans=0;
        memset(dp,-1,sizeof(dp));
        scanf("%s",&s);
        ll a=get();
        ans+=check();
        memset(dp,-1,sizeof(dp));
        scanf("%s",&s);
        ll b=get();
        ans+=b-a;
        ans=(ans+MAXN)%MAXN;
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值