也是简单的数位dp。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m,dp[10][2],bit[10],ret=0; void get_bit(int x) { ret=0; while (x) {bit[++ret]=x%10;x/=10;} } int dfs(int pos,bool flag,bool flag2) { if (!pos) return 1; if ((!flag2) && (~dp[pos][flag])) return dp[pos][flag]; int ans=0,lim=flag?2:-1,up=flag2?bit[pos]:9; for (int i=0;i<=up;i++) { if (i==4 || i==lim) continue; ans+=dfs(pos-1,(i==6),flag2&&i==up); } if (!flag2) dp[pos][flag]=ans; return ans; } int ask(int x) { get_bit(x); return dfs(ret,0,1); } int main() { memset(dp,-1,sizeof(dp)); for (;;) { scanf("%d%d",&n,&m); if ((!n) && (!m)) break; printf("%d ",ask(m)-ask(n-1)); } return 0; }