【题目链接】
【算法】
数位DP,注意处理前导零的情况
【代码】
#include<bits/stdc++.h> using namespace std; #define MAXL 15 int n,m; int dp[MAXL][10],a[MAXL]; template <typename T> inline void read(T &x) { int f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; } for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } template <typename T> inline void write(T x) { if (x < 0) { x = -x; putchar('-'); } if (x > 9) write(x/10); putchar(x%10+'0'); } template <typename T> inline void writeln(T x) { write(x); puts(""); } inline void getdp() { int i,j,k; for (i = 0; i <= 9; i++) dp[1][i] = 1; for (i = 2; i <= MAXL; i++) { for (j = 0; j <= 9; j++) { for (k = 0; k <= 9; k++) { if (abs(j-k) >= 2) dp[i][j] += dp[i-1][k]; } } } } inline int calc(int n) { int i,j,ans = 0; a[0] = 0; while (n) { a[++a[0]] = n % 10; n /= 10; } for (i = 1; i < a[0]; i++) { for (j = 1; j <= 9; j++) { ans += dp[i][j]; } } for (i = a[0]; i >= 1; i--) { for (j = 0; j < a[i]; j++) { if (i == a[0] && !j) continue; if ((i == a[0]) || (abs(j-a[i+1]) >= 2)) ans += dp[i][j]; } if (i != a[0] && abs(a[i]-a[i+1]) < 2) break; } return ans; } int main() { getdp(); read(n); read(m); writeln(calc(m+1)-calc(n)); return 0; }