杭电 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几次..我也是无语了..哪里都不 ...
随机推荐
- 一起来学SpringBoot(十七)优雅的参数校验
参数校验在开发中经常需要写一些字段校验的代码,比如字段非空,字段长度限制,邮箱格式验证等等,写这些与业务逻辑关系不大的代码个人感觉有两个麻烦: 验证代码繁琐,重复劳动方法内代码显得冗长每次要看哪些参数 ...
- Matrix (二分套二分
Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i ...
- js 判断访问终端类型
// 判断访问终端类型 var browser = { versions: function() { var u = navigator.userAgent, app = navigator.appV ...
- AngularJS小练习20170508
首先可能需要安装npm,并且配置环境. 1.打开Dos(命令提示符).按Windows徽标键+R组合键,输入cmd然后按回车键进入Dos. 2.安装Yeoman.在Dos下输入npm install ...
- hdfs深入:03、hdfs的架构以及副本机制和block块存储
HDFS分布式文件系统设计目标 1. 硬件错误 由于集群很多时候由数量众多的廉价机组成,使得硬件错误成为常态 2. 数据流访问 所有应用以流的方式访问数 ...
- JAVA基础——对象流
对象的输入输出流的作用: 用于写入对象 的信息和读取对象的信息. 使得对象持久化. ObjectInputStream : 对象输入流 ObjectOutPutStream :对象输 ...
- vsftp虚拟用户方式访问
需求:外部人员需要对公司服务器上某个文件夹内容进行读写操作 文件目录信息:/opt/abc drwxr-xr-x 9 www www 4096 12月 4 13:02 abc #注 ...
- 自动清除日期目录shell脚本
很多时候备份通常会使用到基于日期来创建文件夹,对于这些日期文件夹下面又有很多子文件夹,对于这些日期文件整个移除,通过find结合rm或者delete显得有些力不从心.本文提供一个简单的小脚本,可以嵌入 ...
- mysql解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)的报错
一般这个错误是由密码错误引起,解决的办法自然就是重置密码. 假设我们使用的是root账户. 1.重置密码的第一步就是跳过MySQL的密码认证过程,方法如下: #vim /etc/my.cnf(注:wi ...
- (十五)python3 可变长参数(arg,*args,**kwargs)
可变长参数(*args,**kwargs) 一.最常见的是在定义函数时,预先并不知道, 函数使用者会传递多少个参数给你, 所以在这个场景下使用这两个关键字.其实并不是必须写成*args 和**kwar ...