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

代码(dfs解法):

#include<iostream>
#include<cstring>
#define LL long long
using namespace std;

LL bit[25], dp[25][2];

LL dfs(int pos, int is4, int lim)
{
    if(pos<0) return 1;
    if(!lim && dp[pos][is4]!=-1) return dp[pos][is4];
    int las=lim?bit[pos]:9;
    LL res=0;
    for(int i=0; i<=las; ++i)
        if(!(is4 && i==9))
            res+=dfs(pos-1, i==4, lim&&i==las);
    if(!lim) dp[pos][is4]=res;
    return res;
}

int main()
{
    int T;
    memset(dp, -1, sizeof(dp));
    cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        LL len=0, m=n;
        while(n)
        {
            bit[len++]=n%10;
            n/=10;
        }
        LL ans=m-dfs(len-1, 0, 1)+1;
        cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-10-13 01:05:51

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

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

数位DP入门题 hdu 2089 hdu 3555

hdu 2089 不要62 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:对于每次给出的一个区间,找出区间内多少个数字没有出现62和4. 思路: 数位DP入门题,对于dfs设置3个参数. 一个表示现在进行到第几位, 一个表示前一个标记状态,这里表示上一位是不是6. 一个表示是否现在是这位可以取到的最大的数字. 如果上一位是6,那么这位不可以取2.且当前位都不可以取4. 1 #include <bits/stdc++.h> 2 us

hdu3555 Bomb(数位dp)

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

HDU 2089 不要62【数位DP入门题】

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

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)Total Submission(s): 23059    Accepted Submission(s): 8673 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terr

xbz分组题B 吉利数字 数位dp入门

B吉利数字时限:1s [题目描述]算卦大湿biboyouyun最近得出一个神奇的结论,如果一个数字,它的各个数位相加能够被10整除,则称它为吉利数.现在叫你计算某个区间内有多少个吉利数字. [输入]第一行为样例个数N.接下来N行,每一行代表一个输入样例,每个输入样例有2个数,分别代表某个区间的起点a和终点b.注意所求区间为[a,b],1<=a<=b<=10^9 [输出]N行.对于第x个输入样例,在第x行输入该样例所对应的结果. [输入样例]21 101 20 [输出样例]01 [Hint

[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 5001 walk 概率dp入门题

Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel t