HDU 3555 Bomb 数位DP 入门

给出n,问所有[0,n]区间内的数中,不含有49的数的个数

 

数位dp,记忆化搜索

 

dfs(int pos,bool pre,bool flag,bool e)

pos:当前要枚举的位置

pre:当前要枚举的位置的前面是否为4

flag:枚举当前时,这个数的49时候被算过了

e:当前位置是否可以随便取值

 

dp[pos][pre][flag]

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>

#define ull unsigned long long

using namespace std;

const int maxn=66;

ull dp[maxn][2][2];
int a[maxn];

ull solve(ull );

int main()
{
    memset(dp,-1,sizeof dp);  //先初始化
    int test;
    cin>>test;
    while(test--){
        ull n;
        cin>>n;
        cout<<solve(n)<<endl;
    }
    return 0;
}

ull dfs(int pos,bool pre,bool flag,bool e)
{
    if(!pos)
        return flag;  //不是返回0
    if(e && dp[pos][pre][flag]!=-1)
        return dp[pos][pre][flag];

    int last=e?9:a[pos];
    ull ret=0;
    for(int i=0;i<=last;i++){
        ret+=dfs(pos-1,i==4,flag||(pre&&i==9),e||(!e&&i<last));
    }
    if(e)
        dp[pos][pre][flag]=ret;
    return ret;
}

ull solve(ull n)
{
    int len=0;
    while(n){
        a[++len]=n%10;
        n/=10;
    }
    return dfs(len,0,0,0);
}

 

转载于:https://www.cnblogs.com/-maybe/p/4837857.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值