HDU 3555 Bomb (数位DP)

跟前面数位DP基本一样的,只不过这里是要求包含49,那么可以利用容斥,求出不包含49的,然后再用总数减去就行了。

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=100000000;
const int INF=0x3f3f3f3f;
const double eqs=1e-8;
LL dp[21][11], c[21];
LL dfs(int cnt, int pre, int maxd, int zero)
{
    if(cnt==-1) return 1;
    if(maxd&&zero&&dp[cnt][pre]!=-1) return dp[cnt][pre];
    int i, r;
    LL ans=0;
    r=maxd==0?c[cnt]:9;
    for(i=0;i<=r;i++){
        if(!zero||!(pre==4&&i==9)){
            ans+=dfs(cnt-1,i,maxd||i<r,zero||i);
        }
    }
    if(maxd&&zero) dp[cnt][pre]=ans;
    return ans;
}
LL Cal(LL x)
{
    int i, cnt=0;
    while(x){
        c[cnt++]=x%10;
        x/=10;
    }
    return dfs(cnt-1,-1,0,0);
}
int main()
{
    int t;
    LL n;
    scanf("%d",&t);
    memset(dp,-1,sizeof(dp));
    while(t--){
        scanf("%I64d",&n);
        printf("%I64d\n",n+1-Cal(n));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值