题目链接:点我
定义 d-数:数字d有且只在偶数位上,奇数位不能出现数字d。(0<=d<=9)
题意:给定一个区间[L, R],问区间里面可以被m整除的d-数个数。
思路:数位dp,dp[i][j]表示处理到第i位%m == j的方案数。套个记忆化外壳就OK了。
10^2000太大了,端点需要特判一下。
AC代码:
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define PI acos(-1.0)
using namespace std;
typedef long long LL;
typedef double DD;
const int MAXN = 1e5+10;
const int MOD = 1e9+7;
int m, d, L;
int bit[2001];
LL dp[2001][2001];
void add(LL &x, LL y) {x = (x + y) % MOD;}
LL DFS(int pos, int preyu, bool yes)
{
if(pos == L) return preyu == 0;
if(!yes && dp[pos][preyu] != -1) return dp[pos][preyu];
LL ans = 0;
int End = yes ? bit[pos] : 9;
for(int i = 0; i <= End; i++)
{
if(pos % 2 == 0 && i == d) continue;
if(pos % 2 == 1 && i != d) continue;
add(ans, DFS(pos+1, (preyu*10+i)%m, yes&&i==End));
}
if(!yes) dp[pos][preyu] = ans;
return ans;
}
LL Count(string str)
{
int len = str.size(); int cnt = 0;
for(int i = 0; i < len; i++) bit[i] = str[i] - '0';
return DFS(0, 0, 1);
}
bool judge(string str)
{
int len = str.size(); int yu = 0;
for(int i = 0; i < len; i++)
{
int v = str[i] - '0';
yu = yu * 10 + v; yu %= m;
if((i + 1) & 1 && v == d) return false;
if(i & 1 && v != d) return false;
}
return yu == 0;
}
int main()
{
CLR(dp, -1);
cin >> m >> d;
string a, b; cin >> a >> b; L = a.size();
//cout << judge(a) << ' ';
cout << (Count(b) - Count(a) + judge(a) + MOD) % MOD << endl;
return 0;
}