数位动态规划 
    数位动态规划是求解一个大区间[L, R]中间满足条件Q的所有数字的个数(或者和,或其他)的一种方法。它通过分析每一位上的数字,一般用 dp[len][digit][...] 来表示状态“len位长的数字,最高位数字为digit所具有的xx特性”,利用记忆化搜索保存中间结果,从而加快求解速度。 
    通过求 f(n) 从0到n中满足条件Q的数字的个数,则所求的结果为 f(R) - f(L-1).

题目大意 
    给定数字n,找出从0到n中满足条件“数字k中有49(4和9连续)存在”的数字的个数。

题目分析 
    直接数位dp即可。

实现

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
ll dp[30][10]; //dp[len][digit]表示长度为len,且最高位为digit的满足条件Q的数字个数
ll base[30];
int bits[30];
ll Dfs(int len, int digit, bool end_flag, ll n){
if (len <= 1)
return 0;
if (!end_flag && dp[len][digit] != -1)
return dp[len][digit]; ll ans = 0;
int end = end_flag ? bits[len - 2] : 9;
for (int i = 0; i <= end; i++){
if (digit == 4 && i == 9){
if (end_flag)
ans += (1 + n % base[len - 2]);
else
ans += base[len - 2];
}else
ans += Dfs(len - 1, i, end_flag && (i == end), n);
}
if (!end_flag)
dp[len][digit] = ans;
return ans;
}
int Init(ll n){
memset(bits, 0, sizeof(bits));
int k = 0;
base[0] = 1;
while (n){
bits[k++] = n % 10;
base[k] = base[k - 1] * 10;
n /= 10;
}
return k;
}
ll Solve(ll n){
int len = Init(n);
return Dfs(len + 1, 0, true, n);
}
int main(){
int T;
ll num;
scanf("%d", &T);
memset(dp, -1, sizeof(dp));
while (T--){
scanf("%I64d", &num);
ll ret = Solve(num);
printf("%I64d\n", ret);
}
return 0;
}

在数据范围较大的时候,使用 long long int类型,但要注意所有使用long long int类型的变量被调用的函数中,参数形式均保持一致为long long int。

hdu_3555 bomb的更多相关文章

  1. HDU3555 Bomb[数位DP]

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

  2. Leetcode: Bomb Enemy

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

  3. HDU 5934 Bomb(炸弹)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  4. hdu 3622 Bomb Game(二分+2-SAT)

    Bomb Game Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. Bomb

    Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impro ...

  6. CF 363B One Bomb(枚举)

    题目链接: 传送门 One Bomb time limit per test:1 second     memory limit per test:256 megabytes Description ...

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

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

  8. [HDU3555]Bomb

    [HDU3555]Bomb 试题描述 The counter-terrorists found a time bomb in the dust. But this time the terrorist ...

  9. hdu 5934 Bomb

    Bomb Problem Description There are N bombs needing exploding.Each bomb has three attributes: explodi ...

随机推荐

  1. CentOS6 启动流程图文解剖

    我们在使用Linux操作系统的时候,我们只需按下电源键,等待,然后输入账户和密码就可以使用Linux操作系统了.那么在按下电源到输入账号和密码之前,操作系统都做了些什么?下面就来讲述在这段时间发生的动 ...

  2. Java过滤器应用-对Ajax请求做Session失效判断

    过滤器常用来对Session过期做判断 Layout.js 1.为ajax请求添加标识 2.无论ajax请求成功与否,complete函数终会执行 // 全局Ajax设置, 用于session过期后的 ...

  3. java中的构造函数

    在c++中就学习了构造函数,今天学习java又碰到了构造函数,重新写一篇博客来理解一下 其实直接听这个词并不能理解这是什么,但其实看了它的作用,就很好理解了 当创建一个对象时,往往需要做一些初始化工作 ...

  4. 如何重置CentOS/RHEL 7中遗忘的根用户帐户密码

    你有没有遇到过这种情况:想不起来Linux系统上的用户帐户密码?要是你忘了根用户密码,情况就更为糟糕.你无法执行任何面向整个系统的变更.要是你忘了用户密码,很容易使用根帐户来重置密码. 可要是你忘了根 ...

  5. UE4编程之C++创建一个FPS工程(二)角色网格、动画、HUD、子弹类

    转自:http://blog.csdn.net/u011707076/article/details/44243103 紧接上回,本篇文章将和大家一同整理总结UE4关于角色网格.动画.子弹类和HUD的 ...

  6. dev RichText高亮

    需要引用的DLL DevExpress.CodeParser DevExpress.Office DevExpress.RichEdit DevExpress.XtraRichEdit   MySyn ...

  7. Refresh / Updating a form screen in Oracle D2k Forms 6i

    Refresh / Updating a form screen in Oracle D2k Forms 6i ProblemYou want to show number of records pr ...

  8. CVE-2015-1328(本地提权漏洞)

    /* # Exploit Title: ofs.c - overlayfs local root in ubuntu # Date: 2015-06-15 # Exploit Author: rebe ...

  9. Timeout expired超时时间已到. 达到了最大池大小 错误及Max Pool Size设置

    此文章非原创,仅为分享.学习!!! 参考数据库链接串: <add key="data" value="server=192.168.1.123; port=3306 ...

  10. Linux的查找命令

    1. 文件搜索find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find [搜索路径] [搜索条件][搜索文件名] 如果什么参数也不加,fi ...