1. 程式人生 > >HDU 2089 數位DP

HDU 2089 數位DP

題意:中文題

思路:另外一道數位DP模版題

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=5010;
int dig[20];
ll dp[20][10];
ll dfs(int pos,int pre,int lim){
    if(pos<0) return 1;
    if(!lim&&dp[pos][pre]!=-1) return dp[pos][pre];
    int las=lim?dig[pos]:9;
    ll ret=0;
    for(int i=0;i<=las;i++){
        if(i==4||pre==6&&i==2) continue;
        ret+=dfs(pos-1,i,lim&&(i==las));
    }
    if(!lim) dp[pos][pre]=ret;
    return ret;
}
ll slove(ll n){
    int len=0;
    while(n){
        dig[len++]=n%10;
        n/=10;
    }
    return dfs(len-1,0,1);
}
int main(){
    memset(dp,-1,sizeof(dp));
    ll n,m;
    while(scanf("%I64d%I64d",&n,&m)!=-1){
        if(n==0&&m==0) break;
        printf("%I64d\n",slove(m)-slove(n-1));
    }
    return 0;
}