题意:
中文题意。
思路:
数位dp板子题,不过还是要说一下,上限为1时得前一次上限为1并且这次达到了上限。。。
代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 const int N = 20; 6 int dp[N][10]; 7 int a[N]; 8 int dfs(int pos,int pre,bool lim) 9 { 10 if (pos == -1) return 1; 11 if (!lim && dp[pos][pre] != -1) return dp[pos][pre]; 12 int mx = lim ? a[pos] : 9; 13 int res = 0; 14 for (int i = 0;i <= mx;i++) 15 { 16 if (i == 4) continue; 17 if (pre == 6 && i == 2) continue; 18 res += dfs(pos-1,i,i == mx && lim); 19 } 20 if (!lim) return dp[pos][pre] = res; 21 else return res; 22 } 23 int solve(int x) 24 { 25 int pos = 0; 26 while (x) 27 { 28 a[pos++] = x % 10; 29 x /= 10; 30 } 31 return dfs(pos-1,0,1); 32 } 33 int main() 34 { 35 int n,m; 36 memset(dp,-1,sizeof(dp)); 37 while (scanf("%d%d",&n,&m) != EOF) 38 { 39 if (n == 0 && m == 0) break; 40 printf("%d ",solve(m) - solve(n-1)); 41 } 42 return 0; 43 }