Bomb



Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 6609    Accepted Submission(s): 2303

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的更多相关文章

  1. acm入门 杭电1001题 有关溢出的考虑

    最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...

  2. 杭电acm 1002 大数模板(一)

    从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...

  3. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

  4. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  5. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  6. C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~

    暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...

  7. HDU 3555 Bomb 数位dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Mem ...

  8. 杭电ACM2076--夹角有多大(题目已修改,注意读题)

    杭电ACM2076--夹角有多大(题目已修改,注意读题) http://acm.hdu.edu.cn/showproblem.php?pid=2076 思路很简单.直接贴代码.过程分析有点耗时间. / ...

  9. 杭电ACM2092--整数解

    杭电ACM2092--整数解    分析 http://acm.hdu.edu.cn/showproblem.php?pid=2092 一个YES,一个Yes.试了10几次..我也是无语了..哪里都不 ...

随机推荐

  1. JSP的有哪些内置对象,作用分别是什么?

    request:表示HttpServletRequest对象,它包含了有关浏览器请求的信息,并且提供了几个用于获取cookie,header和session数据的有用方法: response:表示Ht ...

  2. Java入门第38课——猜字母游戏之设计程序结构

    问题        本案例需要实现猜字母游戏程序中的程序结构 方案        分析猜字母游戏可以看出,程序首先需要随机产生5个不同的字母作为需要猜测的结果,因此,可以先定义一个方法,以实现此功能: ...

  3. shell脚本的练习

    创建一个以.sh结束的文件. 规则: 文件的头部使用#!/bin/sh 开头   这个是一个标识的作用,告诉使用哪种脚本来执行 echo 用来向命令行来输出的东西

  4. jQuery radio 选中提示

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. JS日期,金钱处理

    一丶获取两个时间的天数 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...

  6. DHCP和PXE

    继续学习,Let's go!DHCP和PXE都是什么呢?如果非科班出身,可能一脸懵逼,好多东西需要去学习了,真的,继续学吧,付出不一定会有回报,不付出肯定就是等死了,呵呵! 一.DHCP 真正需要手动 ...

  7. 瀑布流布局js

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  8. Chrome插件:微信公众号自动登录(chrome.extension)

    manifest.json: { "manifest_version": 2, "name": "WX.AutoLogin", " ...

  9. 全国绿色计算大赛 模拟赛第二阶段 (Python)

    第1关气温预测 def dailyTemps(temp_list): result = [] for ca in range(0, len(temp_list)-1): for i in range( ...

  10. [Python3网络爬虫开发实战] 4-解析库的使用

    上一章中,我们实现了一个最基本的爬虫,但提取页面信息时使用的是正则表达式,这还是比较烦琐,而且万一有地方写错了,可能导致匹配失败,所以使用正则表达式提取页面信息多多少少还是有些不方便. 对于网页的节点 ...