hdu3555 Bomb(数位dp)

题目传送门

Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 23059    Accepted Submission(s): 8673

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 "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?

Input

The 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.

Output

For 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.

Author

[email protected]

Source

2010 ACM-ICPC Multi-University Training Contest(12)——Host by WHU

代码:

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mod 1000000007
ll n,m;
ll dp[25][3];
int bit[25];
void init()
{
    //dp初始值
    dp[0][0]=1;
    dp[0][1]=dp[0][2]=0;
    for(int i=1;i<=20;i++)
    {
        dp[i][0]=dp[i-1][0]*10-dp[i-1][1];//除去49
        dp[i][1]=dp[i-1][0];//首位为9
        dp[i][2]=dp[i-1][2]*10+dp[i-1][1];//
    }
}
ll solve(ll a)
{
    int len=0;
    while(a!=0)
    {
        bit[++len]=a%10;
        a/=10;
    }
    bit[len+1]=0;//防止越位
    ll ans=0;
    bool flag=false;
    for(int i=len;i>=1;i--)
    {
        ans+=dp[i-1][2]*bit[i];
        if(flag) ans+=dp[i-1][0]*bit[i];
        if(!flag&&bit[i]>4) ans+=dp[i-1][1];
        if(bit[i+1]==4&&bit[i]==9)flag=true;
    }
    if(flag)ans++;//加上a
    return ans;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    init();
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n;
        cout<<solve(n)<<endl;
    }
    return 0;
}

Recommend

zhouzeyong   |   We have carefully selected several similar problems for you:  3554 3556 3557 3558 3559

原文地址:https://www.cnblogs.com/zhgyki/p/9527145.html

时间: 2024-10-23 05:51:56

hdu3555 Bomb(数位dp)的相关文章

hdu---(3555)Bomb(数位dp(入门))

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

hdu3555 Bomb(数位dp)

Bomb                                                                                     Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)                                                                             

hdu3555 Bomb (数位dp入门题)

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

HDU3555 Bomb 数位DP第一题

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

[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

hud 3555 Bomb 数位dp

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

HDU 3555 Bomb (数位DP)

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

HDU 3555 Bomb(数位DP)

Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets

hdu3555(数位dp)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题意:求区间[a,b]内包含有'49'的数的总个数. 分析:dp[pos][0]表示到第pos位没有包含49,后面1~pos-1位任意填时的总个数,dp[pos][1]表示到第pos位时前一位刚好是'4',后面任意填时的总个数,dp[pos][2]表示已经包含49后面任意填的总个数... #include <cstdio> #include <cstring> #include