1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #include<bits/stdc++.h> using namespace std; #define ll long long int a[20]; int dp[20][2]; int dfs(int pos,int pre,int sta,bool limit)
{ if(pos==-1)return 1; if(!limit&&dp[pos][sta]!=-1)return dp[pos][sta]; int up = limit?a[pos]:9; int tmp = 0; for(int i=0;i<=up;i++) { if(pre==6&&i==2)continue; if(i==4)continue; tmp += dfs(pos-1,i,i==6,limit&&i==a[pos]); } if(!limit)dp[pos][sta]=tmp; return tmp; } int solve(int x) { int pos = 0; while(x) { a[pos++]=x%10; x/=10; } return dfs(pos-1,-1,0,true); } int main() { int n,m; while(~scanf("%d %d",&n,&m)&&(n||m)) { memset(dp,-1,sizeof(dp)); printf("%d\n",solve(m)-solve(n-1)); } return 0; }
|