HDOJ 题目3555 Bomb(数位DP)
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 10419 Accepted Submission(s): 3673
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 input terminates by end of file marker.
3
1
50
500
0
1
15HintFrom 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.
pid=3554" style="color:rgb(26,92,200); text-decoration:none">3554
3556 3557 3558 3559#include<stdio.h>
#include<string.h>
int bit[20];
__int64 dp[20][10][2];
__int64 dfs(int pos,int pre,int isture,int limit)
{
if(pos<0)
{
return isture;
}
if(!limit&&dp[pos][pre][isture]!=-1)
{
return dp[pos][pre][isture];
}
int last=limit? bit[pos]:9;
__int64 ans=0;
for(int i=0;i<=last;i++)
{
ans+=dfs(pos-1,i,isture||(pre==4&&i==9),limit&&(i==last));
}
if(!limit)
{
dp[pos][pre][isture]=ans;
}
return ans;
}
__int64 solve(__int64 n)
{
int len=0;
while(n)
{
bit[len++]=n%10;
n/=10;
}
return dfs(len-1,0,0,1);
}
int main()
{
int n;
memset(dp,-1,sizeof(dp));
int t;
scanf("%d",&t);
while(t--)
{
__int64 n;
scanf("%I64d",&n);
printf("%I64d\n",solve(n));
}
}
人家的代码http://blog.csdn.net/scf0920/article/details/42870573
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=100000000;
const int INF=0x3f3f3f3f;
const double eqs=1e-8;
LL dp[21][11], c[21];
LL dfs(int cnt, int pre, int maxd, int zero)
{
if(cnt==-1) return 1;
if(maxd&&zero&&dp[cnt][pre]!=-1) return dp[cnt][pre];
int i, r;
LL ans=0;
r=maxd==0? c[cnt]:9;
for(i=0;i<=r;i++){
if(!zero||!(pre==4&&i==9)){
ans+=dfs(cnt-1,i,maxd||i<r,zero||i);
}
}
if(maxd&&zero) dp[cnt][pre]=ans;
return ans;
}
LL Cal(LL x)
{
int i, cnt=0;
while(x){
c[cnt++]=x%10;
x/=10;
}
return dfs(cnt-1,-1,0,0);
}
int main()
{
int t;
LL n;
scanf("%d",&t);
memset(dp,-1,sizeof(dp));
while(t--){
scanf("%I64d",&n);
printf("%I64d\n",n+1-Cal(n));
}
return 0;
}
HDOJ 题目3555 Bomb(数位DP)的更多相关文章
- HDU 3555 Bomb 数位dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Mem ...
- hud 3555 Bomb 数位dp
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Subm ...
- HDU 3555 Bomb 数位DP 入门
给出n,问所有[0,n]区间内的数中,不含有49的数的个数 数位dp,记忆化搜索 dfs(int pos,bool pre,bool flag,bool e) pos:当前要枚举的位置 pre:当前要 ...
- hdoj 3555 BOMB(数位dp)
//hdoj 3555 //2013-06-27-16.53 #include <stdio.h> #include <string.h> __int64 dp[21][3], ...
- 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 ...
- HDU3555 Bomb —— 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) M ...
- hdu3555 Bomb 数位DP入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 简单的数位DP入门题目 思路和hdu2089基本一样 直接贴代码了,代码里有详细的注释 代码: ...
随机推荐
- (转)微信开发连接SAE数据库
原文地址:http://blog.csdn.net/tterminator/article/details/51067130 在读写云数据库MySQL之前,需要说明的是,在新浪云平台上使用数据库时,该 ...
- 《Linux命令行与shell脚本编程大全》 第六章环境变量
很多程序和脚本都通过环境变量来获取系统信息.存储临时数据和配置信息. 6.1 什么是环境变量: bash shell用一个叫环境变量(environment variable)的特性来存储有关shel ...
- SQL注入详解
SQL是一种将SQL代码添加到输入参数中,传递到SQL服务器解析并执行的一种攻击手段 产生的原因 web开发人员无法保证所有输入都已经安全过滤 攻击者利用发送给SQL服务器的输入数据,构造可执行的SQ ...
- docker下编译mangoszero WOW60级服务端(一)
这几天看到暴雪准备开放怀旧服的新闻,突然想到几年前用大芒果window一键服务端自己搭建过服务,就想着在Linux环境下重新编译一套,毕竟Linux作为服务端,性能和稳定性都会高一些,于是在mac虚拟 ...
- bash, sh, dash 傻傻分不清楚
原文链接,转载请注明出处: http://www.happycxz.com/m/?p=137 常见shell类型 Bourne shell (sh) UNIX 最初使用,且在每种 UNIX 上都可以使 ...
- 深度学习之tensorflow (一)
一.TensorFlow简介 1.TensorFlow定义: tensor :张量,N维数组 Flow : 流,基于数据流图的计算 TensorFlow : 张量从图像的一端流动到另一端的计算 ...
- nginx-http-concat资源文件合并模块
网页中引入多个CSS和JS的时候,浏览器会发出很多(css个数+js个数)次网络请求,甚至有的网页中有数十个以上的CSS或JS文件,用户体验特别不好,正好可以利用nginx-http-concat n ...
- Python环境变量搭建
1.首先下载相对应的Python版本,安装后在系统环境变量的path路径下加入安装的默认路径: 2.测试:dos命令下输入python.回车,然后测试,exit()退出来,测试完成.
- Logger.getLogger和 LogFactory.getLog
Logger.getLogger和 LogFactory.getLog Logger.getLogger LogFactory.getLogLogger来自log4j自己的包.如果用Logger.ge ...
- redis数据类型及常用命令介绍(图文实例)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAhgAAAFLCAYAAACUdvXUAAAgAElEQVR4nO3da1da58L2fT5KfPvc96 ...