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

HDU - 3555 Bomb(数位dp)

时间:2017-07-11 19:28:45      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:tin   print   条件   list   using   tac   abs   stat   hdu   

题意:求1~N中含有49的数字个数。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int digit[30];
LL dp[30][2];
LL dfs(int len, bool state, bool limit){//len--当前位,从高到低枚举,state--上一位的状态,limit--当前位的数字是否有限制
    if(!len) return 1;
    if(!limit && dp[len][state] != -1) return dp[len][state];//!limit--记录重复枚举的数的个数,以便记忆化搜索
    LL ans = 0, up = limit ? digit[len] : 9;
    for(int i = 0; i <= up; ++i){
        if(state && i == 9) continue;
        ans += dfs(len - 1, i == 4, limit && i == up);
    }
    if(!limit) dp[len][state] = ans;//记录截止到当前位且当前位无限制时满足条件的数的个数
    return ans;
}
LL solve(LL x){
    int cnt = 0;
    while(x){
        digit[++cnt] = x % 10;
        x /= 10;
    }
    return dfs(cnt, false, true);
}
int main(){
    int T;
    scanf("%d", &T);
    memset(dp, -1, sizeof dp);
    while(T--){
        LL N;
        scanf("%lld", &N);
        printf("%lld\n", N - (solve(N) - solve(0)));
    }
    return 0;
}

 

HDU - 3555 Bomb(数位dp)

标签:tin   print   条件   list   using   tac   abs   stat   hdu   

原文地址:http://www.cnblogs.com/tyty-Somnuspoppy/p/7151864.html

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