hdu 2089 不要62 数位dp

#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
using namespace std;

const int N = 1e6 ;
int dp[10][2] , digit[10];
/*state 标记前缀是否为6*/
int dfs(int len,bool state,bool fp)
{
    if(!len)
        return 1;
    if(!fp && dp[len][state] != -1)
        return dp[len][state];
    int cnt = 0 , fpmax = fp ? digit[len]:9;
    for(int i = 0;i <= fpmax;i++)
    {
        if(i == 4 || state&&i==2)
            continue;
        cnt += dfs(len-1,i==6,fp&&i==fpmax);
    }
    if(!fp)
        dp[len][state] = cnt;
    return cnt;
}
int Solve(int n)
{
    int len = 0;
    while(n)
    {
        digit[++len] = n%10;
        n /= 10;
    }
    return dfs(len,0,true);
}

int main()
{
    memset(dp,-1,sizeof(dp));
    int n,m;
    while(~scanf("%d%d",&n,&m),m+n)
        printf("%d\n",Solve(m)-Solve(n-1));
    return 0;
}

你可能感兴趣的:(动态规划)