AC_Code:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <cmath> 6 #include <queue> 7 #include <stack> 8 #include <vector> 9 #include <set> 10 #include <map> 11 #include <algorithm> 12 using namespace std; 13 typedef long long ll; 14 const int maxn=10; 15 16 int dp[maxn][maxn][2]; 17 int a[maxn]; 18 int len; 19 20 int dfs(int pos, int pre, int status, bool lead, bool limit){ 21 if( pos<1 ) return status; 22 if((!limit)&&(!lead)&&dp[pos][pre][status]!=-1) return dp[pos][pre][status]; 23 int up=limit?a[pos]:9; 24 int ans=0; 25 for(int i=0;i<=up;i++){ 26 if( pre==6&&i==2 ) continue; 27 else if( i==4 ) continue; 28 ans += dfs(pos-1,i,1,(lead&&(!i)),limit&&(i==up)); 29 } 30 return ((!limit)&&(!lead))?dp[pos][pre][status]=ans:ans; 31 } 32 33 int part(int x){ 34 len=0; 35 while(x) a[++len]=x%10,x/=10; 36 memset(dp,-1,sizeof(dp)); 37 return dfs(len,-1,1,1,1); 38 } 39 40 int main() 41 { 42 int n,m; 43 while(~scanf("%d%d",&n,&m)){ 44 if( n==0&&m==0 ) break; 45 else if( n==0 ) printf("%d\n",part(m)-part(n)+1); 46 else printf("%d\n",(part(m)-part(n-1))); 47 } 48 return 0; 49 }
原文地址:https://www.cnblogs.com/wsy107316/p/12250028.html
时间: 2024-10-21 04:16:37