首页 > 代码库 > hdu2089(数位dp)

hdu2089(数位dp)

 

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2089

题意:求区间[a,b]内不含有62或4的数的个数。

分析:数位dp,dp[pos][0]表示到第pos位还没含有62或4,dp[pos][1]表示到第pos位前一位是6,dp[pos][2]表示已包含4或62.

技术分享
#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <iostream>#include <algorithm>#include <queue>#include <cstdlib>#include <stack>#include <vector>#include <set>#include <map>#define LL long long#define mod 10007#define inf 0x3f3f3f3f#define N 100010#define FILL(a,b) (memset(a,b,sizeof(a)))#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int dp[10][3];int dig[22];int dfs(int pos,int pre,int flag,int limit){    if(!pos)return flag;    if(!limit&&flag&&~dp[pos][2])return dp[pos][2];    if(!limit&&!flag&&pre==6&&~dp[pos][1])return dp[pos][1];    if(!limit&&!flag&&pre!=6&&~dp[pos][0])return dp[pos][0];    int len=limit?dig[pos]:9;    int ans=0;    for(int i=0;i<=len;i++)    {        ans+=dfs(pos-1,i,flag||i==4||pre==6&&i==2,limit&&i==len);    }    if(!limit)    {        if(flag)dp[pos][2]=ans;        else if(pre==6)dp[pos][1]=ans;        else dp[pos][0]=ans;    }    return ans;}int solve(int x){    int len=0;    while(x)    {        dig[++len]=x%10;        x/=10;    }    return dfs(len,0,0,1);;}int main(){    int a,b;    while(scanf("%d%d",&a,&b)>0)    {        if(a+b==0)break;        memset(dp,-1,sizeof(dp));        printf("%d\n",(b-a+1)-(solve(b)-solve(a-1)));    }}
View Code

 

hdu2089(数位dp)