Bomb

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

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.

 
Author
fatboy_cw@WHU
 
Source
 
题意: 给你一个数n,要你统计出1到n中出现含有49数字的个数: 比如 498,549,49.....
对于这一道题: 看到一个博客引用了这张图片,觉得说的很清晰,就引用了..
 
我们对于 i-1长度的数字分析,无疑就这么集中情况(当然只是围绕49来说的哇)首部分析:
                                                          i-1长度                                  那么对于 i长度
首部为49 ,那么它的格式必然为:              49****                                   ?49****(?可能为9)
 
首部保函9 ,那么它的格式必然为:             9*****                                   ?9*****(?可能为4)
 
首部部位49 ,那么它的格式为:                *******                                  ?*******(?可能为9)
 
    我们不妨用dp[i][2]表示首部为49的,dp[i][1]表示首部为9的,dp[i][0]表示首部不为49,于是我们可以发现这样一个规律:
 
     dp[i-1][2]向前移一位,即原来的个位变为十位,十位变为百位的那种移位。 形成dp[i][2],但是需要注意的是:
      当dp[i-1][2]时,其实由我上面说的,?可能为9 ,所以当向前移一位时,?为9的可能性被去掉了。所以
    dp[i-1][2]*10(移动一位时)需要减去 开头为9的那种模式dp[i-1][1],所以得到:
  (1)      dp[i][2]=dp[i-1][2]*10-dp[i-1][1];
    对于i位首部为9那么后面只需要满足不为49即可,刚好满足dp[i][0];
  (2)  所以 dp[i][1]=d[i-1][0];
   对于首部不为49的
       同样也可以分析出来...
      dp[i][0]=dp[i-1][0]*10+dp[i-1][1];
 
于是得到这样一个预处理方程:
                        dp[i][2]=dp[i-1][2]*10-dp[i-1][1];
                        dp[i][1]=d[i-1][0]; 
                        dp[i][0]=dp[i-1][0]*10+dp[i-1][1];
代码:详情见代码:
 //#define LOCAL
#include<cstdio>
#include<cstring>
#define LL __int64
using namespace std;
const int maxn=;
LL dp[maxn][]={};
int nn[maxn];
int main()
{ #ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int cas,i;
LL n;
scanf("%d",&cas);
/*数位DP的惯有模式预处理*/
dp[][]=;
for(i=;i<=;i++)
{
dp[i][]=dp[i-][]*-dp[i-][];
dp[i][]=dp[i-][];
dp[i][]=dp[i-][]*+dp[i-][];
}
while(cas--)
{
scanf("%I64d",&n);
i=;
n+=;
memset(nn,,sizeof(nn));
while(n>)
{
nn[++i]=n%;
n/=;
}
LL ans=;
bool tag=;
int num=;
for( ; i>= ; i-- )
{
ans+=dp[i-][]*nn[i]; /*计算49开头的个数*/
if(tag){
ans+=dp[i-][]*nn[i]; /*当前面出现了49的时候,那么后面出现的任何数字也要进行统计*/
}
if(!tag&&nn[i]>)
{
ans+=dp[i-][]; /*如果没有出现49开头,只要首部大于5,那么必定保函有一个49*/
}
if(num==&&nn[i]==)
tag=;
num=nn[i];
}
printf("%I64d\n",ans);
}
return ;
}

hdu---(3555)Bomb(数位dp(入门))的更多相关文章

  1. HDU 3555 Bomb 数位DP 入门

    给出n,问所有[0,n]区间内的数中,不含有49的数的个数 数位dp,记忆化搜索 dfs(int pos,bool pre,bool flag,bool e) pos:当前要枚举的位置 pre:当前要 ...

  2. HDU 3555 Bomb 数位dp

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

  3. HDU - 3555 - Bomb(数位DP)

    链接: https://vjudge.net/problem/HDU-3555 题意: The counter-terrorists found a time bomb in the dust. Bu ...

  4. Bomb HDU - 3555 (数位DP)

    Bomb HDU - 3555 (数位DP) The counter-terrorists found a time bomb in the dust. But this time the terro ...

  5. hdu3555 Bomb 数位DP入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 简单的数位DP入门题目 思路和hdu2089基本一样 直接贴代码了,代码里有详细的注释 代码: ...

  6. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  7. HDU(3555),数位DP

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

  8. HDU 3555 Bomb (数位DP-记忆化搜索模板)

    题意 求区间[1,n]内含有相邻49的数. 思路 比较简单的按位DP思路.这是第一次学习记忆化搜索式的数位DP,确实比递推形式的更好理解呐,而且也更通用~可以一般化: [数位DP模板总结] int d ...

  9. hud 3555 Bomb 数位dp

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

  10. hdoj 3555 BOMB(数位dp)

    //hdoj 3555 //2013-06-27-16.53 #include <stdio.h> #include <string.h> __int64 dp[21][3], ...

随机推荐

  1. [HDOJ5877]Weak Pair(DFS,线段树,离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5877 题意:给一棵树和各点的权值a,求点对(u,v)个数,满足:1.u是v的祖先,2.a(u)*a(v ...

  2. SpringMVC 服务器端验证

    1.导入JSR303验证类库Jar包2.在MVC的配置文件中添加<mvc:annotation-driven/>的配置3.在MVC的配置文件中添加验证器的配置4.在接收表单数据的类中添加验 ...

  3. [Effective Java]第七章 方法

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. QQ音乐项目(OC版) - 实现细节

    QQ 音乐看似简单,但自己手动实现起来,才发现没有那么简单,有好多细节,需要注意. github : https://github.com/keenleung/QQMusic-OC 一.业务逻辑 首先 ...

  5. 一个ListBox的例子

    1.向ListBox中放入其他控件 XAML: <Window x:Class="ItemsControls.MainWindow" xmlns="http://s ...

  6. 关于deferred

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 转 Cocos网络篇[3.2](3) ——Socket连接(1)

    Cocos网络篇[3.2](3) ——Socket连接(1) 2015-03-05 22:24:13 标签:network http socket cocos [唠叨] 在客户端游戏开发中,使用HTT ...

  8. Nginx入门笔记之————配置文件结构

    在nginx.conf的注释符号位# nginx文件的结构,这个对刚入门的同学,可以多看两眼. 默认的config: #user nobody; worker_processes ; #error_l ...

  9. css 常用样式命名规则

    大家在写css的时候,对一些html标签起一个合适的名字是个很头疼的事情,现在给大家分享项目中常用的名字供参考. 外套:wrap  ——用于最外层 头部:header  ——用于头部 主要内容:mai ...

  10. Python学习笔记13—错误和异常

    常见的异常: