628D - Magic Numbers(数位dp)

47 篇文章 0 订阅
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
Copy
2 6
10
99
output
8
input
Copy
2 0
1
9
output
4
input
Copy
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.

思路:自我感觉不错的一个数位dp,筛选条件是偶数位必须是d 奇数位不能是d,有个坑点就是如果b太大导致取摸之后数比a小, 这时候需要先加上mod再取模。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=2010;
const ll mod=1e9+7;
ll dp[maxn][maxn];
char a[maxn], b[maxn];
int wei[maxn], m, d;

ll dfs(int pos, ll sum, bool limit, int len){
	if(pos==-1) return sum==0;
	if(!limit && dp[pos][sum]!=-1) return dp[pos][sum];
	
	ll ans=0; 
	int up= limit ? wei[pos] : 9;
	for(int i=0; i<=up; i++)
	{
		if((len-pos)%2==0 && i!=d) continue;
		if((len-pos)%2==1 && i==d) continue;
		ans=(ans%mod+dfs(pos-1, (sum*10+i)%m, limit&&(i==wei[pos]), len)%mod)%mod;
	}
	
	return limit ? ans : dp[pos][sum]=ans;
}

ll solve(char *s){
	int len=strlen(s);
	
	for(int i=0; i<len; i++)
	{
		wei[i]=s[len-i-1]-'0';
	}
	
	return dfs(len-1, 0, true, len);
}

bool check(){
	int len=strlen(a);
	ll s=0;
	for(int i=0; i<len; i++)
	{
		s=(s*10+a[i]-'0')%m;
		if((i+1)%2==0 && a[i]-'0'!=d || (i+1)%2==1 && a[i]-'0'==d)
			return false;
	}
	if(!s) 
		return true;
	else
		return false;
}

int  main(){
	memset(dp, -1, sizeof(dp));
	
	scanf("%d%d", &m, &d);
	scanf("%s%s", a, b);
	
	ll ans=solve(b)-solve(a);

	if(check()){//判断a是否合格
		ans++;
	}
	ans=(ans+mod)%mod;
	printf("%lld\n", ans);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值