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. 二分搜索法(转载自vanezkw)

    二分查找算法java实现 今天看了一下JDK里面的二分法是实现,觉得有点小问题.二分法的实现有多种今天就给大家分享两种.一种是递归方式的,一种是非递归方式的.先来看看一些基础的东西. 1.算法概念. ...

  2. Hibernate4 No Session found for current thread原因

    Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...

  3. vs 折叠跟展开所有方法。

    Ctrl + M + O: 折叠所有方法 Ctrl + M + M: 折叠或者展开当前方法 Ctrl + M + L: 展开所有方法

  4. vector用法总结(转载)

    一.vector的基本概念 vector是同一种类型的对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库负责管理存储元素的相关内存.我们把vector称为容器,是因为它可以包 ...

  5. win10系统的点评

    Windows 10 是美国微软公司所研发的新一代跨平台及设备应用的操作系统.在正式版本发布一年内,所有符合条件的Windows7.Windows 8.1的用户都将可以免费升级到Windows 10, ...

  6. 实用的WPF Xml的简易读写类以及用法示例

    转自:http://www.silverlightchina.net/html/study/WPF/2012/0808/17980.html 最近真是写博客写的不可收拾,今天再来一篇. 因为做一些程序 ...

  7. C#语法问答式总结

    传入某个属性的set方法的隐含参数的名称是什么?value,它的类型和属性所声名的类型相同. 如何在C#中实现继承?在类名后加上一个冒号,再加上基类的名称. C#支持多重继承么?不支持.可以用接口来实 ...

  8. IP地址 子网掩码 默认网关 网络地址 广播地址

    “IP地址”是“TCP/IP”(Transmite Control Protocol 传输控制协议/Internet Protocol网际协议)里其中的一种协议. Internet之所以能将广阔范围内 ...

  9. IOS 在http请求中使用cookie

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainbird.blog.51cto.com/211214/805173 一直以 ...

  10. Python入门(二,基础)

    一,基本语法 Python标识符 在python里,标识符有字母.数字.下划线组成. 在python中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. python中的标识符是区分大 ...