链接:点击打开链接
题意:求N~M中不含62并且不含4的数有多少(6和2必须相连,61152不满足含62这种情况,11562含62);
代码:
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int siz=50;
int a[siz];
long long dp[siz][siz];
long long dfs(int pos,int pre,int lim){
int i,u;
long long ans=0;
if(pos==0)
return 1;
if(dp[pos][pre]!=-1&&lim==0)
return dp[pos][pre];
u=lim?a[pos]:9;
for(i=0;i<=u;i++){
if(i==4||(pre==6&&i==2)) //除掉非法状态
continue; //dp[i][j]表示剩下i位,前一位为j的种数
ans+=dfs(pos-1,i,lim&(i==u));
}
if(lim==0)
dp[pos][pre]=ans;
return ans;
}
long long cal(long long x){
int pos=0;
while(x){
a[++pos]=x%10;
x/=10;
}
return dfs(pos,0,1);
}
int main(){
long long l,r;
memset(dp,-1,sizeof(dp));
while(scanf("%I64d%I64d",&l,&r)!=EOF){
if(l==0&&r==0)
break;
printf("%I64d\n",cal(r)-cal(l-1));
}
return 0;
}