Magic Numbers CodeForces - 628D [数位DP]


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 1727374171 are 7-magic but 7771233471 are not 7-magic. On the other hand the number 7 is 0-magic123 is 2-magic34 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 ≤ 20000 ≤ 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 16263646567686 and 96.

The numbers from the answer of the second example are 246 and 8.

The numbers from the answer of the third example are 17672717575767078797 and 9747.


思路:偶数位置只能有d,奇数位置 不能有d

数位DP....  肝


#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define bug1 cout <<"bug1"<<endl
#define bug2 cout <<"bug2"<<endl
#define bug3 cout <<"bug3"<<endl
using namespace std;
typedef long long ll;

const int MAX_N=2e5+5;
int m,d;
char a[2300],b[2300];
int bit[2300];
int dp[2300][4000];
int len;

ll dfs(int pos,int mod,bool lead,bool limit){
    if(pos==len+1) return (mod==0);
    if(!limit && dp[pos][mod]!=-1) return dp[pos][mod];
    int up=limit ? bit[pos] : 9 ;
    ll ans=0;
    for(int i=0;i<=up;i++){
        int tempmod=(mod*10+i)%m; //模拟除法
        if(pos%2==1 && i==d)    continue;
        if(pos%2==0 && i!=d)    continue;
        ans+=dfs(pos+1,tempmod,lead&&i==0,limit && bit[pos]==i);
    }
    ans=ans%MOD;
    if(!limit)  dp[pos][mod]=ans;
    return ans;
}

ll solve(const char *s){
    memset(dp,-1,sizeof(dp));
    int pos=1;
    len=strlen(s+1);
    while(pos<=len){
        bit[pos]=s[pos]-'0'; ///bit存的是数字而不是字符
        pos++;

    }
//    cout <<"***********"<<endl;
//    printf("%s\n",bit+1);
    return dfs(1,0,true,true);
}

bool judge(const char*s){
    int mod=0;
    len=strlen(s+1);
    for(int i=1;i<=len;i++){
        mod=(mod*10+s[i]-'0')%m;
        if(i%2==1&&s[i]-'0'==d)    return false;///是s[i]和d的关系
        if(i%2==0&&s[i]-'0'!=d)   return false;
    }
    return mod==0;
}

int main(void){
    cin >> m>>d>>a+1>>b+1;
    printf("%lld",((solve(b)-solve(a)+judge(a))%MOD+MOD)%MOD);
}



全部评论

相关推荐

最近拿到了正浩的提前批offer感觉自己的实力得到了肯定,也给了我更多底气
搞机墨镜猫:正浩提前批官网好像就只有电力电子软硬件,哥们投的是这两个岗位吗
26届校招投递进展
点赞 评论 收藏
分享
小鹏、大疆、米哈游、MinMax小鹏上午投的下午就约面,进度未免也太快了
蛇年行大运fff:哥们 盗贴有意思吗,我发xhs上的给你搬过来了😅😅😅
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-15 17:17
听说过付费实习,没想到这么贵啊我去,要不我给你个腰子吧
哈哈哈,你是老六:这种公司一定要注意啊,不要随便签合同,只要签了后面钱可能回不来,而且你通过法律途径也弄不回
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务