HDU 3555 Bomb 数位dp
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3555
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
#### 问题描述
> 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?
#### 输入
> The first line of input consists of an integer T (1
> The input terminates by end of file marker.
#### 输出
> 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
题意
给你一个数n,求1到n里面有多少个数其中存在子串49的。
题解
数位dp。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 20;
typedef long long LL;
//dp[len][0]表示长度<=len,且不包含49的总数
//dp[len][1]表示长度<=len,且以9结尾但不包含49的总数
//dp[len][2]表示长度<=len,且包好49的总数
//dp[len][0]+dp[len][2]=所有长度小于等于len的数。
//dp[len][1]是dp[len][0]的一个子集。
LL dp[maxn][3];
void pre() {
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 1; i < maxn; 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];
}
}
int main() {
pre();
int tc;
scanf("%d", &tc);
while (tc--) {
LL x; scanf("%I64d", &x);
LL ans = 0;
int arr[maxn], tot=0;
while (x) { arr[++tot]=x%10; x /= 10; }
bool flag = false; //标记x的高位时否已经出现49的组合。
for (int i = tot; i >= 1; i--) {
//这时考虑的是高位已经固定,这一位数为0(是可以有0的,因为高位还会有数,虽然最高位后面每数了,但它统计的是<=len,
//而不是==len!!!)到arr[i]-1的所有情况中包好49的数
ans += dp[i-1][2] * arr[i];
if (flag) ans += dp[i-1][0] * arr[i];
else if (arr[i] > 4) {//如果4刚好是arr[i],那也是不能乱搞的!边界情况我们会一直往后推来考虑
ans += dp[i - 1][1];
}
if (i + 1 <= tot&&arr[i] == 9 && arr[i + 1] == 4) {
flag = 1;
}
}
if (flag) ans++;
printf("%I64d\n", ans);
}
return 0;
}
再来一发dfs:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 20;
typedef __int64 LL;
//dp[len][0]是给第len+1位不为4的时候用的
//dp[len][1]是给第len+1位为4的时候用的(有点像私人订制)
//我们只保存没有限制的状态,对于有限制的状态每次都要算,不过没有限制的状态出现的要不有限制的多的多。
LL dp[maxn][2],ten[maxn];
LL n;
int arr[maxn];
LL dfs(int len, int is4, int ismax) {
if (len == 0) return 0;
if (!ismax&&dp[len][is4] >= 0) return dp[len][is4];
LL res = 0;
int ed = ismax ? arr[len] : 9;
for (int i = 0; i <= ed; i++) {
if (i == 9 && is4) {
//这里是可以直接算的哦
res += ismax ? (n%ten[len - 1] + 1) : ten[len - 1];
}
else {
res += dfs(len - 1, i == 4, ismax&&i == ed);
}
}
return ismax ? res : dp[len][is4] = res;
}
LL solve(LL x) {
int tot = 0;
while (x) { arr[++tot] = x % 10; x /= 10; }
return dfs(tot, false, true);
}
void init() {
memset(dp, -1, sizeof(dp));
ten[0] = 1;
for (int i = 1; i < maxn; i++) ten[i] = ten[i - 1] * 10;
}
int main() {
int tc;
scanf("%d", &tc);
init();
while (tc--) {
scanf("%I64d", &n);
printf("%I64d\n", solve(n));
}
return 0;
}
杂七杂八
orzorzorz
HDU 3555 Bomb 数位dp的更多相关文章
- HDU 3555 Bomb 数位DP 入门
给出n,问所有[0,n]区间内的数中,不含有49的数的个数 数位dp,记忆化搜索 dfs(int pos,bool pre,bool flag,bool e) pos:当前要枚举的位置 pre:当前要 ...
- HDU - 3555 - Bomb(数位DP)
链接: https://vjudge.net/problem/HDU-3555 题意: The counter-terrorists found a time bomb in the dust. Bu ...
- Bomb HDU - 3555 (数位DP)
Bomb HDU - 3555 (数位DP) The counter-terrorists found a time bomb in the dust. But this time the terro ...
- HDU(3555),数位DP
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others ...
- HDU 3555 Bomb (数位DP-记忆化搜索模板)
题意 求区间[1,n]内含有相邻49的数. 思路 比较简单的按位DP思路.这是第一次学习记忆化搜索式的数位DP,确实比递推形式的更好理解呐,而且也更通用~可以一般化: [数位DP模板总结] int d ...
- hud 3555 Bomb 数位dp
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Subm ...
- hdoj 3555 BOMB(数位dp)
//hdoj 3555 //2013-06-27-16.53 #include <stdio.h> #include <string.h> __int64 dp[21][3], ...
- 数位DP入门之hdu 3555 Bomb
hdu 3555 Bomb 题意: 在1~N(1<=N<=2^63-1)范围内找出含有 ‘49’的数的个数: 与hdu 2089 不要62的区别:2089是找不不含 '4'和 '62'的区 ...
- HDU 3555 Bomb(数位DP模板啊两种形式)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Problem Description The counter-terrorists found ...
随机推荐
- HTML5+CSS3前端开发资源整合
HTML5+CSS3前端开发资源整合 推个广告 个人网站:http://www.51pansou.com HTML5视频下载:HTML5视频 HTML5源码下载:HTML5源码 meta相关: & ...
- Windows下安装Elasticsearch
1.下载elasticsearch-1.6.0 .jdk-7u67-windows-x64.exe 1.6.0必须用jdk1.7才能运行 2.配置JAVA_HOME:C:\Program Files\ ...
- js根据日期获得星期
var weekday = getWeekday('2013-9-26'); function getWeekday(sDate){ var dt = new Date(sDate.replace(/ ...
- Ubuntu14.04 切换root账户su root失败解决办法
原因是需要备份一个vimrc,可是cp就提示Permission denied. su root就提示su: Authentication failure 解决办法: sudo passwd root ...
- 【坑】log4j-over-slf4j.jar AND slf4j-log4j12.jar的冲突问题
为了解决这个问题,已经有砸电脑的冲动了.通过百度查找都说是Maven依赖的原因,经过各种尝试仍然没有解决,后来终于在QQ群的帮助下,算是暂时过关. [问题] 程序在本地运行没有问题,打成jar包发布到 ...
- python匿名函数(lambda)
简单来说,编程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数 当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方 ...
- [转]C++编写Config类读取配置文件
//Config.h #pragma once #include <string> #include <map> #include <iostream> #incl ...
- 263. Ugly Number
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- 配置DB2的数据库ODBC连接
打开cmd窗口,输入db2cmd启动db2 clp窗口 输入db2 list node directory查看是否有数据库需要连接的节点 如果不存在,则 节点编目:db2 catalog tcpip ...
- TAT 前端突击队 第四关 题目 腐蚀的画
腐蚀的画 1.一个漂亮的画作在经过几千年岁月的洗礼下,部分地方已经被腐蚀了,像一个孤独的老人,满脸爬满了皱纹.2.但在一个晚上,老王突然发现,这些腐蚀的部分中,隐藏着岁月留下的密秘.请你帮助老王寻找这 ...