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.

 
 
 
开始看不懂题的时候可以看一下后面的举例子,做题的时候没有看到例子好久才看懂的,说多了都是泪。
 

题目大意:

  给一个数字n,范围在1~2^63-1,求1~n之间含有49的数字有多少个。

经典的数位dp,参考了大腿的代码:http://www.cnblogs.com/luyi0619/archive/2011/04/29/2033117.html

状态转移:

  dp[i][0]代表长度为 i 并且不含有49的数字的个数;

  dp[i][1]代表长度为 i 并且不含有49,但是最高位是9的数字的个数;

  dp[i][2]代表长度为 i 并且含有49的数字的个数。

  数组 a[i] 从低位到高位存储 n 的每一位数字。

详解参考:http://www.cnblogs.com/liuxueyang/archive/2013/04/14/3020032.html

代码如下:

#include<iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
ll dp[25][3]; void init()
{
dp[0][0] = 1; //表示位数为i的无49的个数; 等于1是为首位为9,位数为1的数服务;
dp[0][1] = 0; //表示位数为i的无49的个数但最高位为9的个数;
dp[0][2] = 0; //表示位数为i的含有49的个数;
for(int i = 1; i < 25; i++)
{
dp[i][0] = dp[i - 1][0] * 10 - dp[i - 1][1]; //减去最高位为9的情况;
dp[i][1] = dp[i - 1][0]; //最高位加9的个数
dp[i][2] = dp[i - 1][2] * 10 + dp[i - 1][1]; //加上没含49但最高位是9的个数(加上4就成49了);
}
}
ll solve(ll n)
{
ll ans = 0;
bool vis = false;
int bit[25],tot = 0;
while(n)
{
bit[++tot] = n % 10;
n /= 10;
}
bit[tot + 1] = 0;
for(int i = tot; i >= 1; i--)
{
ans += dp[i - 1][2] * bit[i];
if(vis) ans += bit[i] * dp[i - 1][0]; // 这一步和下一步要细心体会其中的高深之处,看了kuangbin大神的代码好久才明白这个道理;
else if(bit[i] > 4) ans += dp[i - 1][1]; //如果n本身已经含有49了就不用考虑是否最高位为9了,直接加上没49的个数,因为n含了49
if(bit[i + 1] == 4 && bit[i] == 9) vis = true;
}
if(vis) ans++; //如果n本身也包含49,则要加1;
return ans;
} int main()
{
ll n;
int t;
init();
scanf("%d", &t);
while(t--)
{
scanf("%lld",&n);
printf("%lld\n",solve(n));
}
return 0;
}

  

 

Bomb的更多相关文章

  1. HDU3555 Bomb[数位DP]

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submi ...

  2. Leetcode: Bomb Enemy

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

  3. HDU 5934 Bomb(炸弹)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  4. hdu 3622 Bomb Game(二分+2-SAT)

    Bomb Game Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. CF 363B One Bomb(枚举)

    题目链接: 传送门 One Bomb time limit per test:1 second     memory limit per test:256 megabytes Description ...

  6. hdu3555 Bomb (记忆化搜索 数位DP)

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

  7. [HDU3555]Bomb

    [HDU3555]Bomb 试题描述 The counter-terrorists found a time bomb in the dust. But this time the terrorist ...

  8. hdu 5934 Bomb

    Bomb Problem Description There are N bombs needing exploding.Each bomb has three attributes: explodi ...

  9. HDOJ 3555 Bomb

    数位DP的DFS写法.... Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Oth ...

随机推荐

  1. Weblogic页面应用查询oracle数据库后台报错或页面日期格式显示错误

    问题:在生产环境中有两台WEB服务器,分别为227和228,部署的应用代码都是每日同步的,两边完全一致,但是某些页面查询数据时,227无结果,并且后台报java数组越界的错误,而228一切正常.经开发 ...

  2. JavaScript中的枚举

    在JavaScript目前的版本中,没有枚举这个概念(当然,ECMA-262第三版中已经将enum作为关键字保留). 然而,如同JavaScript中没有class一样,但我们仍然可以通过间接的方式- ...

  3. Count(*)或者Count(1)或者Count([列]) 区别

    在SQL 中Count(*)或者Count(1)或者Count([列])或许是最常用的聚合函数.很多人其实对这三者之间是区分不清的.本文会阐述这三者的作用,关系以及背后的原理. 往常我经常会看到一些所 ...

  4. sql group by datetime on day

    SELECT        DATEADD(dd, DATEDIFF(dd, 0, postDate), 0)FROM          tableWHERE        (postDate > ...

  5. 从西直门立交桥谈IT架构与重构(干货)

    2015年8月13日 PM 20:00 Neeke君从一个战场奔赴至另一个战场,回到办公室,打开电脑,登陆微信,精彩的的微社群分享马上就要开始了! 大家好,我是Neeke,中文名高驰涛,PHP开发组成 ...

  6. mybatis组合模糊+分页查询

    //组合模糊查询就是这么简单 <select id="findAllJiemu" parameterType="java.util.Map" result ...

  7. UVM的类库

    [转]http://www.asicdv.com/ 一个UVM验证平台可以看成由多个模块组合在一起的,这和以前的verilog代码,以及verilog结合其它各种语言的验证手段在理念上是一样的,最大的 ...

  8. Android之listview && adapter

    今天我们讲的也是非常重要的一个控件listview-最常用也是最难的 一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView ...

  9. VLC嵌入网页,终于要成功了!

    <OBJECT classid="clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921" width="640" heig ...

  10. kaggle 竞赛之套路

    图片数据:卷积还是王道,有几个比较通用性的框架被人拿来改来改去 非图片特征数据:用分类: boost系列算法:牛逼的框架实现 xgboost AdaBoost算法针对不同的训练集训练同一个基本分类器( ...