题意:
题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d。
然后求[L, R]里面的多少个数是d-串,且是m的倍数。
题解:
数位dp。
dp[x][y]代表的是余数为x, 然后剩下的长度是y的情况的方案数是多少。
代码:
#include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); #define LL long long #define ULL unsigned LL #define fi first #define se second #define pb push_back #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lch(x) tr[x].son[0] #define rch(x) tr[x].son[1] #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) typedef pair<int,int> pll; const int inf = 0x3f3f3f3f; const int _inf = 0xc0c0c0c0; const LL INF = 0x3f3f3f3f3f3f3f3f; const LL _INF = 0xc0c0c0c0c0c0c0c0; const LL mod = (int)1e9+7; const int N = 2e3 + 100; int dp[N][N]; /// left length char s[N]; int m, d; /// length, yu, lim int dfs(int len, int yu, int lim, int g){ if(!lim && dp[yu][len] != -1) return dp[yu][len]; if(len == 0){ return !yu; } int up = 9; if(lim) up = s[len] - '0'; LL tmp = 0; for(int i = 0; i <= up; ++i){ if(g){ if(i != d) continue; } else { if(i == d) continue; } tmp += dfs(len-1, (yu*10+i)%m, lim && (i==up), g^1); } tmp %= mod; if(!lim) dp[yu][len] = tmp; return tmp; } bool check(int len){ int yu = 0, g = 0; for(int i = len; i >= 1; --i){ if(g && s[i] != '0' + d) return false; if(!g && s[i] == '0' + d) return false; yu = (yu * 10 + s[i] - '0') % m; g ^= 1; } return yu == 0; } int main(){ memset(dp, -1, sizeof(dp)); scanf("%d%d", &m, &d); scanf("%s", s+1); int len1 = strlen(s+1); reverse(s+1, s+1+len1); LL ans = check(len1) - dfs(len1, 0, 1, 0); scanf("%s", s+1); len1 = strlen(s+1); reverse(s+1, s+1+len1); ans += mod + dfs(len1, 0, 1, 0); ans %= mod; cout << ans << endl; return 0; }