题目传送门

Bomb

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

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
fatboy_cw@WHU
 
Source
 
Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:  3554 3556 3557 3558 3559 
题意:给你n,从[1,n]中找出含有“49”的数的个数
题解:数位dp入门题
代码:
第一份是间接法做的
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> PII;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
ll n;
int bit[];
ll dp[][];
ll dfs(int pos,int pre,int sta,bool limit)
{
if(pos==-)return ;
if(!limit&&dp[pos][sta]!=-)return dp[pos][sta];
int up=limit?bit[pos]:;
ll ans=;
for(int i=;i<=up;i++)
{
if(pre==&&i==)
continue;
ans+=dfs(pos-,i,i==,limit&&i==bit[pos]);
}
if(!limit)dp[pos][sta]=ans;
return ans;
}
ll solve(ll x)
{
int len=;
while(x)
{
bit[len++]=x%;
x/=;
}
return dfs(len-,-,,true);
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%lld",&n);
memset(dp,-,sizeof(dp));
printf("%lld\n",n+-solve(n));
}
return ;
}

下面的是直接法做的:

pre=0: 没有49; pre=1: 前一位为4; pre=2: 前几位中有49

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> PII;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
ll n;
int bit[];
ll dp[][];
ll dfs(int pos,int pre,bool limit)
{
if(pos==-)return pre==;
if(!limit&&dp[pos][pre]!=-)return dp[pos][pre];
int up=limit?bit[pos]:;
ll ans=;
for(int i=;i<=up;i++)
{
if(pre==||pre==&&i==)
ans+=dfs(pos-,,limit&&i==bit[pos]);
else if(i==)
ans+=dfs(pos-,,limit&&i==bit[pos]);
else
ans+=dfs(pos-,,limit&&i==bit[pos]);
}
if(!limit)dp[pos][pre]=ans;
return ans;
}
ll solve(ll x)
{
int len=;
while(x)
{
bit[len++]=x%;
x/=;
}
return dfs(len-,,true);
}
int main()
{
int T;
scanf("%d",&T);
memset(dp,-,sizeof(dp));
while(T--){
scanf("%lld",&n);
printf("%lld\n",solve(n));
}
return ;
}
 

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

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

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

  2. HDU3555 Bomb —— 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    M ...

  3. hdu3555 Bomb 数位DP入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 简单的数位DP入门题目 思路和hdu2089基本一样 直接贴代码了,代码里有详细的注释 代码: ...

  4. HDU3555 Bomb[数位DP]

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

  5. HDU3555 Bomb 数位DP第一题

    The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the ti ...

  6. hdu3555 Bomb (数位dp入门题)

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

  7. 【hdu3555】Bomb 数位dp

    题目描述 求 1~N 内包含数位串 “49” 的数的个数. 输入 The first line of input consists of an integer T (1 <= T <= 1 ...

  8. HDU 3555 Bomb 数位dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Mem ...

  9. hud 3555 Bomb 数位dp

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

随机推荐

  1. 攻防世界--getit

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/8ef2f7ef55c240418f84b3c514a7a28a 准备 得知 64位文件 ...

  2. vue 中使用 watch 出现了如下的报错

    vue 中使用 watch 出现了如下的报错 报错: Method "watch" has type "object" in the component def ...

  3. printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - 输出格式转换

    总览 (SYNOPSIS) #include <stdio.h> int printf(const char *format, ...); int fprintf(FILE *stream ...

  4. python指定cpu使用率,与内存占用率

    python指定cpu使用率,与内存占用率 """ runing.py -c 2 -t 0.01 -m 1000 -c cpu核数,不加-c参数为最大核数 -t cpu运 ...

  5. LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现

    题目描述: python实现 Search in Rotated Sorted Array 搜索旋转排序数组   中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1 ...

  6. java 计算时间差

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date d1 = df.parse(&quo ...

  7. MySQL MHA之 master_ip_failover.sh脚本

    master_ip_failover.sh脚本是用perl编写的,可以在mha-manager源码包中可以找到,下面给出的结合keepalived进行自动切换的脚本: [root@mha script ...

  8. MySQL不支持的特性

    MySQL 1.不支持物化视图. 2.不支持位图索引. 3.不支持并行查询. 4.不支持哈希关联,MySQL的所有关联都是嵌套循环关联.不过,可以通过建立一个哈希索引来曲线实现. 5.不允许对同一表同 ...

  9. python方法参数:*和**操作符

    * def test_args_kwargs(arg1, arg2, arg3): print("arg1:", arg1) print("arg2:", ar ...

  10. Ubuntu分区小知识与分区方案

    Most PC operating systems still work with an ancient disk partition scheme that historically makes d ...