正难则反,考虑统计[l,r] 非萌串,用总数扣去这部分得到答案。
非萌串在递推时只要当前数字和前两位不相同即可,但是要额外注意有前导0的情况,因为前导0是可以相等的,并且010这种也是非萌串。
考虑前导0后写出了6维的傻逼DP
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
const int mod = 1e9 + 7;
typedef long long ll;
char L[maxn],R[maxn],s[maxn];
ll dp[maxn][15][15][2][2][2];
ll lc = 0,rc = 0,cnt = 0;
int l,r,len;
void sub() {
for(int i = 0; i < l; i++) {
if(L[i] != '0') {
L[i] = L[i] - 1;
if(i == l - 1 && L[i] == '0') {
L[l - 1] = 0;
l--;
}
break;
}
else L[i] = '9';
}
}
ll dfs(int cur,int pre1,int pre2,int p1,int p2,int limit) {
if(cur < 0)
return 1;
if(dp[cur][pre1][pre2][p1][p2][limit] != -1) return dp[cur][pre1][pre2][p1][p2][limit];
ll &ans = dp[cur][pre1][pre2][p1][p2][limit] = 0;
int up = limit ? s[cur] - '0' : 9;
for(int i = 0; i <= up; i++) {
if(i == 0) {
if(!p1) ans += dfs(cur - 1,pre2,i,p2,p2 || i,limit && i == up);
else {
if(i == pre1 || i == pre2) continue;
ans += dfs(cur - 1,pre2,i,p2,p2 || i,limit && i == up);
ans %= mod;
}
continue;
}
if(i == pre1 || i == pre2) continue;
ans += dfs(cur - 1,pre2,i,p2,p2 || i,limit && i == up);
ans %= mod;
}
return ans;
}
ll solve(char t[]) {
strcpy(s,t);
memset(dp,-1,sizeof dp);
len = strlen(t);
return dfs(len - 1,0,0,0,0,1);
}
int main() {
scanf("%s%s",L,R);
l = strlen(L);
r = strlen(R);
for(int i = 0; i < l; i++)
lc = lc * 10 % mod + L[i] - '0';
for(int i = 0; i < r; i++)
rc = rc * 10 % mod + R[i] - '0';
reverse(L,L + l);
reverse(R,R + r);
sub();
lc--;
printf("%lld\n",((rc + mod - lc) + mod - solve(R) + solve(L)) % mod);
return 0;
}