数位dp第二道~就当成搜索,套板子写了写。我写的dp[pos][pre][state0]记录的是当前pos位没有限制时、前面的数是pre时、前面是否都是0时的方案数。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
int n, m, cnt, a[];
ll dp[][][]; ll dfs(int pos, int pre, int state0, int limit) {
if (!pos) return ;
if (!limit && dp[pos][pre][state0] >= ) return dp[pos][pre][state0]; int high = limit ? a[pos] : ;
ll ret = ;
for (int low = ; low <= high; low++) {
if (abs(low - pre) < && !state0) continue;
ret += dfs(pos - , low, (low == ) & state0, (low == a[pos]) & limit);
} if (!limit) dp[pos][pre][state0] = ret;
return ret;
} ll solve(int x) {
for (cnt = ; x; x /= )
a[++cnt] = x % ;
return dfs(cnt, -, , );
} int main() {
memset(dp, -, sizeof dp);
cin >> n >> m;
cout << solve(m) - solve(n - ) << endl;
return ;
}