HDU 3555 Bomb

数位DP

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <string>
using namespace std;

long long dp[25][25];
int p[25];
int tot;
long long a;

long long f(long long x)
{
    tot=0;
    while(x)
    {
        p[tot++]=(int)(x%10);
        x=x/10;
    }
    for(int i=0; i<tot/2; i++) swap(p[i],p[tot-i-1]);

    long long res=0;

    for(int i=0;i<tot;i++)
    {
        for(int j=0;j<p[i];j++)
        {
            if(p[i-1]==4&&j==9&&i>=1) continue;
            res=res+dp[tot-i][j];
        }
        if(p[i-1]==4&&p[i]==9&&i-1>=0) break;
    }

    return res;
}

void init()
{
    memset(dp,0,sizeof dp);
    for(int i=0;i<=9;i++) dp[1][i]=1;
    for(int i=2;i<=20;i++)
    {
        for(int j=0;j<=9;j++)
        {
            long long sum=0;
            for(int k=0;k<=9;k++)
            {
                if(j==4&&k==9) continue;
                else sum=sum+dp[i-1][k];
            }
            dp[i][j]=sum;
        }
    }
}

int main()
{
    init();
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%lld",&a);
        a++;
        printf("%lld\n",a-f(a));
    }
    return 0;
}
时间: 2024-10-10 06:22:03

HDU 3555 Bomb的相关文章

hdu 3555 Bomb(数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意:就是给你一个数n,判断从0到n有多少个数含有数字49...... 是不是觉得跟hdu2089很相似呀... 思路:跟hdu2089一样的,注意给出的数比较大,所以这儿用__int64  .... code: #include<cstdio> #include<iostream> #include<cstring> #include<algorithm&

[ACM] hdu 3555 Bomb (数位DP,统计1-N中含有“49”的总数)

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

HDU - 3555 Bomb (数位DP)

题意:求1-n里有多少人包含"49"的数字 思路:数位DP,分三种情况:到第i位没有49的情况,到第i位没有49且最高位是9的情况,到第i位有49的情况,将三种情况都考虑进去就是了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; long long dp[30][3], n; int arr

HDU 3555——Bomb

HDU 3555——Bomb 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题意:让你找找不大于n的数里有多少含‘49’, 我们发现,T很大,N很大,暴力指定不行,因为含不含'49'是每一位的性质,可以考虑数位dp,(况且这就是数位dp的模板形式啊喂),因为在模板的基础改的,所以找的是1-n有多少个数不含49,总数减一下就行 dp状态记为dp[当前是第几位][前一位是不是4],所以第二维开到2就够用啦 模板题,上代码 1 #include<

HDU 3555 Bomb (数位DP)

数位dp,主要用来解决统计满足某类特殊关系或有某些特点的区间内的数的个数,它是按位来进行计数统计的,可以保存子状态,速度较快.数位dp做多了后,套路基本上都差不多,关键把要保存的状态给抽象出来,保存下来. 简介: 顾名思义,所谓的数位DP就是按照数字的个,十,百,千--位数进行的DP.数位DP的题目有着非常明显的性质: 询问[l,r]的区间内,有多少的数字满足某个性质 做法根据前缀和的思想,求出[0,l-1]和[0,r]中满足性质的数的个数,然后相减即可. 算法核心: 关于数位DP,貌似写法还是

HDU 3555 Bomb(数位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 curre

hdu 3555 Bomb 【数位DP】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题意:上一题是不要62 这个是"不要49" 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <functio

HDU 3555 —— Bomb

Bomb 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 number sequence includes the sub-sequence "4

hdu 3555 Bomb ( 数位DP)

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

(数位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