Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstring>
- using namespace std;
- int m, d;
- string num_str;
- long long dp[2000][2000][2];
- const int MOD = 1e9 + 7;
- long long go(int pos, int rem, bool tight) {
- if (pos == num_str.length()) {
- return (rem == 0) ? 1 : 0;
- }
- if (dp[pos][rem][tight] != -1) {
- return dp[pos][rem][tight];
- }
- long long ans = 0;
- int upper_bound = tight ? (num_str[pos] - '0') : 9;
- for (int digit = 0; digit <= upper_bound; ++digit) {
- if ((pos + 1) % 2 == 0) { // Even position
- if (digit != d) {
- continue;
- }
- } else { // Odd position
- if (digit == d) {
- continue;
- }
- }
- bool new_tight = tight && (digit == upper_bound);
- int new_rem = (rem * 10 + digit) % m;
- ans += go(pos + 1, new_rem, new_tight);
- ans %= MOD;
- }
- return dp[pos][rem][tight] = ans;
- }
- bool is_valid(const string& s) {
- int rem = 0;
- for (int i = 0; i < s.length(); ++i) {
- int digit = s[i] - '0';
- if ((i + 1) % 2 == 0) {
- if (digit != d) return false;
- } else {
- if (digit == d) return false;
- }
- rem = (rem * 10 + digit) % m;
- }
- return rem == 0;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- string a, b;
- cin >> m >> d >> a >> b;
- num_str = b;
- memset(dp, -1, sizeof(dp));
- long long ans_b = go(0, 0, true);
- num_str = a;
- memset(dp, -1, sizeof(dp));
- long long ans_a = go(0, 0, true);
- long long ans = (ans_b - ans_a + MOD) % MOD;
- if (is_valid(a)) {
- ans = (ans + 1) % MOD;
- }
- cout << ans << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement