hdu_3555 bomb
数位动态规划
数位动态规划是求解一个大区间[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的更多相关文章
- HDU3555 Bomb[数位DP]
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submi ...
- Leetcode: Bomb Enemy
Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...
- HDU 5934 Bomb(炸弹)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 3622 Bomb Game(二分+2-SAT)
Bomb Game Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Bomb
Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impro ...
- CF 363B One Bomb(枚举)
题目链接: 传送门 One Bomb time limit per test:1 second memory limit per test:256 megabytes Description ...
- hdu3555 Bomb (记忆化搜索 数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory ...
- [HDU3555]Bomb
[HDU3555]Bomb 试题描述 The counter-terrorists found a time bomb in the dust. But this time the terrorist ...
- hdu 5934 Bomb
Bomb Problem Description There are N bombs needing exploding.Each bomb has three attributes: explodi ...
随机推荐
- 在opencv中实现中文输出
http://pan.baidu.com/s/1hrQTWDe 已经成功 ; 来自为知笔记(Wiz)
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 The Book List
描述 The history of Peking University Library is as long as the history of Peking University. It was b ...
- 编译android源码官方教程(2)建立编译环境「linux & mac osx」
https://source.android.com/source/initializing.html Establishing a Build Environment IN THIS DOCUMEN ...
- C语言第3天标准的输入输出函数
:first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; m ...
- css基本知识
WANGJUN59451 css基本知识 1.CSS 简介 CSS 指层叠样式表 (Cascading Style Sheets),是一种用来表现 HTML 文档样式的语言,样式定义如何显示 HT ...
- Java-集合框架整理
一.List 接口集合: 1.优势以及特点:有序,允许重复元素 . 2.实现类: * AarrayList 类:不同步,可变长度数组,倍增率为 1/n ; * LinkedList 类:不同步,链表结 ...
- git log 查看 提交历史
在提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,可以使用 Git log 命令查看. 接下来的例子会用我专门用于演示的 simplegit 项目,运行下面的命令获取该项目源代码: git ...
- 02_Spring控制反转案例快速入门
Spring控制反转案例快速入门 1.下载Spring最新开发包 http://www.springsource.org/download/community 下载spring3.2 的开发包 目录结 ...
- Codeforces Round #281 (Div. 2) D. Vasya and Chess 水
D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- git学习笔记10-新开发的功能不想要了-强行删除分支
添加一个新功能时,你肯定不希望因为一些实验性质的代码,把主分支搞乱了,所以,每添加一个新功能,最好新建一个feature分支,在上面开发,完成后,合并,最后,删除该feature分支. 现在,你终于接 ...