Bomb HDU - 3555 (数位DP)

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? 

InputThe 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. 
OutputFor 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. 题意:求1-n之间有几个数带有49
题解:一题数位DP,可以用记忆化搜索解决,感觉板子还是有点懂了,但不知道改了之后会不会了,具体的dfs的每一步的具体操作已经放在代码里了
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std; using namespace std;
#define ms(a,b) memset(a,b,sizeof(a))
#define lson rt*2,l,(l+r)/2
#define rson rt*2+1,(l+r)/2+1,r
typedef unsigned long long ull;
typedef long long ll;
const int MAXN=1e4+5;
const double EPS=1e-8;
const int INF=0x3f3f3f3f;
const int MOD = 1e9+7; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 1e5+5;
const int mod = 1e9+7; int bit[40];
ll f[40][4];
ll dp(int pos,int st,bool flag)
{
//pos为位,st为状态,st=0:没有49,st=1:前一位为4,st=2,表示49都已经出现过了
//flag表示高位与原数是否相同
if(pos == 0)
return st == 2;
if(flag && f[pos][st] != -1)
return f[pos][st];
ll ans = 0;
int x;
if(flag)
x = 9;
else
x = bit[pos];
//cout<<x<<endl;
for(int i = 0;i <= x; i++)
{
if((st == 2) || (st == 1 && i == 9)) //如果上一位已经有49(st==2)或者上一位为4(st==1)当前位为9
ans += dp(pos-1,2,flag || i<x); //只能加上上一个状态,且上一个状态一定为已经有49了
else if(i == 4)
ans += dp(pos-1,1,flag || i<x); //当前为4和前一位也为4的状态是一样的
else //维持st=0的状态
ans += dp(pos-1,0,flag || i<x);
}
if(flag)
f[pos][st] = ans; //记忆化
return ans;
} ll calc(ll x)
{
int len = 0;
while(x)
{
bit[++len] = x % 10;
x /= 10;
}
//cout<<len<<endl;
return dp(len,0,0);
}
int main()
{
int t;
scanf("%d",&t);
memset(f,-1,sizeof f);
while(t--)
{
ll n;
scanf("%lld",&n);
printf("%lld\n",calc(n));
}
}

Bomb HDU - 3555 (数位DP)的更多相关文章

  1. Bomb HDU - 3555 数位dp

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

  2. HDU 3555 数位dp

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

  3. HDU 3555 数位dp入门

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

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

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

  5. Bomb HDU - 3555

    Bomb HDU - 3555 求1~n中含有49数的个数 #include<bits/stdc++.h> #define LL long long using namespace std ...

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

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

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

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

  8. Bomb HDU 3555 dp状态转移

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题意: 给出一个正整数N,求出1~N中含有数字“49”的数的个数 思路: 采用数位dp的状态转移方程 ...

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

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

随机推荐

  1. android 开发-系统设置界面的实现

    具体与Preference的用法类似,这里就不做过多解释,直接贴示例代码,需要在res下新建xml文件夹,在xml文件夹下添加xml文件. xml:(注意:root节点是:PreferenceScre ...

  2. 在java.ext.dirs中使用环境变量导致crontab执行不成功的问题及解决

    在java.ext.dirs中使用环境变量导致crontab执行不成功的问题及解决 Table of Contents 1. java.ext.dirs的使用和环境变量 2. 问题:在crontab中 ...

  3. oracle中查找与已知表的数据库对象

    在此次情况中,业务顾问就给我提供了一张客户公司客户化的Form,然后让找出界面上的数据是怎样生成的. 首先我们从EBS form 界面上找到了界面的数据来源于一张表ks_so_line_margin_ ...

  4. Maven建立spring-web项目

    参考博客网址: https://blog.csdn.net/caoxuekun/article/details/77336444 1.eclipse集成maven 2.maven创建web项目 3.搭 ...

  5. Reduce侧连接

    1.reduce side join 在reduce端进行表的连接,该方法的特点就是操作简单,缺点是map端shffule后传递给reduce端的数据量过大,极大的降低了性能 连接方法: (1)map ...

  6. yii相关手册文档

    1.Yii官方手册 Yii Framework 2.0 权威指南:http://www.yiichina.com/doc/guide/2.0/start-databases 2.yii高级应用程序手册 ...

  7. window7防火墙无法更改某些设置,错误代码0×80070422

    原因:这是由于管理工具的服务中的windows防火墙被禁用了. 解决方案:在window7中点击控制面板,然后点击管理工具,在点服务,然后找到windows firewall 然后将其改为自动就就可以 ...

  8. 基于PowerShell的Lync Server管理 使用C# 之 Telephony 功能 查看 /修改

    本以为这个属性可以在用户信息中直接反应出来,但是看了好几遍还是没找到这个属性名称 这个功能没有在get-User 的结果中直接反映出来 但是可以通过 Property 查找单个选项 如: Get-Cs ...

  9. linux 命令——43 killall(转)

    Linux 系统中的killall命令用于杀死指定名字的进程(kill processes by name).我们可以使用kill命令杀死指定进程PID的进 程,如果要找到我们需要杀死的进程,我们还需 ...

  10. 2017.12.13 Java中是怎样通过类名,创建一个这个类的数组

    先在类方法中定义数组的方法: public int[] method6(int[] arr){ for(int i = 0; i<arr.length;i++){ arr[i] = (int)( ...