思路:
数位的一道入门题
,表示第位,第是否是的结果。
Code:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e3+7; inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; } ll dp[10][2]; int digit[10]; ll dfs(int len,int p1,bool limit) { if(!len) return 1; if(!limit&&dp[len][p1==6]!=-1) return dp[len][p1==6]; int endi=(limit?digit[len]:9); ll ans=0; for(int i=0;i<=endi;++i) { if(i==4||p1==6&&i==2) continue; ans+=dfs(len-1,i,limit&&i==endi); } if(!limit) dp[len][p1==6]=ans; return ans; } ll solve(int n) { int len=0; while(n) { digit[++len]=n%10; n/=10; } return dfs(len,0,true); } int main() { int n,m; memset(dp,-1,sizeof dp); while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) return 0; printf("%lld\n",solve(m)-solve(n-1)); } return 0; }