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

HDU 3555 数位DP

时间:2018-01-31 14:46:16      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:cas   state   type   long   rip   namespace   ++   cat   help   

The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence "49", the power of the blast would add one point.
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?

InputThe first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.

The input terminates by end of file marker.
OutputFor each test case, output an integer indicating the final points of the power.Sample Input
3
1
50
500
Sample Output
0
1
15


        
 
Hint
From 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499",
so the answer is 15.

题意:
问小于等于n的数字中包含着49的数字有多少个。
做法:
数位dp板子题目,统计有多少个不包含49的,减去就好
代码:
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<cstdlib>
typedef long long ll;
const int state = 2;
int a[25];
ll dp[25][state];
ll dfs(int pos,int pre,bool limit){
    if(pos == -1)
        return 1;//深度
    int up;//枚举上界
    if(!limit&&dp[pos][pre]!=-1)//记忆化
        return dp[pos][pre];
    up = limit?a[pos]:9;
    ll ans = 0;
    for(int i=0;i<=up;i++){
        if(pre==1&&i==9)
            continue;
        ans+=dfs(pos-1,i==4,limit&&i==a[pos]);
    }
    if(!limit)//有选择地记忆化
    return dp[pos][pre]=ans;
    else
    return ans;
}
ll solve(ll x){
    int pos = 0;
    memset(a,0,sizeof(a));
    while(x){
        a[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,0,true);
}
int main(){
    ll p,q;
    memset(dp,-1,sizeof(dp));
    int t;
    scanf("%d",&t);
    while(t--){
        ll s;
        scanf("%lld",&s);
        p=solve(s);
        q = s - p + 1;
        printf("%lld\n",q);
    }
    return 0;
}

 这篇博客不错,学习数位DP的童鞋可以看一下:

http://blog.csdn.net/wust_zzwh/article/details/52100392

HDU 3555 数位DP

标签:cas   state   type   long   rip   namespace   ++   cat   help   

原文地址:https://www.cnblogs.com/xfww/p/8391219.html

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