很裸的数位DP,只需要dfs的时候维护dt状态值就行了
dt==2 说明此数字不吉利
dt==1 说明有6没2
dt==0 说明到dex位此数字还是吉利的
代码如下~
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <climits>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int dp[11][8];
int digits[11];
int dfs(int dex,int dt,int p){
int ans=0;
if(dex==0) return dt==2;
if(dp[dex][dt]!=-1&&!p) return dp[dex][dt];
int u=p?digits[dex]:9;
for(int i=0;i<=u;i++){
if(i==4||dt==1&&i==2||dt==2) ans+=dfs(dex-1,2,p&&i==u);
else if(i==6) ans+=dfs(dex-1,1,p&&i==u);
else ans+=dfs(dex-1,0,p&&i==u);
}
if(!p) dp[dex][dt]=ans;
return ans;
}
int digit(int num){
int k=0;
while(num){
digits[++k]=num%10;
num/=10;
}
return dfs(k,0,1);
}
int main(){
#ifdef DID
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
memset(dp,-1,sizeof(dp));
while(scanf("%d%d",&n,&m)==2&&(n||m)){
int k1=digit(m),k2=digit(n-1);
printf("%d\n",m-n+1-(k1-k2));
}
return 0;
}