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

 
思路:
dp[i][0]表示不含49的个数 。
dp[i][1]表示不含49且高位为9的个数;
dp[i][2]表示包括49的个数;
dp[i][0]=10*dp[i-1][0]-dp[i-1][1];  //不含49的数能够随意加10个数字。减去9前面加4的个数
dp[i][1]=dp[i-1][0];                //不含49的数最高位加9
dp[i][2]=dp[i-1][2]*10+dp[i-1][i]; //包括49的数字能够加入0~9,高位为9的能够加4;
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"algorithm"
#include"math.h"
#include"vector"
using namespace std;
#define LL __int64
#define N 25
LL dp[N][3];
void inti()
{
memset(dp,0,sizeof(dp));
dp[0][0]=1;
int i;
for(i=1; i<N; i++)
{
dp[i][0]=dp[i-1][0]*10-dp[i-1][1];
dp[i][1]=dp[i-1][0];
dp[i][2]=dp[i-1][2]*10+dp[i-1][1];
}
}
LL work(LL x)
{
int i,k,flag;
int a[N];
LL ans;
k=0;
while(x)
{
a[++k]=x%10;
x/=10;
}
a[k+1]=ans=flag=0;
for(i=k; i>0; i--)
{
ans+=a[i]*dp[i-1][2];
if(flag)
ans+=a[i]*dp[i-1][0];
else
{
if(a[i]>4)
ans+=dp[i-1][1];
}
if(a[i+1]==4&&a[i]==9)
flag=1;
// printf("%d\n",ans);
}
return ans;
}
int main()
{
int T;
LL n;
inti();
scanf("%d",&T);
while(T--)
{
scanf("%I64d",&n);
printf("%I64d\n",work(n+1));
}
return 0;
}

hdu 3555 Bomb ( 数位DP)的更多相关文章

  1. HDU 3555 Bomb 数位dp

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

  2. HDU 3555 Bomb 数位DP 入门

    给出n,问所有[0,n]区间内的数中,不含有49的数的个数 数位dp,记忆化搜索 dfs(int pos,bool pre,bool flag,bool e) pos:当前要枚举的位置 pre:当前要 ...

  3. HDU - 3555 - Bomb(数位DP)

    链接: https://vjudge.net/problem/HDU-3555 题意: The counter-terrorists found a time bomb in the dust. Bu ...

  4. Bomb HDU - 3555 (数位DP)

    Bomb HDU - 3555 (数位DP) The counter-terrorists found a time bomb in the dust. But this time the terro ...

  5. HDU(3555),数位DP

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

  6. HDU 3555 Bomb (数位DP-记忆化搜索模板)

    题意 求区间[1,n]内含有相邻49的数. 思路 比较简单的按位DP思路.这是第一次学习记忆化搜索式的数位DP,确实比递推形式的更好理解呐,而且也更通用~可以一般化: [数位DP模板总结] int d ...

  7. hud 3555 Bomb 数位dp

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

  8. hdoj 3555 BOMB(数位dp)

    //hdoj 3555 //2013-06-27-16.53 #include <stdio.h> #include <string.h> __int64 dp[21][3], ...

  9. 数位DP入门之hdu 3555 Bomb

    hdu 3555 Bomb 题意: 在1~N(1<=N<=2^63-1)范围内找出含有 ‘49’的数的个数: 与hdu 2089 不要62的区别:2089是找不不含 '4'和 '62'的区 ...

  10. HDU 3555 Bomb(数位DP模板啊两种形式)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Problem Description The counter-terrorists found ...

随机推荐

  1. oracle中

    select tmp_tb.*, ROWNUM row_id from (SELECT MX.*                                          --这里不能直接用* ...

  2. POJ 3177 Redundant Paths (边双连通+缩点)

    <题目链接> <转载于 >>>  > 题目大意: 有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新 ...

  3. hdu 2181 哈密顿绕行世界问题【DFS】

    题目链接 题目大意: Problem Description 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市.    Input ...

  4. Python Pexpect库的简单使用

    Python Pexpect库的使用 简介 最近需要远程操作一个服务器并执行该服务器上的一个python脚本,查到可以使用Pexpect这个库.记录一下. 什么是Pexpect?Pexpect能够产生 ...

  5. Java设计模式从精通到入门五 抽象工厂方法模式

    定义 抽象工厂类为创建一组相关和相互依赖的对象提供一组接口,而无需指定一个具体的类. ​ 这里我得把工厂方法模式得定义拿出来做一下比较:定义一个创建对象的接口,由子类决定实例化哪一个类.工厂方法是一个 ...

  6. mysql(三) 数据表的基本操作操作

    mysql(三) 数据表的基本操作操作 创建表,曾删改查,主键,外键,基本数据类型. 1. 创建表 create table 表名( 列名 类型 是否可以为空, 列名 类型 是否可以为空 )ENGIN ...

  7. VirtualBox 共享文件夹设置及使用方法

    工具:VirtualBox,ubuntu14.04 1.选择要设置共享文件夹的虚拟机,点击设置 点击共享文件夹,新建文件夹,选择路径 2.打开虚拟机,从上方工具栏中找到设备,然后点击安装增强功能(由于 ...

  8. IconFont 图标制作和使用

    一.制作:IcoMoon 这个教程一搜一大把,是很方便快捷的一种方式,提供上传.编辑或者选择IcoMoon-Free下载可以直接拿来用了. 网址:https://icomoon.io/app/ 上传需 ...

  9. BZOJ.1178.[APIO2009]会议中心(贪心 倍增)

    BZOJ 洛谷 \(Description\) 给定\(n\)个区间\([L_i,R_i]\),要选出尽量多的区间,并满足它们互不相交.求最多能选出多少个的区间以及字典序最小的方案. \(n\leq2 ...

  10. docker 与启动后的镜像进行交互

    docker ps docker exec -t -i jenk /bin/bash 在启动时进行交互 docker run -i -t ubuntu:15.10 /bin/bash