zoukankan      html  css  js  c++  java
  • 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|1
    using 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
    ",(b-a+1)-(solve(b)-solve(a-1)));
        }
    }
    View Code
  • 相关阅读:
    2013-3 阿里性能稳定性沙龙
    8种Nosql数据库系统对比
    百度技术笔记之2013-1
    百度技术沙龙之2013-2&3
    【消息队列MQ】各类MQ比较
    Unity3D 游戏引擎之C#使用Socket与HTTP连接server数据传输包
    android的ndk学习(1)
    杭电 3555 Bomb
    FaceBook开源库Fresco
    SDUTOJ 2476Period
  • 原文地址:https://www.cnblogs.com/lienus/p/4248964.html
Copyright © 2011-2022 走看看