码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3555 Bomb (数位DP)

时间:2015-01-19 12:53:28      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:acm   c语言   算法   编程   数位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;
}


HDU 3555 Bomb (数位DP)

标签:acm   c语言   算法   编程   数位dp   

原文地址:http://blog.csdn.net/scf0920/article/details/42870573

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!