题目
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).
输入
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.
输出
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.
Code
/*******************************
| Author: chzu_w
| Problem: D. Magic Numbers
| Contest: Educational Codeforces Round 8
| URL: https://codeforces.com/contest/628/problem/D
| When: 2023-04-25 19:55:04
|
| Memory: 256 MB
| Time: 2000 ms
*******************************/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define endl '\n'
#define pb push_back
#define NO cout << "NO" << endl;
#define YES cout << "YES" << endl;
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
typedef vector<int> VI;
typedef pair<int,int> PII;
ll MOD = 1e9 + 7;
ll powmod(ll a,ll b) {ll res=1;a%=MOD; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
mt19937 mrand(random_device{}());
const int N = 2e5 + 10;
int que[N], tot;
void solve()
{
int m, d; cin >> m >> d;
vector<VI> f(2200, VI(2200, -1));
string l, r; cin >> l >> r;
function<ll(ll, bool, ll, bool)> dfs = [&] (ll pos, bool limit, ll sum, bool flag) -> ll {
if (!pos) return (sum == 0);
if (!limit && ~f[pos][sum]) return f[pos][sum];
int up = limit ? que[pos] : 9;
ll res = 0;
if (!flag) {
if (d <= up) res = dfs(pos - 1, limit && d == up, (sum * 10 + d) % m, flag ^ 1) % MOD;
} else {
for (int i = 0; i <= up; i++) {
if (i == d) continue;
(res += dfs(pos - 1, limit && i == up, (sum * 10 + i) % m, flag ^ 1)) %= MOD;
}
}
if (!limit) return f[pos][sum] = res;
return res;
};
auto check = [&] (string x) {
int r = 0;
int len = x.size();
for(int i = 0; i < len; i ++ )
que[i + 1] = x[i] - '0';
for(int i = 1; i <= len; i ++)
{
if(i & 1) //奇数位
{
if(que[i] == d)
return false;
}
else{
if(que[i] != d)
return false;
}
r = (r * 10 + que[i]) % m;
}
return r == 0;
};
auto calc = [&] (string s) {
tot = 0;
reverse(all(s));
for (auto x : s) {
que[++tot] = x - '0';
}
return dfs(tot, true, 0, true);
};
ll res = (calc(r) - calc(l) + MOD) % MOD;
(res += check(l)) %= MOD;
cout << res << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// int T;cin >> T;
// while ( T -- )
solve();
return 0;
}