杭电 3555 Bomb
Problem Description
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?
Input
The first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.
The input terminates by end of file marker.
Output
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
Hint
From 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.
第一个数位动规!
!!!
看代码理解的。差点儿相同懂那么点点点了。
AC代码例如以下:
传统模板!
!
!
///递推 46MS 328K #include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std; int main()
{
__int64 dp[20][3];//0表示该位数中不含49的情况数。1表示不含49但头为9的情况数。2表示含49的情况数
int i,j;
memset(dp,0,sizeof dp);
dp[0][0]=1;
for(i=1;i<=20;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];
//cout<<dp[i][0]<<" "<<dp[i][1]<<" "<<dp[i][2]<<endl;
}
int t;
cin>>t;
__int64 num[30];
__int64 n;
while(t--)
{
memset(num,0,sizeof num);
cin>>n;
n++;//这一步非常重要
int tt=1;
while(n)
{
num[tt++]=n%10;
n/=10;
}
__int64 ans=0;
int flag=0;int last=0;
for(i=tt-1;i>=1;i--)
{
ans+=num[i]*dp[i-1][2];
if(flag==1)
ans+=dp[i-1][0]*num[i];
if(flag==0&&num[i]>4)
ans+=dp[i-1][1];
if(num[i+1]==4&&num[i]==9)
flag=1;
}
cout<<ans<<endl;
}
return 0;
}
///数位之记忆化搜索 78MS 276K #include<cstdio>
#include<iostream>
#include<cstring>
#define mod 1000000009
#define ll __int64
#define M 100005
using namespace std; ll dp[30][3];
ll num[30]; ll dfs(int pos,int statu,int limit)
{
int i;
if(!pos) return statu==2;
if(!limit&&dp[pos][statu]!=0) return dp[pos][statu];
int end=limit? num[pos]:9;
ll sum=0;
for(i=0;i<=end;i++)
{
int flag=statu;
if(flag==0&&i==4) flag=1;
if(flag==1&&i==9) flag=2;
if(flag==1&&i!=4&&i!=9) flag=0;
sum+=dfs(pos-1,flag,limit&&i==end);
}
return limit? sum:(dp[pos][statu]=sum);
} ll _49(ll n)
{
int pos=1;
memset(dp,0,sizeof dp);
while (n>0)
{
num[pos++]=n%10;
n/=10;
}
//cout<<pos<<"~~~~~~~~~~~~~"<<endl;
return dfs(pos-1,0,1);
} int main()
{
int i,j;
int t;
scanf("%d",&t);
while(t--)
{
ll n;
scanf("%I64d",&n);
printf("%I64d\n",_49(n));
} return 0;
}
杭电 3555 Bomb的更多相关文章
- acm入门 杭电1001题 有关溢出的考虑
最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...
- 杭电acm 1002 大数模板(一)
从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”
按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...
- C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~
暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...
- HDU 3555 Bomb 数位dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Mem ...
- 杭电ACM2076--夹角有多大(题目已修改,注意读题)
杭电ACM2076--夹角有多大(题目已修改,注意读题) http://acm.hdu.edu.cn/showproblem.php?pid=2076 思路很简单.直接贴代码.过程分析有点耗时间. / ...
- 杭电ACM2092--整数解
杭电ACM2092--整数解 分析 http://acm.hdu.edu.cn/showproblem.php?pid=2092 一个YES,一个Yes.试了10几次..我也是无语了..哪里都不 ...
随机推荐
- (转)淘淘商城系列——dubbo监控中心
http://blog.csdn.net/yerenyuan_pku/article/details/72777623 之前我们就已学过了dubbo,想必大家对dubbo的架构有所了解,dubbo的架 ...
- A C compiler that parses this code will contain at least the following symbol table entries
A C compiler that parses this code will contain at least the following symbol table entries Consider ...
- Flex 布局 (两个div居中自适应 宽度变小变一列,宽度够就是两列)
https://www.runoob.com/w3cnote/flex-grammar.html display: flex; justify-content: center; align-items ...
- 在vue中场景,循环行,点击当前行编辑数据
当前列表 点击编辑,行变为编辑框. <Row style="color:#999;margin-bottom:11px"> <Row style="ma ...
- React入门介绍(2)- React Component-React组件
React Component-React组件 允许用户自由封装组件是React非常突出的特性,用户可将自己创建的组件像普通的HTML标签一样插入页面,React.CreateClass方法就是用来创 ...
- Python模块 shelve xml configparser hashlib
常用模块1. shelve 一个字典对象模块 自动序列化2.xml 是一个文件格式 写配置文件或数据交换 <a name="hades">123</a>3. ...
- 零基础入门学习Python(23)--递归:这帮小兔崽子
知识点 我们都知道兔子繁殖能力是惊人的,如下图: 我们可以用数学函数来定义: 假设我们需要求出经历了20个月后,总共有多少对小兔崽子? 迭代实现 def fab(n): n1 = 1 n2 = 1 n ...
- 用PHP写一个最简单的解释器Part4(写一个最简单的脚本语言)
好吧!我承认我想标题党了.大家对解释器的吸引,绝对没有自己动手写一个脚本语言更有吸引力.不过如果看到标题过来的,可能也是 我承认,之前收藏的减肥视频,我都是这样对待他们的. 不过我还是相信很多程序猿o ...
- python+selenium之元素的八大定位方法
以百度搜索框为例,先打开百度网页 1.点右上角爬虫按钮 2.点左下角箭头 3.讲箭头移动到百度搜索输入框上,输入框高亮状态 4.下方红色区域就是单位到输入框的属性: <input id=&quo ...
- 91-Williams' Percent Range 威廉指标.(2015.7.4)
Williams' Percent Range 威廉指标 ~计算: %R = (HIGH(i-n)-CLOSE)/(HIGH(i-n)-LOW(i-n))×100 注解:CLOSE: 当前时段的收盘价 ...