Bomb

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

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
 

题意:求1~n闭区间内含有“49”的数的个数

题解:

dp[i][2] 长度为i 含有“49”的个数

dp[i][1] 长度为i  不含有“49”但是高位为“9”的个数

dp[i][0] 长度为i  不含有“49”的个数

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

dp[i][2]=dp[i-1][2]*10+dp[i-1][1]; //考虑第i位为“4” i-1位为“9”

dp[i][1]=dp[i-1][0];

dp[i][0]=dp[i-1][0]*10-dp[i-1][1];

对于n处理之前为什么要自增1

因为题目要求处理的是闭区间 可能自增1当作开区间处理

http://www.cnblogs.com/liuxueyang/archive/2013/04/14/3020032.html

 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#define ll __int64
using namespace std;
int t;
ll n;
ll a[];
ll dp[][];
void init()
{
dp[][]=;
for(int i=; i<=; i++)
{
dp[i][]=*dp[i-][]-dp[i-][];
dp[i][]=dp[i-][];
dp[i][]=*dp[i-][]+dp[i-][];
}
}
int main()
{
init();
while(scanf("%d",&t)!=EOF)
{
for(int i=; i<=t; i++)
{
scanf("%I64d",&n);
memset(a,,sizeof(a));
int len=;
n++;
while(n)
{
a[len]=n%;
n=n/;
len++;
}
int flag=;
int last=;
ll ans=;
for(int j=len; j>=; j--)
{
ans+=dp[j-][]*a[j];
if(flag)
ans+=dp[j-][]*a[j];
if(!flag&&a[j]>)
ans+=dp[j-][];
if(last==&&a[j]==)
flag=;
last=a[j];
}
printf("%I64d\n",ans);
}
}
return ;
}

HDU 3555 数位dp的更多相关文章

  1. HDU 3555 数位dp入门

    开始想用dp[i][j]来记录第i位j开头含有49的数的个数 但是init后并不知道如何进行cal 想了想可以用不要62的思想 当作不要49来做 然后减一下 就好 看网上的代码 不要62和这道题用的d ...

  2. hdu 3555数位dp基础入门题

    #include<stdio.h> #define N 20 long long  dp[N][3]; void init(){ long long  i; dp[0][0]=1; for ...

  3. Bomb HDU - 3555 数位dp

    Code: #include<cstdio> #include<algorithm> #include<cstring> #include<string> ...

  4. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  5. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做 ...

  7. hdu:2089 ( 数位dp入门+模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 数位dp的模板题,统计一个区间内不含62的数字个数和不含4的数字个数,直接拿数位dp的板子敲就行 ...

  8. HDU 4352 XHXJ's LIS HDU(数位DP)

    HDU 4352 XHXJ's LIS HDU 题目大意 给你L到R区间,和一个数字K,然后让你求L到R区间之内满足最长上升子序列长度为K的数字有多少个 solution 简洁明了的题意总是让人无从下 ...

  9. hdu 3709 数位dp

    数位dp,有了进一步的了解,模板也可以优化一下了 题意:找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数例如4139,以3为支点4*2 ...

随机推荐

  1. 火车安排问题(dp好题)

    火车站内往往设有一些主干线分叉出去的铁路支路,供火车停靠,以便上下客或装载货物.铁路 支路有一定长度:火车也有一定的长度,且每列火车的长度相等. 假设某东西向的铁路上,有一小站.该站只有一条铁路支路可 ...

  2. ros使用RPLIDAR激光雷达

    1.首先下载RPLIDAR的驱动功能包 https://github.com/robopeak/rplidar_ros 2.然后解压放到~/catkin_ws/src目录下 3.执行catkin_ma ...

  3. 学习linux与wp8.1——启航

    现在不知不觉已经大三了,而本专业的东西没有多大感兴趣的,我看好wp开发和linux开发. 为什么要学习wp开发?其实就是一种兴趣,我手中有部620而已,学着学着就感兴趣了,所以打算继续学下去.同时,我 ...

  4. Hibernate中的继承映射

    1.继承映射 继承映射分为两种情况:简单继承映射和继承映射. 在简单继承映射中,每个子类都要写一个映射文件. 在继承映射中,只使用一个映射文件.继承映射分为三种情况: 所有子类映射到一张表 需要使用鉴 ...

  5. win10 用微软账户登录无法访问共享的问题

    百度找了一大堆可以解决的,最终最简单的方式(可能是bug): 测试了一下,Win10用微软账户登录的,连局域网共享时,输入用户名的时候,前面加个乱七八糟的域名就可以访问了: 比如: 用户名:   ba ...

  6. java基础之 垃圾回收机制

    1. 垃圾回收的意义 在C++中,对象所占的内存在程序结束运行之前一直被占用,在明确释放之前不能分配给其它对象:而在Java中,当没有对象引用指向原先分配给某个对象的内存时,该内存便成为垃圾.JVM的 ...

  7. iOS程序进入后台后仍运行定时器NSTimer

    由于本应用需要在应用进入后台后还要进行定时的检测功能,因此对于我来说怎样让APP在进入后台后 保持运行状态是比较大的需求.然后在iOS系统中是很难实现的,不管是 通过 音频还是 定位系统,我查找了一些 ...

  8. MySQL语句45道练习题及答案

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  9. 关押罪犯(noip2010)

    解法: (1)搜索(30分) (2)二分(此题属于最大值最小问题) (3)贪心+并查集 下面着重说一下“贪心+并查集” 因为有A.B两座监狱,每个犯人不是在A,就是在B监狱. 至于每个犯人在那个监狱, ...

  10. 扩展jQuery easyui datagrid增加动态改变列编辑的类型

    $.extend($.fn.datagrid.methods, { addEditor : function(jq, param) { if (param instanceof Array) { $. ...