hdu3555(数位DP dfs/递推)
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 10649 Accepted Submission(s): 3758
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?
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.
1
50
500
1
15
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.
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL long long
LL dp[][];
int digit[]; LL dfs(int len,bool state,bool fp)
{
if(!len)
return ;
if(!fp && dp[len][state] != -)
return dp[len][state];
LL ret = ;int fpmax = fp ? digit[len] : ;
for(int i=;i<=fpmax;i++)
{
if(state && i == )
continue;
ret += dfs(len-,i == ,fp && i == fpmax);
//第二个参数表明这位是否为4,如果为4,下一位就不能为9,否则没有限制
//第三个参数表明前缀是否相同,如果相同,下一位就只能枚举到最大值,否则就没有限制
}
if(!fp)
dp[len][state] = ret;
return ret; //ret代表0到n不含49的个数
} LL f(LL n)
{
int len = ;
while(n)
{
digit[++len] = n % ;
n /= ;
}
return dfs(len,false,true);
} int main()
{
//freopen("test.txt","r",stdin);
LL a,b;
memset(dp,-,sizeof(dp));
int t;
scanf("%d",&t);
while(t--)
{
cin>>b;
cout<<(b+-f(b))<<endl;
} return ;
}
hdu3555(数位DP dfs/递推)的更多相关文章
- [AHOI2009]中国象棋 DP,递推,组合数
DP,递推,组合数 其实相当于就是一个递推推式子,然后要用到一点组合数的知识 一道很妙的题,因为不能互相攻击,所以任意行列不能有超过两个炮 首先令f[i][j][k]代表前i行,有j列为一个炮,有k列 ...
- UVa 926【简单dp,递推】
UVa 926 题意:给定N*N的街道图和起始点,有些街道不能走,问从起点到终点有多少种走法. 很基础的dp.递推,但是有两个地方需要注意,在标记当前点某个方向不能走时,也要同时标记对应方向上的对应点 ...
- hdu3555 数位dp
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Subm ...
- HDU 5965 三维dp 或 递推
题意:= =中文题 思路一:比赛时队友想的...然后我赛后想了一下想了个2维dp,但是在转移的时候,貌似出了点小问题...吧?然后就按照队友的思路又写了一遍. 定义dp[i][j][k],表示第i列, ...
- 【BZOJ4944】【NOI2017】泳池 概率DP 常系数线性递推 特征多项式 多项式取模
题目大意 有一个\(1001\times n\)的的网格,每个格子有\(q\)的概率是安全的,\(1-q\)的概率是危险的. 定义一个矩形是合法的当且仅当: 这个矩形中每个格子都是安全的 必须紧贴网格 ...
- [NOI2017]泳池——概率DP+线性递推
[NOI2017]泳池 实在没有思路啊~~~ luogu题解 1.差分,转化成至多k的概率减去至多k-1的概率.这样就不用记录“有没有出现k”这个信息了 2.n是1e9,感觉要递推然后利用数列的加速技 ...
- [hdu 2089] 不要62 数位dp|dfs 入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求[n, m]区间内不含4和62的数字个数. 这题有两种思路,直接数位dp和dfs 数位d ...
- lightoj 1126 - Building Twin Towers(dp,递推)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1126 题解:一道基础的dp就是简单的递推可以设dp[height_left][ ...
- BZOJ5017 [Snoi2017]炸弹[线段树优化建边+scc缩点+DAG上DP/线性递推]
方法一: 朴素思路:果断建图,每次二分出一个区间然后要向这个区间每个点连有向边,然后一个环的话是可以互相引爆的,缩点之后就是一个DAG,求每个点出发有多少可达点. 然后注意两个问题: 上述建边显然$n ...
随机推荐
- [luoguP2982][USACO10FEB]慢下来Slowing down(dfs序 + 线段树)
传送门 这个题显然可以用树链剖分做. 然而线段树也能做. 每个点都对它的子树有贡献,所以先求一边 dfs序,然后直接在 dfs序 中搞 线段树 就行. ——代码 #include <cstdio ...
- 【dfs+理解题意+构造】【待重做】codeforces E. Ice cream coloring
http://codeforces.com/contest/805/problem/E [题意] 染色数是很好确定,最少染色数是max(si)(最小为1,即使所有的si都为0,这样是单节点树形成的森林 ...
- hdu 3940
#include<stdio.h> #include<math.h> #include<string.h> double first(double vx,doubl ...
- Javascript 检查字符串是否是数字的几种方法
//判断是否是正整数 function IsNum(s) { if(s!=null){ var r,re; re = /\d*/i; //\d表示数字,*表示匹配多个数字 r = s.match(re ...
- java 字节码 指令集
有时候为了能理解JVM对程序所做的优化等,需要查看程序的字节码,因此知道了解一些常见的指令集很重要! 指令码 助记符 说明 0x00 nop 什么都不做 0x01 aconst_null 将null推 ...
- 怎么配置ODBC microsoft Access 安装(win10)
笔者看着书配置Access连接方法但就是找不到书中的"Access.mdb". 后来才知道.mdb需要自己创建....对于刚入门的我很无语啊!!!!! 下面带你走出小坑 1.从控制 ...
- MD5加密Java工具类
原文:http://www.open-open.com/code/view/1421764946296 import java.security.MessageDigest; public class ...
- 拷贝地图 CopyAndOverwriteMap()
private void CopyAndOverwriteMap() { //Get IObjectCopy interface IObjectCopy objectCopy = new Object ...
- 深入理解 C 指针阅读笔记 -- 第六章
Chapter6.h #ifndef __CHAPTER_6_ #define __CHAPTER_6_ /*<深入理解C指针>学习笔记 -- 第六章*/ typedef struct _ ...
- 在DIV中自己主动换行
word-break:break-all和word-wrap:break-word都是能使其容器如DIV的内容自己主动换行. 它们的差别就在于: 1,word-break:break-all 比如di ...