1. 程式人生 > >hdu2089 不要62(數位dp模板)

hdu2089 不要62(數位dp模板)

ac程式碼:

#include<iostream>
#include<cstring>
#include<algorithm>
#define r(n) scanf("%d",&n)
#define rll(n) scanf("%lld",&n)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

typedef long long ll;
const int INF=0x3f3f3f3f;
const int N=1e6+1;

int n,m,a[15],num,dp[15][2];
int dfs(int pos,int lim,bool is6){
	if(pos==-1) return 1;
	if(!lim&&dp[pos][is6]) return dp[pos][is6];
	int up=lim?a[pos]:9,sum=0;
	for(int i=0;i<=up;i++){
		if(i==4) continue;
		if(is6&&i==2) continue;
		sum+=dfs(pos-1,lim&&i==up,i==6);
	}
	if(!lim) dp[pos][is6]=sum;
	return sum;
}
int solve(int x){
	num=0;
	while(x){ a[num++]=x%10;x/=10;}
	return dfs(num-1,1,0);
}
int main(){
	while(scanf("%d%d",&n,&m)&&n){
		printf("%d\n",solve(m)-solve(n-1));
	}
	return 0;
}