数位动态规划 
    数位动态规划是求解一个大区间[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. 使用C#写windows服务

    首先,创建一个windows服务项目

  2. [Django_1_0]初次见面

    Django 初次见面 文章将写安装和第一次使用时候的操作.文章是照着文档做的,但是以后的内容会有不一样. 安装 pip install django 我这里是使用python3的,也可以使用 pip ...

  3. VC++打开对话框选择一个文件夹路径 BROWSEINFO结构

    typedef struct _browseinfoW { HWND hwndOwner; PCIDLIST_ABSOLUTE pidlRoot; LPWSTR pszDisplayName; // ...

  4. JavaScript的循环语句

    JavaScript的循环语句 1.JavaScript的循环语句 (1)for循环语句 - 循环代码块一定的次数: (2)for/in循环语句 - 循环遍历对象的属性: (3)while循环语句 - ...

  5. 【leetcode❤python】 112. Path Sum

    #-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):#     def __init_ ...

  6. CodeForces 42A Guilty — to the kitchen!

     Guilty — to the kitchen! Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  7. XML详解:第二部分 XML Schema

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  8. [JAVA设计模式]第四部分:行为模式

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. django(一)搭建开发环境

    本学习系列均使用centos7操作系统,基于python3进行操作.centos7下的python3安装配置http://www.cnblogs.com/Guido-admirers/p/625941 ...

  10. install Matlab2016b on Ubuntu 14.04

    From Download Download the install file from Download MATLAB, Simulink, Stateflow, and Other MathWor ...