杭电 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几次..我也是无语了..哪里都不 ...
随机推荐
- 测试ip是否可以ping通
7.写一个脚本hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通.如果能ping通,则提示用户“该IP地址可访问”:如果不可ping通,则提示用户“该IP地址不可访问 参考脚本 ...
- QTreeWidgetItem和QTreeWidgetItemIterator
1.{ ui->treeWidget->setHeaderHidden(true); ui->treeWidget->clear(); QTreeWidgetItem *ima ...
- CAD使用GetxDataString读数据(com接口)
主要用到函数说明: MxDrawEntity::GetxDataString2 读取一个字符扩展数据,详细说明如下: 参数 说明 [in] LONG lItem 该值所在位置 [out, retval ...
- make、makefile
http://blog.csdn.net/wed110/article/details/34853475 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows ...
- eclipse如何设置多个字符的智能提示
clipse代码里面的代码提示功能默认是关闭的,只有输入“.”的时候才会提示功能,用vs的用户可能不太习惯这种,vs是输入任何字母都会提示,下面说一下如何修改eclipse配置,开启代码自动提示功能打 ...
- shell 循环 read line
cat dockerlist |while read line;do docker rmi $line ;done
- print keys %map_function 输出 散列的值: OK_funcsplit_funcpackage_VAR
my %map_function = ( 88 "OK_func" => "open_statement", 89 &qu ...
- IO 双引号 输出 输入
#! /usr/bin/perl use strict;use warnings; print "\n---------<STDIN>_store_into_an_array_a ...
- java中mkdir()和mkdirs()区别
mkdirs()可以建立多级文件夹 mkdir()只会建立一级的文件夹 例如: new File("/file/one/two").mkdirs(); 可建立/file/one/t ...
- Educational Codeforces Round 58 (Rated for Div. 2) (前两题题解)
感慨 这次比较昏迷最近算法有点飘,都在玩pygame...做出第一题让人hack了,第二题还昏迷想错了 A Minimum Integer(数学) 水题,上来就能做出来但是让人hack成了tle,所以 ...