杭电 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几次..我也是无语了..哪里都不 ...
随机推荐
- DEALLOCATE - 删除一个准备好的查询
SYNOPSIS DEALLOCATE [ PREPARE ] plan_name DESCRIPTION 描述 DEALLOCATE 用于删除前面准备好的查询. 如果你没有明确 DEALLOCATE ...
- python连接mysql的操作
一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可. Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的l ...
- C++运行外部exe并判断exe返回值
有三个API函数可以运行可执行文件WinExec.ShellExecute和CreateProcess.CreateProcess因为使用复杂,比较少用. WinExec主要运行EXE文件. ⑴ 函数 ...
- CAD参数绘制块引用对象(网页版)
主要用到函数说明: _DMxDrawX::DrawBlockReference 绘制块引用对象.详细说明如下: 参数 说明 DOUBLE dPosX 插入点的X坐标 DOUBLE dPosY 插入点的 ...
- JavaSE-18 常用工具类
学习要点 Object类 枚举 包装类 Math类 Random类 字符串处理 日期时间 Object类 1 什么是Object类 Object类存储在java.lang包中,是所有java类(Ob ...
- python note of class
reference: https://www.zhihu.com/question/27699413/answer/267906889 摘要: 我们在描述一个真实对象(物体)时包括两个方面:它可以做什 ...
- PyCharm 自动添加作者及时间
打开pycharm,快捷键ctrl + alt + s 打开模板设置自己所需内容 完整打开路径:file>settings>editor>code style>file and ...
- viewDidLoad等相关函数调用
viewDidLoad 此方法只有当view从nib文件初始化的时候才被调用.viewDidLoad用于初始化,加载时用到的. loadView 此方法在控制器的view为nil的时候被调用.虽然经常 ...
- ionic提供的配色方案
.light #ffffff .stable #f8f8f8 .positive #387ef5 .calm #11c1f3 .balanced #33cd5f .energized #ffc900 ...
- jquery toggle()设置
很多朋友对jquery toggle()比较熟练,甚至经常用到,而且对toggle的三个参数也比较了解$(selector).toggle(speed,callback,switch).但是当你设置$ ...