挺不错的一道按位DP
/* ID: xinming2 PROG: stall4 LANG: C++ */ #include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <string> #include <stack> #include <cctype> #include <vector> #include <queue> #include <set> #include <utility> #include <cassert> using namespace std; ///#define Online_Judge #define outstars cout << "***********************" << endl; #define clr(a,b) memset(a,b,sizeof(a)) #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1 | 1 #define mk make_pair #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++) #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++) #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--) #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--) const int MAXN = 100000 + 50; const int sigma_size = 26; const long long LLMAX = 0x7fffffffffffffffLL; const long long LLMIN = 0x8000000000000000LL; const int INF = 0x7fffffff; const int IMIN = 0x80000000; #define eps 1e-8 const int MOD = (int)1e9 + 7; typedef long long LL; const double PI = acos(-1.0); typedef pair<int , int> pi; #define Bug(s) cout << "s = " << s << endl; ///#pragma comment(linker, "/STACK:102400000,102400000") int dp[25][15] , digit[25]; int get(int t ,int i , int lastnum) { if(t == 11) { if(i == 0)return 11; return i; } if(abs(i - lastnum) >= 2)return i; return -1; } int dfs(int len , int lastnum ,int t, bool fp)///fp表示是否为首位 { if(t == -1)return 0; if(!len)return 1; if(!fp && t >= 0 && dp[len][t] != -1)return dp[len][t]; int res = 0; int maxi = fp ? digit[len] : 9; for(int i = 0 ; i <= maxi ; i++) { res += dfs(len - 1 , i , get(t , i , lastnum) ,fp && i == maxi); } if(!fp)dp[len][t] = res; return res; } int f(int n) { int len = 0; while(n) { digit[++len] = n % 10; n /= 10; } return dfs(len , 0 , 11 , true); } int main() { int a , b; clr(dp , -1); while(~scanf("%d%d" , &a , &b)) { printf("%d\n" , f(b) - f(a - 1)); } return 0; }