HDU3555 区间的数里面有49的个数(数位dp)

题目:区间的数里面有49的个数

分析:

dp[pos][0]:长度为pos的数中,不包含49的,前一位不为4的有多少个;
dp[pos][1]:长度为pos的数中,不包含49的,前一位为4的有多少个;
dp[pos][2]:长度为pos的数中,包含49的有多少个;

一开始我是打算先算不满足的,在算满足的;结果不行,可能这样算出来的数太大了所以不行吧;

通过这道题可以更加理解了(不要62)这个题目的sta 的表示 , 原来如此呀

#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
int dig[20];
ll dp[20][3];
/*
dp[pos][0]:长度为pos的数中,不包含49的,前一位不为4的有多少个;
dp[pos][1]:长度为pos的数中,不包含49的,前一位为4的有多少个;
dp[pos][2]:长度为pos的数中,包含49的有多少个;
*/
ll dfs(int pos,int pre,bool have49,bool limit)
{
    if(pos==0) return have49;
    if(!limit)//首先满足没有上界限制
    {
        if(have49 && ~dp[pos][2]) return dp[pos][2];
        if(!have49 && pre==4 && ~dp[pos][1]) return dp[pos][1];
        if(!have49 && pre!=4 && ~dp[pos][0]) return dp[pos][0];
    }

    int up=limit?dig[pos]:9;
    ll ans=0;
    for(int i=0;i<=up;i++)
    {
        if(pre==4 && i==9) ans+=dfs(pos-1,i,1,limit && i==up);
        else ans+=dfs(pos-1,i,have49,limit && i==up);
    }

    if(!limit)
    {
        if(have49) dp[pos][2]=ans;
        else
        {
            if(pre==4) dp[pos][1]=ans;
            else dp[pos][0]=ans;
        }
    }

    return ans;
}
ll solve(ll x)
{
    int pos=0;
    while(x)
    {
        dig[++pos]=x%10;
        x/=10;
    }
    return dfs(pos,0,0,1);
}

int main()
{
    int T;
    ll N;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%I64d",&N);
        memset(dp,-1,sizeof(dp));
        printf("%I64d\n",solve(N));
    }
}

原文地址:https://www.cnblogs.com/shuaihui520/p/9930078.html

时间: 2024-08-29 01:40:50

HDU3555 区间的数里面有49的个数(数位dp)的相关文章

hdu3709(求区间内平衡数的个数)数位dp

题意:题中平衡数的定义: 以一个位置作为平衡轴,然后左右其他数字本身大小作为重量,到平衡轴的距离作为全职,实现左右平衡(即杠杆原理平衡).然后为区间[x,y]内平衡数的个数. (0 ≤ x ≤ y ≤ 1018) 解法:数位dp.如果一个数的平衡数,那么它的平衡轴位置是确定的.原来一直尝试数位dp在dfs时候列举平衡轴的位置,后来才意识到可以提前枚举平衡轴位置,然后再dfs,这样比较好写.dp[mid][pre][wei];表示对称轴是mid,计算第pre个位置以后需要力矩大小wei的数的个数.

[数位dp] kuangbinoj 1012 bin巨的数

题意: 作为ACM史上年度重量级人物,bin巨目前已经掌握了史上最NB的数,群巨快来仰慕!!我们定义这样一个数,它里面的每一个数字都是成双成对出现 的,but,如果这个数里面存在0那么这也是NB的数,比如11,122122,12035,当然,需要剔除那些首位是0的数.我们的目标就是计算一个区 间内bin巨有多少NB数! 思路: 简单的数位dp,需要考虑每个数字出现的次数(0,1),是否有前导0,是否出现了0. 这里需要注意的就是0不是一个bin巨数. #include"cstdlib"

(数位DP 1.2)hdu 3555 Bomb(统计1~n中,包含49的数的个数)

题目: Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 9273    Accepted Submission(s): 3275 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrori

hdu 5919 主席树(区间不同数的个数 + 区间第k大)

Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 849    Accepted Submission(s): 204 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,?

(数位DP 1.1)hdu 2089 不要62(求一个区间[a,b]中不包含4,64的数的个数)

题目: 不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 23759    Accepted Submission(s): 8128 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来

windy数(简单数位DP)

1026: [SCOI2009]windy数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 6306  Solved: 2810[Submit][Status][Discuss] Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? Input 包含两个整数,A B. Output 一个整数 Sample I

hdu3555 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=3555 Problem Description 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 nu

【数位dp】hdu3555 Bomb

题意就是找0到n有多少个数中含有49.数据范围接近10^20 DP的状态是2维的dp[len][3]dp[len][0] 代表长度为len不含49的方案数dp[len][1] 代表长度为len不含49但是以9开头的数字的方案数dp[len][2] 代表长度为len含有49的方案数 状态转移如下dp[i][0] = dp[i-1][0] * 10 - dp[i-1][1];  // not include 49  如果不含49且,在前面可以填上0-9 但是要减去dp[i-1][1] 因为4会和9构

hdu3555 Bomb (记忆化搜索 数位DP)

http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7316    Accepted Submission(s): 2551 Problem Description The counter-terrorists found a time