Bomb

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

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.

 
#include<iostream>
#include<string.h>
#define ll long long
using namespace std;
ll shu[], dp[][];
ll dfs(ll len, bool if4, bool shangxian)
{
if (len == )
return ;
if (!shangxian&&dp[len][if4])
return dp[len][if4];
ll mx, cnt = ;//cnt记录的是区间内不含49的个数
mx = (shangxian ? shu[len] : );
for (ll i = ; i <= mx; i++)
{
if (if4&&i == )//如果shu[len]==4&&上一个状态是9
continue;
cnt = cnt + dfs(len - , i == , shangxian&&i == mx);
}
return shangxian ? cnt : dp[len][if4] = cnt;
}
ll solve(ll n)
{
memset(shu, ,sizeof(shu));
ll k = ;
while (n)//将n的每一位拆解出来逆序存在shu[i]中。eg:109,shu[0]=9,shu[1]=0,shu[2]=1;
{
shu[++k] = n % ;//注意这里是++k
n = n / ;
}
return dfs(k, false, true);
}
int main()
{
ll t;
scanf("%lld", &t);
while (t--)
{
ll n;
scanf("%lld", &n);//这里计算的区间是[0,n],题目要计算的是[1,n];
printf("%lld\n", n-(solve(n)-));
//如果是计算区间[a,b];printf(solve(b)-solve(a-1)); }
return ; }

Bomb(要49)--数位dp的更多相关文章

  1. hdu 3555 Bomb 炸弹(数位DP,入门)

    题意: 给一个数字n,求从1~n中有多少个数是含有49的,比如49,149,1490等都是含49的. 思路: 2^64也顶多是十进制的20多位,那么按十进制位来分析更简单.如果能计算k位十进制数中分别 ...

  2. Bomb HDU - 3555 数位dp

    Code: #include<cstdio> #include<algorithm> #include<cstring> #include<string> ...

  3. Bomb 数位dp

    ---恢复内容开始--- 不能有49 数位dp模板题: #include<bits/stdc++.h> using namespace std; //input by bxd #defin ...

  4. 数位dp介绍

    不了解dp的可以先看一下dp 数位dp含义: 数位:一个数有个位,十位,百位,千位等等,数的每一位都是数位. 数位dp归为计数dp,是在数位上进行操作的dp. 数位dp的实质是一种快速枚举的方式,它满 ...

  5. hdu3555 Bomb (记忆化搜索 数位DP)

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

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

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

  7. HDU 3555 Bomb 数位dp

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

  8. HDU 3555 Bomb(数位DP)

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

  9. Bomb HDU - 3555 (数位DP)

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

随机推荐

  1. Android内核的编译与测试

    1.下载Android内核 source.android.com/source->Downloading and Building Building Kernels 大概要花2个小时,其源码在培 ...

  2. va泛型

    va泛型 什么是泛型? 泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的 ...

  3. linux每天一小步---mkdir命令详解

    1 命令功能 mkdir命令用于创建单个目录或者多级目录,但前提在于用户对于当前目录有写权限. 2 命令语法 mkdir  [选项]  [目录名] 3 命令参数 -m 在创建目录的同时设定目录权限(而 ...

  4. vc++ 不同对话框中传递信息的方法(基于自定义消息SendMessage) (转载)

    转载自:http://blog.csdn.net/myj0513/article/details/6827360 背景: 新建了一个基于对话框的MFC程序,在主对话框中添加tabcontrol控件,又 ...

  5. asp.net部署时加密config文件

    1:运行cmd,并定位到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727(可以直接运行vs2005的命令提示工具,但是貌似vs2010默认指向的framewo ...

  6. 关于SqlServer连接错误

    以前用数据库好好的,今天突然就出现连接错误,贴出出错误消息 出现这种错误的原因:服务里面sqlserver服务没有打开. 解决方案 : 计算机右键,打开管理,找到服务,把服务里面的SQL Server ...

  7. 纸壳CMS3.0中的规则引擎,表达式计算

    纸壳CMS3.0中的规则引擎,用于计算通用表达试结果.通常业务逻辑总是复杂多变的,使用这个规则引擎可以灵活的修改计算表达式. IRuleManager IRuleManager,是使用规则引擎的主要接 ...

  8. UDP通讯

    上一篇有说到TCP通讯,这篇来谈谈UDP通讯方式 基于Udp协议是无连接模式通讯,占用资源少,响应速度快,延时低.至于可靠性,可通过应用层的控制来满足.(不可靠连接) (1).建立一个套接字(Sock ...

  9. mongodb 备份还原

    一.简介 说起来数据库的“备份-还原”,在RDBMS系统中,都有很好的支持,也有很多选项可以设置,功能强大,也能自动完成大部分的备份功能,只要当初设置好了就可以了.对于MongoDB文档型的数据库来说 ...

  10. Substrings(SPOJ8222) (sam(后缀自动机))

    You are given a string \(S\) which consists of 250000 lowercase latin letters at most. We define \(F ...