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. 编码为multipart/form-data自定义类型(包括文件)如何自动绑定到webapi的action的参数里

    application/x-www-form-urlencoded与 multipart/form-data: Fom表单中如果没有type=file的控件,用默认的application/x-www ...

  2. DotNetBar v12.9.0.0 Fully Cracked

    更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.9.0.0 如果遇到破解问题可以与我 ...

  3. 苹果推送APNS自己总结

    开发状态服务器地址 gateway.sandbox.push.apple.com 2195 产品状态服务器地址 gateway.push.apple.com         2195 Developm ...

  4. beanUtil

    mvc中,页面传值进来,struts2框架是用modeldriven spingmvc是model 不用框架的话,要手动一个一个的设置,然后在用dao方法与数据库联系 servlet框架有BeanUt ...

  5. 认真对待每一道算法题 之 两个排序好的数组寻找的第k个大的数

    转载博客:http://www.cnblogs.com/buptLizer/archive/2012/03/31/2427579.html 题目意思:给出两个排好序的数组 ,不妨设为a,b都按升序排列 ...

  6. C++11 std::copy

    这个函数并不是简单的 while(first != last) { *result = *first; result++; first++; } 事实上这种写法是最具普适性的,值要求inputIter ...

  7. poj蚂蚁问题

    问题描述: n只蚂蚁以每秒1cm的速度在长为Lcm的竿子上爬行.当蚂蚁爬到竿子的端点时就会掉落.由于竿子太细,两只蚂蚁相遇时,它们不能交错通过,只能各自反向 爬回去.对于每只蚂蚁,我们知道它距离竿子左 ...

  8. maven3在eclipse3.4.2中创建java web项目

    学习maven时参考的一些的博客地址:http://www.cnblogs.com/fnng/archive/2011/12/16/2290587.htmlhttp://sarin.iteye.com ...

  9. BZOJ 2241 打地鼠

    暴力. 这怎么这么快.... #include<iostream> #include<cstdio> #include<cstring> #include<a ...

  10. ubuntu下修改mysql默认字符编码出现的Job failed to start解决办法

    ubuntu下修改mysql默认字符编码出现的Job failed to start解决办法 前几天卸掉了用了好多年的Windows,安装了Ubuntu12.04,就开始各种搭环境.今天装好了MySQ ...